user13412285
user13412285

Reputation:

Calling one-parameter function with two arguments?

I have this code:

type Sym = (string * float) list

let rec lookup v = function
  | (v', k) :: vtab -> if v = v' then k else lookup v vtab
  | (_ : Sym)    -> failwith ("unbound: " + v)

To me, it looks like that lookup takes one argument v. But then we do lookup v vtab - now it seems like two arguments are being passed to lookup? How can this be valid when lookup only takes one argument?

Upvotes: 0

Views: 107

Answers (1)

Fyodor Soikin
Fyodor Soikin

Reputation: 80915

It does take two parameters. The first one is v, the second one comes from function.

In F# function is syntactic sugar for match. More specifically, the word function means fun x -> match x with.

So you can read your code as:

let rec lookup v = fun x -> match x with
  | (v', k) :: vtab -> if v = v' then k else lookup v vtab
  | (_ : Sym)    -> failwith ("unbound: " + v)

Which in turn is the same as:

let rec lookup v x = match x with
  | (v', k) :: vtab -> if v = v' then k else lookup v vtab
  | (_ : Sym)    -> failwith ("unbound: " + v)

Upvotes: 2

Related Questions