blueMountain
blueMountain

Reputation: 23

Haskell function implementation using lambdas

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

Upvotes: 0

Views: 95

Answers (1)

AntC
AntC

Reputation: 2806

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?

Upvotes: 4

Related Questions