Advanced Functional Prog.


Introduction (source)

Variables and (recursive) functions definitions can be specified as follow:

let list = [1;2;3]    (** 1::2::3::[] **)

let increment x = x+1 (** fun x -> x+1 **)

let rec func list = 
  match list with
    | []      -> []
    | (x::xs) -> (increment x)::(func xs)

printfn "%A" (func list)

Thus, lists datatype are recursively defined by a particular value (the empty list []) and an operator to add an element to a list (::), and most of the programs on lists will be defined by using pattern matching (i.e. 'case' selection).

Questions: does the integers values are a recursive datatype ? Does trees structures can be recusively defined ?

In particular, can you defined an F# function to compute the sum 1+2+...+n, or the power 2^n or yet x^n ? Can you propose a program that generates the list [1;2;...;n] or again [v;v;...;v] of length n ?

answer
NB. In the answer, the range function does not return the exact result... propose a possible solution to correct that.


2 - 9