I have the following Haskell expression:
let increment x = 1 + x
in \xs -> map chr (map increment (map ord xs))
I know that the above expression takes a list, and applies the ord function to each element in the list. For a list of chars, the result would be a list of integers (the ASCII value). The increment function then increments each integer in the list by 1. The chr function then converts each integer back into its corresponding character.
And I also have the following expression:
map chr . map (1+) . map ord
I am trying to figure out whether the above is equivalent to the first expression. However when I tried the above expression in GHCI, I got an error:
map chr . map (1+) . map ord ['a', 'b', 'c', 'd', 'e']
I am not sure why the expression didn't work. Since function composition is used, wouldn't the expression be evaluated as:
(map chr (map (1+) (map ord ['a', 'b', 'c', 'd', 'e'])))
With map (1+) getting a list as a result of map ord, and map chr getting a list as a result of map (1+)?
(map chr . map (1+) . map ord) ['a', 'b', 'c', 'd', 'e']. Otherwise it gets evaluated asmap chr . map (1+) . (map ord ['a', 'b', 'c', 'd', 'e']), which doesn't work as that last bit is a value rather than a function. - bradrnmap chr . map (1+) . map ordis equivalent tomap (chr . (1+) . ord).) - bradrn