Reputation: 1110
Haskell newbie here. I'm playing around with this expression:
"The sum of 3 and 4 is " ++ (show (3 + 4))
Which evaluates fine. This is also fine:
"The sum of 3 and 4 is " ++ (show $ 3 + 4)
But I get a 'parse error on input $' when I replace the last pair of parenthesis with $
:
"The sum of 3 and 4 is " ++ $ show $ 3 + 4
which I'm not really sure why. I'm following the LearnYouAHaskell series which says:
'$ being sort of the equivalent of writing an opening parentheses and then writing a closing one on the far right side of the expression.'
What have I missed?
Upvotes: 2
Views: 208
Reputation: 11924
Partially applied infix operators need to be enclosed in parentheses. So:
"The sum of 3 and 4 is " ++ $ show $ 3 + 4
fails, but...
("The sum of 3 and 4 is " ++) $ show $ 3 + 4
works just fine. Note that you could also use the (.)
function to compose the functions, like so:
("The sum of 3 and 4 is " ++) . show $ 3 + 4
... but that comes later in learn-you-a-haskell.
Also, as @Shanthakumar posted, it might be desirable to do it this way:
(++) "The sum of 3 and 4 is " $ show $ 3 + 4
Upvotes: 3