0

This is the function:

f []      = []
f (h:t)   = (\x -> x - h) : f t

It takes a list and returns a list of anonymous functions, that substract each each element from x.

Apparently there is some way to write this whole function in 20 characters or less.

I tried to do it with map but that just applies a function to each element of the list. I also tried to replace the anonymous function with (-a) which didn't work either.

Does anybody have an idea?

Cœur
  • 37,241
  • 25
  • 195
  • 267
dYTe
  • 191
  • 1
  • 8

1 Answers1

7

You can simply use:

f :: Num a => [a] -> [a -> a]
f = map subtract

Since subtract :: Num n => n -> n -> n, it thus means that we map every element h from the list to a function subtract h. Subtract h subtracts h from any given value x. So subtract h x == x - h.

Willem Van Onsem
  • 443,496
  • 30
  • 428
  • 555