1

I am a bit new to functional programming, and while I am somewhat familiar with F#, I am still learning about all the strange ways it works.

//I love my Rice and Curry'd functions

let add x  = 
   let subFunction y = 
      x + y                   
   subFunction    

//explicit parameter
let add1 y = add 1 y

//implicit parameter 
let add10 = add 10

//calling it with the parameter it doesn't show that it takes
let twenty = add10 10

So here add10 has implicit parameters by the fact that it is calling a function that returns a function that takes a parameter. Why is it accepted that I can declare it that way instead of the same way I declared add1?

It is really deceptive as judging by its declaration it, one would assume its just an int.

Alexander Ryan Baggett
  • 2,347
  • 4
  • 34
  • 61

1 Answers1

6

That's something coming from lambda calculus called eta-reduction

Basically it means that you can simplify your function/lambda by eliminating the last parameter on both sides of the expression when they are the same:

// instead of
let f x y = x + y

// in F# you can write
let f x y = (+) x y

// which is also the same as
let f x y = ((+) x) y

// then you can remove y
let f x   = (+) x

// and then remove x
let f     = (+)

In F# you can play with this as long as you don't reach the value restriction. So in your code let add1 y = add 1 y and let add10 = add 10 are equivalent. This is an example of how can you apply logic to reason about your code, applied to refactoring.

Community
  • 1
  • 1
Gus
  • 25,839
  • 2
  • 51
  • 76