Reputation: 447
First of all, I am very new to Haskell and for the moment I am just trying to prepare for an exam.
I have this expression:
reverse . take 3 [1 .. 10]
and what I get is an error. Is that because space operator has bigger precedence (10) than .
operator (9) and the expression above is equivalent to reverse . (take 3 [1..10])
which is reverse . ([1, 2, 3])
which is a composition between reverse and a list which makes no sense, right? I am trying to make sure I got that right, I didn't really find something similar on the internet.
Upvotes: 15
Views: 941
Reputation: 10998
I like to use GHCi and look at the types - let's see:
reverse . take 3 [1 .. 10]
error:
• Couldn't match expected typea -> [a1]
with actual type[a0]
• Possible cause:take
is applied to too many arguments
In the second argument of(.)
, namelytake 3 [1 .. 10]
In the expression:reverse . take 3 [1 .. 10]
:t take 3 [1 .. 10]
take 3 [1 .. 10] :: (Num a, Enum a) => [a]
yields type [a]
- let's see what (.)
is expecting
:t (.)
(.) :: (b -> c) -> (a -> b) -> a -> c
:t reverse
reverse :: [a] -> [a]
reverse fits the first part of what (.)
expects
:t take 3
take 3 :: [a] -> [a]
type fits as the second argument to what (.)
expects
:t reverse . take 3
reverse . take 3 :: [a] -> [a]
resulting function takes a list of a
and returns a list of a
, and so we only need to give it a list of a
; being explicit here:
(reverse . take 3) [1 .. 10]
Upvotes: 1
Reputation: 48572
You're basically correct. Prefix function application (what you called the "space operator") binds more tightly than any infix operator does. And for completeness, the way to fix the error is to do (reverse . take 3) [1 .. 10]
or reverse . take 3 $ [1 .. 10]
instead.
Upvotes: 16