P.C.
P.C.

Reputation: 649

How can use let - in for multiple lines in OCaml?

I want to do the following:

let dist = Stack.pop stck and dir = Stack.pop stck in (
    print_endline(dist);
    print_endline(dir);
    ......
    ......
)

The above gives me the following error:

Error: This expression has type unit
       This is not a function; it cannot be applied.

How can I use the variables dist and dir over multiple lines?

Upvotes: 0

Views: 3418

Answers (2)

Thomash
Thomash

Reputation: 6379

The error is not in the piece of code you show here. I guess you forgot a ; somewhere.

But there is a subtle error in your code. In this line of code

let dist = Stack.pop stck and dir = Stack.pop stck in

You expect to obtain the first element of the stack in dist and the second one in dir but it may not be the case as the order of evaluation is unspecified.

Upvotes: 3

Jeffrey Scofield
Jeffrey Scofield

Reputation: 66818

The basic syntax of your code is OK. Here's a simple example showing that it works fine:

$ ocaml
        OCaml version 4.01.0

# let x = 5 and y = 4 in (
  print_int x;
  print_int y;
  );;
54- : unit = ()
#

The reported errors have to do with other problems. We would need more context to see what's wrong. Possibly the errors are in the lines you elided. Or they could be caused by what comes next.

Upvotes: 2

Related Questions