I started learning Haskell and I encountered a problem I can't just understand. I've got a method used to find value from a list of key-value list (from this page):
let findKey key xs = snd . head . filter (\(k,v) -> key == k) $ xs
I tried fiddling with a bit and decided to get rid of $ sign in this way:
let findKey key xs = snd . head . filter (\(k,v) -> key == k) ( xs )
However, it doesn't even parse (filter applied to too many argumens error). I've read that $ sign is used to simply replace parenthesis and I can't figure out why this simple change of code is bad. Could someone explain it to me?
The unsigned right shift operator ">>>" shifts a zero into the leftmost position, while the leftmost position after ">>" depends on sign extension. Show activity on this post. >>> will always put a 0 in the left most bit, while >> will put a 1 or a 0 depending on what the sign of it is. Show activity on this post.
‘%’ is modulo operator while ‘ /’ is a division operator. If we’ll talk as compiler-developers, the “%” operator may be a modulo, or the first operator of digraphs, and the “/” operator may be the first part on one-line-comment, or may be a division operator, or may be the part of “/=” operator.
Both / and % are two different operators used in Java. These operators are mathematical operators and both have different uses. / Only perform the division operation in mathematics and returns results as the quotient, while % is known as modulus and returns results as the remaining part performed.
Both ‘%’ and ‘/’ are binary operators. ‘%’ is known as the modulus operator or the remainder operator; it is used to find the remainder of division of two numbers. ‘/’ is known as the division operator; it is used to find the quotient in the division of two numbers.
The infix operator ($)
is just "function application". In other words
f x -- and
f $ x
are the same. Since in Haskell parentheses are only used to disambiguate precedence (and for tuple notation and a few other minor places, see comments) we can also write the above in a few other ways
f x
f $ x
(f) x
f (x)
(f) (x) -- and even
(f) $ (x)
In every case, the above expressions denote the same thing: "apply the function f
to the argument x
".
So why have all this syntax? ($)
is useful for two reasons
In the first case, consider the following deeply right-nested function application
f (g (h (i (j x))))
It can be a little difficult to read this and a little difficult to know you have the right number of parentheses. However, it's "just" a bunch of applications so there ought to be a representation of this phrase using ($)
. Indeed there is
f $ g $ h $ i $ j $ x
Some people find this easier to read. More modern style also incorporates (.)
in order to emphasize that the whole left side of this phrase is just a composed pipeline of functions
f . g . h . i . j $ x
And this phrase is, as we saw above, identical to
(f . g . h . i . j) x
which is sometimes nicer to read.
There are also times when we want to be able to pass around the idea of function application. For instance, if we have a list of functions
lof :: [Int -> Int]
lof = [ (+1), (subtract 1), (*2) ]
we might want to map application by a value over them, for instance apply the number 4
to each function
> map (\fun -> fun 4) lof
[ 5, 3, 8 ]
But since this is just function application, we can also use section syntax over ($)
to be a bit more explicit
> map ($ 4) lof
[ 5, 3, 8 ]
The operator $
has the lowest priority, so
snd . head . filter (\(k,v) -> key == k) $ xs
is read as
(snd . head . filter (\(k,v) -> key == k)) xs
while your second expression is rather
snd . head . ( filter (\(k,v) -> key == k) xs )
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With