Guest User

Untitled

a guest
Jan 26th, 2018
76
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
OCaml 0.56 KB | None | 0 0
  1. (* Return a function that maps list items using a given function lambda *)
  2. fun map(lambda) = fn ([]) => []
  3.                    | (x::[]) => [lambda(x)]
  4.                    | (xs) => lambda(hd(xs)) :: map(lambda)(tl(xs));
  5.  
  6. (* Return a list of all k element sublists of xs *)                
  7. fun choose(0, xs) = [[]]
  8.   | choose(1, xs) = map(fn x => [x])(xs)
  9.   | choose(k, xs) = if k > length(xs) then []
  10.     else if k = length(xs) then [xs]
  11.     (* The previous case of tl(xs) as well as all the new sublists with
  12.        the head *)
  13.     else choose(k, tl(xs)) @ map(fn y => hd(xs) :: y)(choose(k-1, tl(xs)));
Add Comment
Please, Sign In to add comment