What is the correct and smooth way to write OCaml function?

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?

+4
source share
3 answers

I hope this helps you think in terms of recursion, not in cycles (let it drop tail recursion for a moment).

So you need to calculate f(n) + f(n+1) + ... f(m) . This can help you think about this problem in inductive mode. That is, suppose you know how to calculate f(n+1) + ... + f(m) , then what do you need to do to calculate the original result? well, just add f(n) to the last one, right? This is exactly what your code should say:

 let rec sum nmf = if n = m then fm else fn + sum (n + 1) mf;; (* here the inductive step *) 

You can see how I added f(n) to the result of f(n+1) + .... + f(m) . So, think inductively, put the problem into smaller pieces, and think about how you can put the results of these small pieces together.

I hope I have not become more confused.

+6
source

You made a classic syntax error: sum n+1 mf parsed as (sum n) + (1 nf) instead of what you expect. In OCaml, a functional application (space) takes precedence over infix statements.

A type error occurs because sum n (which you use in the sum) is not an integer. It takes another argument ( m ) and a function that returns an integer. At this point in the type inference process (when an error occurs) OCaml presents it as 'a -> ('a -> int) -> 'b : takes some unknown things a , a function from a to int, and returns some material b .

+6
source

'a is similar to a generic type in Java. For example: let test a = 1 Its type is 'a -> int This function returns 1 regardless of the type of your argument.

The error is that here you need to insert parentheses (sum (n+1) mf)

Ocaml thought of this as additional arguments, and therefore it leads to a different type than you planned. When placing parentheses, make sure that you have the correct number of arguments. This is a subtle debugging issue when you have a lot of codes. Thus, using parentheses in such situations can save you so much time. :)

+1
source

All Articles