Programación Funcional

Algunos ejemplo para empezar con este tipo de programación utilizando F#


open System

 

[<EntryPoint>]

let main argv =

    System.IO.File.ReadLines("input.txt") |> Seq.map Int32.Parse |> Seq.sum |> printfn "%i"

    0 // return an integer exit code

open System

 

let isEven x = (x % 2) = 0

 

let rec result n = seq {

    if n<10 then n |> ignore

    else 

        if isEven (n%10) then yield (n%10)

        yield! result(n/10)

}

 

[<EntryPoint>]

let main argv = 

    System.IO.File.ReadLines("input.txt") |> Seq.map Int32.Parse |> Seq.item 0

    |> result |> Seq.rev |> printfn "%A"

    0 // return an integer exit code

open System

 

let count = 3

let myList = [1; 2; 3]

let myPriceList = [20; 10; 30]

let lengthList = myList |> List.sort

let priceList = myPriceList |> List.sort |> List.rev

 

let getIndex (n: int) = 

    List.findIndex(fun e -> e =  n) lengthList

 

let getPrice (n: int) =

    List.item n priceList

 

let rec iter items = 

  match items with

  | [] -> ()

  | head::tail -> 

     printf "%i " head

     getIndex head |> getPrice |> printfn "%i"

     iter tail

 

[<EntryPoint>]

let main argv =

    iter myList

    

    0 // return an integer exit code

open System

 

let count = 22

let myList= [5 ;6 ;4 ;135 ;4 ;210 ;135 ;135 ;5 ;4 ;4 ;135 ;4 ;6 ;6 ;135 ;6 ;5 ;5 ;4 ;5 ;6]

let resultList = List.distinct myList

 

let rec iter items = 

  match items with

  | [] -> ()

  | head::tail -> 

     printf "%i " head

     List.countBy(fun e -> if (e = head) then 1 else 0) myList 

     |> List.filter(fun (e,x) -> e = 1) |> List.item(0) |> snd |> printfn "%A"

     iter tail

 

[<EntryPoint>]

let main argv =

    iter resultList

    

    //for i in resultList do

    //    printf "%i " i

    //    List.countBy(fun e -> if (e = i) then 1 else 0) myList |> List.filter(fun (e,x) -> e = 1) |> List.item(0) |> snd |> printfn "%A"

    0 // return an integer exit code