I am studying Jason Hickey's Introduction to Objective Caml .
After I learned Chapter 3, I understand how let and fun . But still I have problems to write my own fun .
Here is an example of the problem I am facing.
Write a function sum that, given two integer bounds n and m and a function f, computes a summation (no for loop allowed). ie, sum nmf = f(n) + f(n+1) + ... + f(m)
So, how do I start thinking about creating this sum of functions?
In Java or a common programming language, this is easy.
Since it is not allowed for a loop here, so I think I should do it in a let rec way?
Something like that:
let rec sum nmf = fun i -> ....
Do I need i be the cursor?
Be that as it may, I cannot continue to think.
Can someone show me the way to create OCaml fun?
This is my final decision:
let rec sum nmf = if n <= m then (fn)+(sum n+1 mf) else 0;;
but of course this is wrong. Error Error: This expression has type 'a -> ('a -> int) -> 'b but an expression was expected of type int
Why? and what is a?