Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- // let x = 1::2::3::[]
- let x = [1;2;3]
- let rec sum = function
- | [] -> 0
- | x::xs -> x+sum xs
- let len L =
- let rec len' acc = function
- | [] -> acc
- |_::xs -> len' (acc+1) xs
- len' 0 L
- len [1..100]
- let rec fmin = function
- | [x] -> (x, [])
- | x::xs ->
- let (z,zs) = fmin xs
- if z>x then (x,xs)
- else (z, x::zs)
- fmin [3;1;2]
- let rec sort = function
- | [] -> []
- | [x] -> [x]
- | L -> let (x,xs) = fmin L
- x::sort xs
- sort [1;4;2;5;3]
- let rec fold f i = function
- | [] -> i
- | x::xs -> f x (fold f i xs)
- // fold is like reduce
- let sum = fold (+) 0
- sum [1..100]
- let minel = fold min (System.Int32.MaxValue)
- minel [2;1;3]
- let length L = fold (fun _ acc->acc+1) 0 L
- length [2;1;3;4]
- let minmax L = fold (fun x (mi,ma) -> (min mi x, max ma x)) (List.head L, List.head L) L
- minmax [1;4;3;2]
- let minel L = List.reduce min L
- let sum = List.reduce (+)
- List.map (fun x->x*2) [1;2;3]
- // created own map
- let map f L = fold (fun x acc -> (f x)::acc) [] L
- map ((*)2) [1;2;3]
- [1..100]
- |> List.map (fun x -> x*x)
- |> sum
- // prime numbers:
- let flip f x y = f y x
- [2..100] |> List.filter ((%)3 >> (<)0)
- let rec primes = function
- | [] -> []
- | x::xs -> x::(primes(List.filter (flip(%)x >> (<)0) xs))
- primes [2..100]
- List.map2 (*) [1;2;3] [4;5;6]
- [1..10] |> List.iter (printf "%d ")
- let L = [1..100]
- // never do this!!! n^2 complexity!!:
- for i = 0 to List.length(L)-1 do printf "%d " L.[i]
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement