Search code examples
haskellanonymous-function

Haskell: anonymous function with two arguments


I have the following anonymous function:

(\x y -> y+y) (5*(7+20))

As far as I understand anonymous functions, x should be (5*(7+20)) and y is not given (which is where it gets fishy). When I try to execute that function, GHCI tells me the return value is

Integer -> Integer

So obviously my interpretation is wrong here and I just can't see why. Can anyone explain to me what is happening here?


Solution

  • Look at it this way: if you had provided a value for y, you'd get an integer. If you don't provide a value, you'll get an expression which takes an integer (which you call y) and returns an integer, i.e. a function

    Integer -> Integer
    

    This works for named functions too. E.g.

    plus :: Int -> Int -> Int
    plus x y = x + y
    

    You can check in ghci that the type of plus 1 is Int -> Int. In fact, this process works for any function in Haskell. You read more at the HaskellWiki.