This exercise is on book《Functional Programming Using F#》.
Exercise 4.18
let rec f g = function
| [] -> []
| x::xs -> g x :: f (fun y -> g(g y)) xs;;
//Find the type for f and explain the value of the expression:
f g [x0; x1; x2; . . . ; xn−1]
-
From
let rec f g = function
, we can not make sure anything about f and g.f: ??->??
-
According to the
[]->[]
, it is clear that the result off
is a type of list, but we can not deduce which type it is. And the input off
is also a list.We could guess that(It is wrong after reading the next line). For we can not deduce the concrete type so here we set it as an abstract type :g
is an argument whose type is a list'a
f: ‘a list -> ‘a list
-
The pattern
x::xs
ensures our deduce of the input of f is a list. But then we find the usage ofg x
. So we get an error deduce thatg
is an argument of list– actuallyg
is a function here. So there is an additional parameter_arg
which is a list.f: g:(??->??)->_arg: ‘a list -> ‘a list
Then with
f (fun y -> g(g y)) xs
, we could ensure thatf
is a function with two parameters:(fun y -> g(g y))
(g) andxs
(_arg). And withg(g y))
,we could determine that the input and result ofg
is the same.f: g:(‘a ->’a )->_arg: ‘a list -> ‘a list