0

Why are there two ways to define a function in reason, and how exactly are they different (both compile to identical JavaScript)

let f1 = fun (a) => 123;
let f2 a => 123;

try this snippet

glennsl
  • 28,186
  • 12
  • 57
  • 75
dark_ruby
  • 7,646
  • 7
  • 32
  • 57
  • See https://reasonml.github.io/guide/language/function , especially the part starting with 'Since function definitions occur often, ...' – Yawar Oct 09 '17 at 05:00

1 Answers1

4

There are actually three function definition forms:

let f = fun a => fun b => a + b;
let f = fun a b => a + b;
let f a b => a + b;

The two latter forms are really just sugar for the first, which is called the curried form, and is a very handy feature in functional programming because it enables easier function composition through partial application of arguments. But it's not so convenient unless we cover it up with some sugar, hence the second form.

But why not skip the second form and go straight to the third? Well, the second form is a bit "accidental". It comes naturally from two other features: 1. because functions are first-class, meaning they can be defined anonymously and passed around like any other value. And 2. because we can bind variables to names. so just like we can say let x = 2;, we can say let f = fun ..., since a function is just another value.

glennsl
  • 28,186
  • 12
  • 57
  • 75