user2877617
user2877617

Reputation:

How to print function return value in Ocaml

How to print the integer type return value of a recursive function in Ocaml.I want to find factorial of a number.I use the following code.But it shows error

let rec factorial x = 
    if (0 > x) then (raise Exit) 
    else
        match x with
            | 0 -> 1
            | n -> (n * (factorial (n - 1)))
        print_int (n * (factorial (n - 1)));;

The following error shows when trying to run it:

This expression is not a function; it cannot be applied

Upvotes: 1

Views: 5610

Answers (2)

Bardou
Bardou

Reputation: 446

I'm not sure I understand the question nor zurgl's answer, so here is a shot in the dark. Did you forget the ";;" between your function definition and the "print_int" line? It's not clear from your post, but if you wrote:

let rec factorial x = 
if (0 > x) then (raise Exit) else
match x with
      0 -> 1
    | n -> (n * (factorial (n - 1)))

print_int (n * (factorial (n - 1)));;

then it is the same as:

let rec factorial x = 
if (0 > x) then (raise Exit) else
match x with
      0 -> 1
    | n -> (n * (factorial (n - 1))) print_int (n * (factorial (n - 1)));;

So what you want is:

let rec factorial x = 
if (0 > x) then (raise Exit) else
match x with
      0 -> 1
    | n -> (n * (factorial (n - 1)));;

print_int (n * (factorial (n - 1)));;

Or, with no ";;" (which are a little out of fashion):

let rec factorial x = 
if (0 > x) then (raise Exit) else
match x with
      0 -> 1
    | n -> (n * (factorial (n - 1)))

let () = print_int (n * (factorial (n - 1)))

Of course this has another problem which is that n is unbound when you call print_int, which is why I'm not sure I understood your question. But the following code works fine:

let rec factorial x = 
if (0 > x) then (raise Exit) else
match x with
      0 -> 1
    | n -> (n * (factorial (n - 1)))

let () = print_int (factorial 10)

Upvotes: 2

zurgl
zurgl

Reputation: 1930

The reason of you error is due to the way the branch's type of your code are inferred.
Regarding of first branch, the type checker infer that an int will be produce by your function but at the end of your function body you're calling an expression which are going to produce an unit type (), thus the type of your function cannot be inferred correctly then you end with this kind of error message.

To avoid this you need to notify to your compiler than this expression should not be take into account, to achieve this you can use the function ignore which as type annotation 'a -> unit = <fun>.

I've slightly modified the meaning of your code to give you an illustration.

let rec factorial = function
  | n when n<0 -> raise (Failure "undefined: positive integer is required.")  
  | 0 -> 0
  | 1 -> ignore (Printf.printf "1\n"); 1 
  | n -> ignore (Printf.printf "%d*" n); n * factorial (n-1) 
;;
val factorial : int -> int = <fun>   

At the execution you will end with.

factorial 10;;  
10*9*8*7*6*5*4*3*2*1
- : int = 3628800                                                                       - : 

Upvotes: 1

Related Questions