Advanced Functional Prog.


Introduction

Next, FP is well-suited to dataflow processing (see "pipe" function/operator) and the preceding program can be simplified to:

[1;2;3] |> (List.map (fun x->x+1));;

Now, other generic usefull functions are: filter, zip, @, etc:

[1;2;3] |> (List.filter (fun x->(x%2)=0));;
[1;2;3] |> (List.zip ["a";"b";"c"]);;
[1;2] @ [3];;

Exercice: the "zip" function composed with "map" can be used to generalize binary operators, but how to directly write this operation (illustrated below with the shift function) ? Is it more interesting that the composition of zip-map ?
answer

shift (+) [1;2] [3;4] = [4;6];;

Note. Binary functions can be written as an infix operator:

let (<+>) = shift (+);;
[1;2] <+> [3;4];;

Exercise: how to make matrix operators, eg. [[1;2];[3;4]] <++> [[5;6];[7;8]] ?


5 - 9