0

Is it necessary to convert all inputs to lambdas in Haskell?
I implemented map function only converting f to \f.
And it occurs error during runtime(attached below).

map_ xs = \f -> case xs of
                  [] -> []
                  y:ys -> (f y):(map f ys)
map_ square [3, 4]
ERROR - Type error in application
*** Expression     : map_ square [3,4]
*** Term           : [3,4]
*** Type           : [c]
*** Does not match : a -> b

1 Answer 1

4

Is it necessary to convert all inputs to lambdas in Haskell?

No. Indeed lambdas usually only appear in fancy higher-level abstractions.

From

case xs of
  [] -> ...
  y:ys -> ...

the compiler infers that xs's type is a list. From

map_ xs = ...

the compiler infers that list is the first argument to map_.

But in your call to map_, you've put the function as the first argument; the list as second. The standard type for map in the Prelude is

map :: (a -> b) -> [a] -> [b]

IOW function first, list second.

So which do you intend?

Sign up to request clarification or add additional context in comments.

3 Comments

I thought when i denotes 'map_ xs = \f -> ...', it meant i should input in the order of map_ f xs. Your solution really worked out. Thank you!
@blueMountain What ever gave you that idea? map_ is simply a different name. Whereas with map _ you would express that the first argument is completely ignored. Indeed there's also a syntactic shorthand for flipping around arguments, but it requires abuse of infix-section notation: (`map` xs) f
f x = ... and f = \x -> ... are (mostly) equivalent. For "multiple" arguments, you peel them off in the same order; f x y = ... and f x = \y -> ... and f = \x -> \y -> ... all (mostly) equivalent. (I say "mostly", because it's not worth getting into the subtle differences here... and because I can't give an accurate explanation of said differences.)

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.