I have a question about ocaml, i'm a beginner :-)
Here is an example of what i'm trying to do : (I know this is non-sense but it's not my real code, it's just an example)
let func a b = a
let func2 a b = b
let func_a a b =
if b < 0 then
func_b b a
else
func a b
let func_b a b =
if a < 0 then
func2 a b
else
func_a b a
The problem is:
Unbound value func_b in the first "if" in func_a...
If anyone could help?
Edit: I understand why this is unbound, but I dont know how to fix it.
Thanks a lot!
Max
The keyword is mutually recursive functions:
let func a b = a
let func2 a b = b
let rec func_a a b =
if b < 0 then
func_b b a
else
func a b
and func_b a b =
if a < 0 then
func2 a b
else
func_a b a
Related
I am new to F# and haven't done functional programming since I was an undergraduate, but I've been trying teach myself. I wrote a naive recursive Extended Euclidean implementation, which works just fine, and am now trying again but with continuations.
I walked through the code by hand twice with a small example and got the correct answer, but when I run it through the interpreter I am not getting the same result, so I'm clearly misunderstanding something I am trying to do.
I ran eea 7 3 by hand, and the (correct) result I computed was (1, 1, -2)
But when I run it in the interpreter I get
eea 7 3;;
val it : int * int * int = (1, 0, 1)
Here's my implementation:
let eea a b =
let rec contEEA a b f =
match b with
| 0 -> f () (a,1,0)
| _ ->
contEEA b (a%b) (fun () t ->
let (d,x',y') = t
(d, y', x'-(y'*(a/b)))
)
contEEA a b (fun () t -> t)
For reference the naive approach, straight from a textbook, is
let rec eea_gcd a b =
match b with
| 0 -> (a, 1, 0)
| _ ->
let d, x', y' = eea_gcd b (a % b)
(d, y', x'-(y'*(a/b)))
Your continuation-based version is always doing exactly one iteration (the last one). When you make the recursive call, your continuation just straight up returns the result instead of "returning" it to the previous call by passing to the previous continuation.
So the call sequence goes like this:
eea 7 3
contEEA 7 3 (fun () t -> t)
b <> 0 ==> second case matches
contEEA 3 1 (fun () t -> ... (d, y', ...))
b <> 0 ==> second case matches
contEEA 1 0 (fun () t -> ... (d, y', ...))
b = 0 ==> first case matches
The continuation is called f () (1, 1, 0)
The continuation calculates result (1, 0, 1 - (0*(3/1)) = (1, 0, 1) and immediately returns it
What you want to do instead is when the first continuation calculates the result of (1, 0, 1) it should pass it to the previous continuation, so that it may carry on the calculations from there, ultimately passing the result to the very first continuation fun () t -> t, which returns it back to the consumer.
To do that, replace this line:
(d, y', x'-(y'*(a/b)))
With this:
f (d, y', x'-(y'*(a/b)))
Also, a few notes on some other aspects.
The first parameter of the continuation (the unit, ()) is not necessary, since it's never actually used (and how can it be?). You can lose it.
After removing the unit parameter, the first continuation becomes fun t -> t, which has a special name id (aka "the identity function")
Rather than destructure the triple with a let, you can do it right in the parameter declaration. Parameters can be patterns!
Applying all of the above, as well as the actual problem fix, here's a better version:
let eea a b =
let rec contEEA a b f =
match b with
| 0 -> f (a,1,0)
| _ ->
contEEA b (a%b) (fun (d,x',y') ->
f (d, y', x'-(y'*(a/b)))
)
contEEA a b id
I want to get the sum of function f(i) values when i is equal from a to b
= f(a)+f(a+1)+...+f(b-1)+f(b)
So I wrote code like this.
let rec sigma : (int -> int) -> int -> int -> int
= fun f a b ->
if a=b then f a
else f b + sigma f a b-1 ;;
but result is that there is stack overflow during evaluation. Is there a infinite loop? and why?
sigma f a b-1 is parsed as (sigma f a b) - 1 instead of your intention, sigma f a (b-1). Since sigma f a b calls sigma f a b recursively in your code, it never stops.
The best practice is to put white spaces around binary operators like sigma f a b - 1 so that you would not misread what you write.
Why is the following code giving me that error?
Note that the is_sorted function returns either true or false
and make_move function returns a list of lists. e.g [[0,1,3,2],[1,0,2,3]]
let rec solve_helper b pos n r fn =
if n = 0 then b :: r :: fn (*fn is the final array with all paths*)
else match pos with
[] -> fn
|(h::t) -> if is_sorted h = true then h
else h :: r (* ERROR HERE: r is the temp array that contains 1 path*)
solve_helper b (make_moves h) (n-1) r
solve_helper b t (n-1) r (*tail recursion*)
;;
let solve_board b n = solver_helper b (make_moves b) n [] []
;;
new code:
let rec solve_helper b pos n r fn =
if n = 0 then r :: fn (*fn is the final array with all paths*)
else match pos with
[] -> fn
|(h::t) -> if is_sorted h = true then
let j = h :: r in
r :: fn
else
let u = h :: r in
let k = solve_helper b (make_moves h) (n - 1) r fn in
solve_helper b t (n - 1) r fn(*tail recursion*)
;;
let solve_board b n = solve_helper b (make_moves b) n [] []
;;
These lines of your code:
else h :: r (* ERROR HERE: r is the temp array that contains 1 path*)
solve_helper b (make_moves h) (n-1) r
solve_helper b t (n-1) r (*tail recursion*)
do not make sense as far as I can tell. They represent a call to a function named r with 10 arguments (two of which are the function r itself).
Possibly you need to edit your code to show exactly what the compiler is seeing.
If your code actually looks like this, you need to rethink this part. It reads like imperative code (a series of things to do) rather than functional code (an expression consisting of functions applied to arguments).
Is it possible to have nested if without else statements. I wrote the following useless program to demonstrate nested ifs. How do I fix this so it's correct in terms of syntax. lines 5 and 6 gives errors.
let rec move_helper b sz r = match b with
[] -> r
|(h :: t) ->
if h = 0 then
if h - 1 = sz then h - 1 ::r
if h + 1 = sz then h + 1 ::r
else move_helper t sz r
;;
let move_pos b =
move_helper b 3 r
;;
let g = move_pos [0;8;7;6;5;4;3;2;1]
You can't have if without else unless the result of the expression is of type unit. This isn't the case for your code, so it's not possible.
Here's an example where the result is unit:
let f x =
if x land 1 <> 0 then print_string "1";
if x land 2 <> 0 then print_string "2";
if x land 4 <> 0 then print_string "4"
You must understand that if ... then is an expression like any other. If no else is present, it must be understood as if ... then ... else () and thus has type unit. To emphasize the fact that it is an expression, suppose you have two functions f and g of type, say, int → int. You can write
(if test then f else g) 1
You must also understand that x :: r does not change r at all, it constructs a new list putting x in front of r (the tail of this list is shared with the list r). In your case, the logic is not clear: what is the result when h=0 but the two if fail?
let rec move_helper b sz r = match b with
| [] -> r
| h :: t ->
if h = 0 then
if h - 1 = sz then (h - 1) :: r
else if h + 1 = sz then (h + 1) :: r
else (* What do you want to return here? *)
else move_helper t sz r
When you have a if, always put an else. Because when you don't put an else, Java will not know if the case is true or false.
I'm trying to write a function that accepts an int n and returns a list that runs down from n to 0.
This is what I have
let rec downFrom n =
let m = n+1 in
if m = 0 then
[]
else
(m-1) :: downFrom (m - 1);;
The function compiles ok but when I test it with any int it gives me the error
Stack overflow during evaluation (looping recursion?).
I know it's the local varible that gets in the way but I don't know another way to declare it. Thank you!!!
First, the real thing wrong with your program is that you have an infinite loop. Why, because your inductive base case is 0, but you always stay at n! This is because you recurse on m - 1 which is really n + 1 - 1
I'm surprised as to why this compiles, because it doesn't include the rec keyword, which is necessary on recursive functions. To avoid stack overflows in OCaml, you generally switch to a tail recursive style, such as follows:
let downFrom n =
let rec h n acc =
if n = 0 then List.rev acc else h (n-1) (n::acc)
in
h n []
Someone suggested the following edit:
let downFrom n =
let rec h m acc =
if m > n then acc else h (m + 1) (m::acc)
in
h 0 [];
This saves a call to List.rev, I agree.
The key with recursion is that the recursive call has to be a smaller version of the problem. Your recursive call doesn't create a smaller version of the problem. It just repeats the same problem.
You can try with a filtering parameter
syntax:
let f = function
p1 -> expr1
| p2 -> expr2
| p3 -> ...;;
let rec n_to_one =function
0->[]
|n->n::n_to_one (n-1);;
# n_to_one 3;;
- : int list = [3; 2; 1]