Clarifying $ vs parentheses

Previous Topic Next Topic
 
classic Classic list List threaded Threaded
3 messages Options
Reply | Threaded
Open this post in threaded view
|

Clarifying $ vs parentheses

Josh Friedlander
I understand that in general $ is a) right-associative and b) lowest-priority. But if so shouldn't these two be roughly the same?

λ take (succ 10) $ cycle "hello world"
"hello world"

But not this?
λ take $ succ 10 $ cycle "hello world"

<interactive>:20:8: error:
    • No instance for (Enum ([Char] -> Int))
        arising from a use of ‘succ’
        (maybe you haven't applied a function to enough arguments?)
    • In the expression: succ 10
      In the second argument of ‘($)’, namely
        ‘succ 10 $ cycle "hello world"’
      In the expression: take $ succ 10 $ cycle "hello world"

<interactive>:20:13: error:
    • No instance for (Num ([Char] -> Int))
        arising from the literal ‘10’
        (maybe you haven't applied a function to enough arguments?)
    • In the first argument of ‘succ’, namely ‘10’
      In the expression: succ 10
      In the second argument of ‘($)’, namely
        ‘succ 10 $ cycle "hello world"’

_______________________________________________
Beginners mailing list
[hidden email]
http://mail.haskell.org/cgi-bin/mailman/listinfo/beginners
Reply | Threaded
Open this post in threaded view
|

Re: Clarifying $ vs parentheses

Bob Ippolito
Because the second one is:

  take (succ 10 (cycle “hello world”))

On Thu, Aug 20, 2020 at 12:03 Josh Friedlander <[hidden email]> wrote:
I understand that in general $ is a) right-associative and b) lowest-priority. But if so shouldn't these two be roughly the same?

λ take (succ 10) $ cycle "hello world"
"hello world"

But not this?
λ take $ succ 10 $ cycle "hello world"

<interactive>:20:8: error:
    • No instance for (Enum ([Char] -> Int))
        arising from a use of ‘succ’
        (maybe you haven't applied a function to enough arguments?)
    • In the expression: succ 10
      In the second argument of ‘($)’, namely
        ‘succ 10 $ cycle "hello world"’
      In the expression: take $ succ 10 $ cycle "hello world"

<interactive>:20:13: error:
    • No instance for (Num ([Char] -> Int))
        arising from the literal ‘10’
        (maybe you haven't applied a function to enough arguments?)
    • In the first argument of ‘succ’, namely ‘10’
      In the expression: succ 10
      In the second argument of ‘($)’, namely
        ‘succ 10 $ cycle "hello world"’


_______________________________________________

Beginners mailing list

[hidden email]

http://mail.haskell.org/cgi-bin/mailman/listinfo/beginners


_______________________________________________
Beginners mailing list
[hidden email]
http://mail.haskell.org/cgi-bin/mailman/listinfo/beginners
Reply | Threaded
Open this post in threaded view
|

Re: Clarifying $ vs parentheses

Francesco Ariis
In reply to this post by Josh Friedlander
Hello Josh,

il 20 agosto 2020 alle 22:02 josh friedlander ha scritto:

> i understand that in general $ is a) right-associative and b)
> lowest-priority. but if so shouldn't these two be roughly the same?
>
> λ take (succ 10) $ cycle "hello world"
> "hello world"
>
> But not this?
> λ take $ succ 10 $ cycle "hello world"
>
> […]

    λ> :info ($)
    ($) :: (a -> b) -> a -> b       -- Defined in ‘GHC.Base’
    infixr 0 $

So, since `$` is right associative, the expression

    take $ succ 10 $ cycle "hello world"

becomes

    take (succ 10 (cycle "hello world"))

`cycle "hello world"` makes sense, `succ 10` makes sense,
`succ 10 anotherArgument` does not.
Even `take someStuff` is probably not what you want, since take is usually
invoked with two arguments.

A useful intuition when you see ($) is `it will evaluate everything on
the right of it first`. This way, `not $ xx yy zz` looks right,
`take $ xx aa yy qq` less so.

Does this help?
—F
_______________________________________________
Beginners mailing list
[hidden email]
http://mail.haskell.org/cgi-bin/mailman/listinfo/beginners