Take the 2-minute tour ×
Programmers Stack Exchange is a question and answer site for professional programmers interested in conceptual questions about software development. It's 100% free, no registration required.

So I have been using F# for a while and studying a bit of Haskell on the side and I have realized I could rewrite the exact same function one of three different ways.

Either with implicit currying, explicit currying, or with lambda expressions.

//lambdas
let add'  =  fun x -> fun y -> x + y

//explicit currying
let add' x  =     
   let subFunction y = 
      x + y                    
   subFunction   

//implicit currying
let add' x y = x + y

All of these have the signature int -> int -> int. All of them do the same thing.

Which one should I use in which case?

share|improve this question
2  
The one that is most readable and shortest. –  svick Mar 2 at 13:36

1 Answer 1

up vote 5 down vote accepted

In the situations similar to the one you've described you should always use the third option, or, better yet, just let add' = (+) (but beware of Monomorhism Restriction). I see no sense in the other two definitions you have provided.

However, it's common to use nested functions in a way similar to your second example if the nested function takes auxiliary arguments, such as an accumulator in tail-recursive functions.

share|improve this answer
    
Yes I was aware of the (+) option, but I did not list it because it was only applicable when converting an infix operator to a function. –  Alexander Ryan Baggett Mar 2 at 10:08
4  
I would disagree. Compare this: sum xs = fold (+) xs and this: sum = fold (+). In the second case, you have a (partially applied) function on the right hand side, so it is similar to the let add = (+). –  M-x Mar 2 at 10:16

Your Answer

 
discard

By posting your answer, you agree to the privacy policy and terms of service.

Not the answer you're looking for? Browse other questions tagged or ask your own question.