Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Why one parameter Ocaml function works with two arguments

I can't understand why the following function works with 2 arguments even if we declare it with one param:

let rec removeFromList e = function
   h :: t -> if h=e then h 
             else h :: removeFromList e t
   | _ -> [];;

removeFromList 1 [1;2;3];;
like image 617
Claudiu Razlet Avatar asked Jun 01 '26 19:06

Claudiu Razlet


1 Answers

You're declaring it with two parameters. The syntax:

let f = function ...

can be seen as a shortcut for

let f x = match x with

So, your definition is actually:

let rec removeFromList e lst = match lst with
  h :: t -> if h=e then h else h :: removeFromList e 
like image 129
ivg Avatar answered Jun 03 '26 15:06

ivg



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!