Advanced Functional Prog.


Packages' Builder

B. Standard Lists' operations

  1. Recall what is the definition of reduce function on lists.
  2. With the preceding function, implements the standard operators/functions:
    • (@) that catenates two lists: [1;2]@[3;4]=[1;2;3;4]
    • map f that applies f to all the values of a list: map ((+) 1) (1<>4)=2<>5
  3. reduce is interesting to transforms binary operators into n-ary ones. For instance, how to:
    • sum all the values of a list by using reduce
    • join all the lists of a list
    • get the length of a list
  4. With p:'t->bool, to what function corresponds the following expression ?
    join (map (fun x->if (p x) then [x] else []) xs)
  5. list is an example of what is called a "monad", a mathematical concept (functor) with two operations called "return" and "bind" (:a kind of composition operator). In particular, return/bind can be defined as follow:
let ret   x    = [x];;
let (>>=) xs f = join (map f xs);;

With these definitions, give the result of the following program ?

(1<>4) >>= fun i -> 
(1<>4) >>= fun j -> 
if (i<=j) then ret (i<>j) else [];;

Can you give a sample use of such a program ?

answers