Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Haskell function composition question

Tags:

haskell

If this works:

Prelude Data.Char> map toUpper ("sdfsd" ++ "dfgfdg")
"SDFSDDFGFDG"

Then why this doesn't?

Prelude Data.Char> map toUpper . (++) "sdfsd" "dfgfdg"

<interactive>:1:14:
    Couldn't match expected type `a -> [Char]'
           against inferred type `[Char]'
    In the second argument of `(.)', namely `(++) "sdfsd" "dfgfdg"'
    In the expression: map toUpper . (++) "sdfsd" "dfgfdg"
    In the definition of `it': it = map toUpper . (++) "sdfsd" "dfgfdg"
like image 766
artemave Avatar asked Mar 02 '10 12:03

artemave


1 Answers

map toUpper . (++) "sdfsd" "dfgfdg"

is parsed as:

(map toUpper) . ((++) "sdfsd" "dfgfdg")

So basically you're doing

(map toUpper) . "sdfsddfgfdg"

This does not work because the second argument to . needs to be a function, not a string.

I assume you were trying to do something more like (map toUpper . (++)) "sdfsd" "dfgfdg". This also does not work because the return type of ++ is [a] -> [a] while the argument type of map toUpper is [a].

The thing here is that while one might think of ++ as a function that takes two lists and returns a list, it really is a function that takes one list and then returns a function which takes another list and returns a list. To get what you want, you'd need to ++ into a function that takes a tuple of two lists and returns a list. That's called uncurrying. The following works:

map toUpper . (uncurry (++)) $ ("sdfsd", "dfgfdg")
like image 82
sepp2k Avatar answered Oct 17 '22 17:10

sepp2k