Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- // Learn more about F# at http://fsharp.net
- // See the 'F# Tutorial' project for more help.
- exception Empty
- let hd list =
- match list with
- | [] -> raise Empty
- | hd::tl -> hd
- let tl list =
- match list with
- | [] -> raise Empty
- | hd::tl -> tl
- let zero x y z =
- x + y + z
- let one x y =
- (y,x)
- let two (a, b) (c, d) =
- (a, b, c, d)
- let three f g (a : int, b : int, c : int) =
- (f a |> g, f b |> g, f c |> g)
- let four =
- fun a b c -> a + b + c
- let five =
- ()
- let six arr =
- (arr |> Array.min, arr |> Array.averageBy (fun elem -> float elem), arr |> Array.max)
- let seven arr =
- arr |> Array.sumBy (fun elem -> float elem)
- let rec eight n =
- match n with
- | 0 -> 1
- | _ -> n * eight (n - 1)
- let rec eightSimple n =
- List.fold (fun acc elem -> acc * elem) 1 [1 .. n]
- let nine list index =
- List.nth list index
- let rec nineRecursive list index =
- let rec foo list index i =
- match i = index with
- | false -> foo (tl list) index (i + 1)
- | true -> hd list
- foo list index 0
- let ten list n =
- let rec foo array n i =
- match i < n with
- | true -> hd array + foo (tl array) n (i + 1)
- | false -> 0
- foo list n 0
- let tenWithCheats list n =
- Seq.fold (fun acc elem -> (acc + elem)) 0 (list |> Seq.take(n))
- let eleven list n =
- ten (List.rev list) n
- [<EntryPoint>]
- let main argv =
- printfn "zero 2 3 5 = %i" (zero 2 3 5)
- printfn "one 3 5 = %A" (one 3 5)
- printfn "two (2 3) (5 4) = %A" (two (2,3) (5, 4))
- printfn "three fn x => x + 1 fm x => x * 2 (1, 2, 3) = %A" (three (fun x -> x + 1) (fun x -> x * 2) (1, 2, 3))
- printfn "eight 3 = %i" (eightSimple 3)
- printfn "nine [1 .. 25] 15 = %A" (nineRecursive [1 .. 25] 15)
- printfn "tenRetared [1 .. 10] 5 = %i" (tenWithCheats [1 .. 10] 5)
- printfn "eleven [1 .. 10] 5 = %i " (eleven [1 .. 10] 5)
- System.Console.ReadLine()
- 0 // return an integer exit code
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement