Advanced Functional Prog.
Packages' Builder
B. Standard Lists' operations
- Recall what is the definition of
reduce
function on lists.
- 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
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
len
gth of a list
- With
p:'t->bool
, to what function corresponds the following expression ?
join (map (fun x->if (p x) then [x] else []) xs)
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
advanced functional programming programmation fonctionnelle avancée dr mulhouse ensisa france thiry laurent uha fun fsharp
advanced functional programming programmation fonctionnelle avancée dr mulhouse ensisa france thiry laurent uha fun fsharp
advanced functional programming programmation fonctionnelle avancée dr mulhouse ensisa france thiry laurent uha fun fsharp
advanced functional programming programmation fonctionnelle avancée dr mulhouse ensisa france thiry laurent uha fun fsharp
advanced functional programming programmation fonctionnelle avancée dr mulhouse ensisa france thiry laurent uha fun fsharp
advanced functional programming programmation fonctionnelle avancée dr mulhouse ensisa france thiry laurent uha fun fsharp
advanced functional programming programmation fonctionnelle avancée dr mulhouse ensisa france thiry laurent uha fun fsharp
advanced functional programming programmation fonctionnelle avancée dr mulhouse ensisa france thiry laurent uha fun fsharp
advanced functional programming programmation fonctionnelle avancée dr mulhouse ensisa france thiry laurent uha fun fsharp
advanced functional programming programmation fonctionnelle avancée dr mulhouse ensisa france thiry laurent uha fun fsharp
advanced functional programming programmation fonctionnelle avancée dr mulhouse ensisa france thiry laurent uha fun fsharp