Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- let length list =
- let rec loop l ac =
- match l with
- | head :: tail -> loop tail (ac + 1)
- | [] -> ac
- loop list 0
- let mapr f list =
- let rec loop l ac =
- match l with
- | head :: tail -> loop tail (ac @ [f head])
- | [] -> ac
- loop list []
- let maxl list =
- if list = [] then
- raise (System.ArgumentException())
- else
- let rec loop ac = function
- | head :: tail -> loop (max head ac) tail
- | [] -> ac
- let h::t = list
- loop h t
- let minli list =
- if list = [] then
- raise (System.ArgumentException())
- else
- let rec loop l ac i ai =
- match l with
- | head :: tail ->
- let t = min head ac
- if t <> ac then loop tail t (i + 1) i
- else loop tail t (i + 1) ai
- | [] -> ai
- let h::t = list
- loop t h 0 0
- let rec append l1 l2 =
- match l1 with
- | head :: tail -> head :: (append tail l2)
- | [] -> l2
- let rec appendr list = function
- | head :: tail -> appendr (list @ [head]) tail
- | [] -> list
- let primes n =
- let rec loop list i =
- match i < n with
- | true -> loop [for x in list do if x = i || x % i <> 0 then yield x] (i + 1)
- | false -> list
- loop [2..n] 2
- let listse one two =
- let rec loop list1 list2 outlist =
- match list1 with
- | h::t ->
- match list2 with
- | [] -> outlist @ list1
- | h2::t2 -> loop t t2 (outlist @ [h] @ [h2])
- | [] -> outlist @ list2
- loop one two []
- let nconcat list =
- let rec loop l outlist =
- match l with
- | h::t -> loop t (outlist @ h)
- | [] -> outlist
- loop list []
- [<EntryPoint>]
- let main argv =
- printfn "%d" (length [1; 2; 3])
- printfn "%A" (map ((+) 1) [1; 2; 3])
- printfn "%A" (mapr ((+) 2) [1; 2; 3])
- printfn "%d" (maxl [1; 3; 2])
- printfn "%d" (maxli [1; 3; 2])
- printfn "%A" (append [1; 2; 3] [4; 5; 6])
- printfn "%A" (appendr [1; 2; 3] [4; 5; 6])
- printfn "%A" (primes 20)
- printfn "%A" (listse [1; 2; 3] [4; 5; 6])
- printfn "%A" (nconcat [[1; 2; 3]; [4; 5; 6]; [7; 8; 9]])
- 0 // return an integer exit code
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement