0
votes

Reading the excellent CoffeeScript Ristretto, I ran into this function:

((x) ->
  (y) ->
    x
)(1)(2)

My understanding is that the result x is the same as the input x.

Would the Haskell version(*) look like this?

foo :: a -> b -> (a -> b) -> a
foo x y f = x

*Note - I don't know if there's a 1-1 translation given Haskell's robust type system v. CoffeeScript

2
Why your foo has three parameters?Lee Duhem
Ah, I think that I was forgetting that all of Haskell's functions are curried. So foo :: a -> b -> a (as @Benesh pointed out) is the same as the above CoffeeScript function. But, as I just read, a closure is used here. So, I'm not sure if the Haskell version is equivalent...Kevin Meredith
Does foo = \x -> \y -> x or (\x -> \y -> x) 1 2 look more like your original function?Lee Duhem

2 Answers

5
votes

I think you're looking for const:

const :: a -> b -> a

A "functional" look at const is that for all x :: a, const x is the constant function of type b -> a - it returns x for any value.

A possible implementation is:

const x y = x

So const 1 2 == 1.

3
votes

Looks like a direct translation to Haskell is

(\x -> \y -> x) 1 2

So you can define that foo as

foo = \x -> \y -> x

This is const, as pointed out by @Benesh.