Hacker News new | ask | show | jobs
by ncd 3667 days ago
It's a function that takes a String and returns a new function which takes a String and returns a String. All functions in Haskell/Elm are arity 1.

So in order to construct functions that accept more than one argument, you actually return successive functions that apply successive arguments, known as currying.

2 comments

This is really important when looking at it from "the outside" (or, one is too lazy to lookup the documentation ;-). Without knowing that, it could be a function that takes a string and compiles it to a function that takes two strings (ie: a macro, I guess). Or one string, and returns a string. Or...

Does this kind of 1/arity have performance implications for Haskell? Does the compiler lower (or is it rise? inline?) the functions to produce efficient machine code?

Thanks! Is this the case in Elm as well? This is the example they give:

  connectWords : String -> String -> String
  connectWords firstWord secondWord =
    firstWord ++ secondWord
In this case, you think of `connectWords` as a function that takes two arguments. But since it is curried, you can also do this:

    let prefix = connectWords "Hello "
        world  = prefix "world"
        bob    = prefix "bob"
    in ...
`world` is "Hello world", and `bob` is "Hello bob". That is the power of currying. A maybe more useful example is specifying the mapping function in `List.map` without supplying the list to map over. This allows you to use the same map with multiple lists.
Yes, all functions in Elm also have arity 1. That example desugars into this:

    connectWords = \ firstWord -> \ secondWord -> firstWord ++ secondWord