
Hi, Can someone please highlight how '$' is making the difference in the execution of this function. With $ in place the function runs fine but without $ for large input values the function overflows the stack. -------- s mp 1 = 4 s mp n = ((s mp $ n-1)^2-2) `mod` mp -------- I cannot understand the difference between (s mp $ n-1) and (s mp n-1) Thanks, Shishir Srivastava +44 (0) 750 127 5019

s mp $ n - 1 parses as s mp (n - 1) s mp n - 1 parses as (s mp n) - 1 On Sun, Jun 7, 2015 at 3:09 PM, Shishir Srivastava < shishir.srivastava@gmail.com> wrote:
Hi,
Can someone please highlight how '$' is making the difference in the execution of this function. With $ in place the function runs fine but without $ for large input values the function overflows the stack.
-------- s mp 1 = 4 s mp n = ((s mp $ n-1)^2-2) `mod` mp --------
I cannot understand the difference between (s mp $ n-1) and (s mp n-1)
Thanks, Shishir Srivastava +44 (0) 750 127 5019
_______________________________________________ Beginners mailing list Beginners@haskell.org http://mail.haskell.org/cgi-bin/mailman/listinfo/beginners

Mike Meyer:
s mp $ n - 1 parses as s mp (n - 1) s mp n - 1 parses as (s mp n) - 1
Hi, I think it is misleading to say that $ "parses" to something. ($) is an infix operator with type: ($) :: (a -> b) -> a -> b -- Defined in ‘GHC.Base’ infixr 0 $ So what it does is it takes a function on the left hand side and applies it to the second argument on the right hand side. The trick is it has the lowest possible precedence value (0) so everything on the right hand side will be evaluated before applying the function on the left. This is useful as a convenience if you want to avoid writing too many parentheses. Martin

On Jun 8, 2015 05:01, "Martin Vlk"
Mike Meyer:
s mp $ n - 1 parses as s mp (n - 1) s mp n - 1 parses as (s mp n) - 1
Hi, I think it is misleading to say that $ "parses" to something.
Which is why I didn't say such a thing. The two sentences show how the expressions he was having trouble with are parsed.
($) is an infix operator with type:
($) :: (a -> b) -> a -> b -- Defined in ‘GHC.Base’ infixr 0 $
So what it does is it takes a function on the left hand side and applies it to the second argument on the right hand side. The trick is it has the lowest possible precedence value (0) so everything on the right hand side will be evaluated before applying the function on the left.
Which explains why the expression containing a $ parses as it does, which is also useful information.

The point is that there is no parsing happening here. Just evaluation.
On Mon, Jun 8, 2015 at 3:20 AM Mike Meyer
On Jun 8, 2015 05:01, "Martin Vlk"
wrote: Mike Meyer:
s mp $ n - 1 parses as s mp (n - 1) s mp n - 1 parses as (s mp n) - 1
Hi, I think it is misleading to say that $ "parses" to something.
Which is why I didn't say such a thing. The two sentences show how the expressions he was having trouble with are parsed.
($) is an infix operator with type:
($) :: (a -> b) -> a -> b -- Defined in ‘GHC.Base’ infixr 0 $
So what it does is it takes a function on the left hand side and applies it to the second argument on the right hand side. The trick is it has the lowest possible precedence value (0) so everything on the right hand side will be evaluated before applying the function on the left.
Which explains why the expression containing a $ parses as it does, which is also useful information. _______________________________________________ Beginners mailing list Beginners@haskell.org http://mail.haskell.org/cgi-bin/mailman/listinfo/beginners

Are you saying that GHC doesn't have a haskell parser?
On Mon, Jun 8, 2015 at 12:06 PM, Rein Henrichs
The point is that there is no parsing happening here. Just evaluation.
On Mon, Jun 8, 2015 at 3:20 AM Mike Meyer
wrote: On Jun 8, 2015 05:01, "Martin Vlk"
wrote: Mike Meyer:
s mp $ n - 1 parses as s mp (n - 1) s mp n - 1 parses as (s mp n) - 1
Hi, I think it is misleading to say that $ "parses" to something.
Which is why I didn't say such a thing. The two sentences show how the expressions he was having trouble with are parsed.
($) is an infix operator with type:
($) :: (a -> b) -> a -> b -- Defined in ‘GHC.Base’ infixr 0 $
So what it does is it takes a function on the left hand side and applies it to the second argument on the right hand side. The trick is it has the lowest possible precedence value (0) so everything on the right hand side will be evaluated before applying the function on the left.
Which explains why the expression containing a $ parses as it does, which is also useful information. _______________________________________________ Beginners mailing list Beginners@haskell.org http://mail.haskell.org/cgi-bin/mailman/listinfo/beginners
_______________________________________________ Beginners mailing list Beginners@haskell.org http://mail.haskell.org/cgi-bin/mailman/listinfo/beginners

Of course not. What a silly thing to suggest. I'm saying that parsing is
not relevant to the behavior of ($). The parser can't tell the *semantic*
difference between $ and any other operator.
On Mon, Jun 8, 2015 at 11:02 AM Mike Meyer
Are you saying that GHC doesn't have a haskell parser?
On Mon, Jun 8, 2015 at 12:06 PM, Rein Henrichs
wrote: The point is that there is no parsing happening here. Just evaluation.
On Mon, Jun 8, 2015 at 3:20 AM Mike Meyer
wrote: On Jun 8, 2015 05:01, "Martin Vlk"
wrote: Mike Meyer:
s mp $ n - 1 parses as s mp (n - 1) s mp n - 1 parses as (s mp n) - 1
Hi, I think it is misleading to say that $ "parses" to something.
Which is why I didn't say such a thing. The two sentences show how the expressions he was having trouble with are parsed.
($) is an infix operator with type:
($) :: (a -> b) -> a -> b -- Defined in ‘GHC.Base’ infixr 0 $
So what it does is it takes a function on the left hand side and applies it to the second argument on the right hand side. The trick is it has the lowest possible precedence value (0) so everything on the right hand side will be evaluated before applying the function on the left.
Which explains why the expression containing a $ parses as it does, which is also useful information. _______________________________________________ Beginners mailing list Beginners@haskell.org http://mail.haskell.org/cgi-bin/mailman/listinfo/beginners
_______________________________________________ Beginners mailing list Beginners@haskell.org http://mail.haskell.org/cgi-bin/mailman/listinfo/beginners
_______________________________________________ Beginners mailing list Beginners@haskell.org http://mail.haskell.org/cgi-bin/mailman/listinfo/beginners

The OP's problem wasn't with the semantics of $. The OP's problem was that
the two expression had different parse trees that gave him different
results when evaluated. So not only was there parsing happening here, but
it was the root cause of his confusion.
Given that the output of GHC's parser was the cause of the confusion, I
couldn't think of anything else you might have meant that made sense in
context.
On Mon, Jun 8, 2015 at 2:06 PM, Rein Henrichs
Of course not. What a silly thing to suggest. I'm saying that parsing is not relevant to the behavior of ($). The parser can't tell the *semantic* difference between $ and any other operator. On Mon, Jun 8, 2015 at 11:02 AM Mike Meyer
wrote: Are you saying that GHC doesn't have a haskell parser? On Mon, Jun 8, 2015 at 12:06 PM, Rein Henrichs
wrote: The point is that there is no parsing happening here. Just evaluation.

You said,
s mp $ n - 1 parses as s mp (n - 1) s mp n - 1 parses as (s mp n) - 1
This is not how they parse. This is how they *evaluate*. A description of
how they *parse* would have included the ($) token in a parse tree.
This mistake is what lead to our confusion and your rudeness hasn't helped
to clarify things. Rather than pretending that we're idiots for not
understanding what you meant, it would have been better if you had just
said what you meant.
On Mon, Jun 8, 2015 at 12:18 PM Mike Meyer
The OP's problem wasn't with the semantics of $. The OP's problem was that the two expression had different parse trees that gave him different results when evaluated. So not only was there parsing happening here, but it was the root cause of his confusion.
Given that the output of GHC's parser was the cause of the confusion, I couldn't think of anything else you might have meant that made sense in context.
On Mon, Jun 8, 2015 at 2:06 PM, Rein Henrichs
wrote: Of course not. What a silly thing to suggest. I'm saying that parsing is not relevant to the behavior of ($). The parser can't tell the *semantic* difference between $ and any other operator. On Mon, Jun 8, 2015 at 11:02 AM Mike Meyer
wrote: Are you saying that GHC doesn't have a haskell parser? On Mon, Jun 8, 2015 at 12:06 PM, Rein Henrichs
wrote: The point is that there is no parsing happening here. Just evaluation.
_______________________________________________ Beginners mailing list Beginners@haskell.org http://mail.haskell.org/cgi-bin/mailman/listinfo/beginners

My apologies if I came off as rude. That was not my intent.
On Mon, Jun 8, 2015 at 2:28 PM, Rein Henrichs
You said,
s mp $ n - 1 parses as s mp (n - 1) s mp n - 1 parses as (s mp n) - 1
This is not how they parse. This is how they *evaluate*. A description of how they *parse* would have included the ($) token in a parse tree.
This mistake is what lead to our confusion and your rudeness hasn't helped to clarify things. Rather than pretending that we're idiots for not understanding what you meant, it would have been better if you had just said what you meant.
On Mon, Jun 8, 2015 at 12:18 PM Mike Meyer
wrote: The OP's problem wasn't with the semantics of $. The OP's problem was that the two expression had different parse trees that gave him different results when evaluated. So not only was there parsing happening here, but it was the root cause of his confusion.
Given that the output of GHC's parser was the cause of the confusion, I couldn't think of anything else you might have meant that made sense in context.
On Mon, Jun 8, 2015 at 2:06 PM, Rein Henrichs
wrote: Of course not. What a silly thing to suggest. I'm saying that parsing is not relevant to the behavior of ($). The parser can't tell the *semantic* difference between $ and any other operator. On Mon, Jun 8, 2015 at 11:02 AM Mike Meyer
wrote: Are you saying that GHC doesn't have a haskell parser? On Mon, Jun 8, 2015 at 12:06 PM, Rein Henrichs
wrote:
The point is that there is no parsing happening here. Just evaluation.
_______________________________________________ Beginners mailing list Beginners@haskell.org http://mail.haskell.org/cgi-bin/mailman/listinfo/beginners
_______________________________________________ Beginners mailing list Beginners@haskell.org http://mail.haskell.org/cgi-bin/mailman/listinfo/beginners

I can't imagine that you actually believed in good faith that I would be
confused about whether a Haskell compiler would contain a Haskell parser.
On Mon, Jun 8, 2015 at 12:31 PM Mike Meyer
My apologies if I came off as rude. That was not my intent.
On Mon, Jun 8, 2015 at 2:28 PM, Rein Henrichs
wrote: You said,
s mp $ n - 1 parses as s mp (n - 1) s mp n - 1 parses as (s mp n) - 1
This is not how they parse. This is how they *evaluate*. A description of how they *parse* would have included the ($) token in a parse tree.
This mistake is what lead to our confusion and your rudeness hasn't helped to clarify things. Rather than pretending that we're idiots for not understanding what you meant, it would have been better if you had just said what you meant.
On Mon, Jun 8, 2015 at 12:18 PM Mike Meyer
wrote: The OP's problem wasn't with the semantics of $. The OP's problem was that the two expression had different parse trees that gave him different results when evaluated. So not only was there parsing happening here, but it was the root cause of his confusion.
Given that the output of GHC's parser was the cause of the confusion, I couldn't think of anything else you might have meant that made sense in context.
On Mon, Jun 8, 2015 at 2:06 PM, Rein Henrichs
wrote: Of course not. What a silly thing to suggest. I'm saying that parsing is not relevant to the behavior of ($). The parser can't tell the *semantic* difference between $ and any other operator. On Mon, Jun 8, 2015 at 11:02 AM Mike Meyer
wrote: Are you saying that GHC doesn't have a haskell parser? On Mon, Jun 8, 2015 at 12:06 PM, Rein Henrichs < rein.henrichs@gmail.com> wrote:
The point is that there is no parsing happening here. Just evaluation.
_______________________________________________ Beginners mailing list Beginners@haskell.org http://mail.haskell.org/cgi-bin/mailman/listinfo/beginners
_______________________________________________ Beginners mailing list Beginners@haskell.org http://mail.haskell.org/cgi-bin/mailman/listinfo/beginners
_______________________________________________ Beginners mailing list Beginners@haskell.org http://mail.haskell.org/cgi-bin/mailman/listinfo/beginners
participants (5)
-
Martin Vlk
-
Mike Meyer
-
Rein Henrichs
-
Shishir Srivastava
-
Sumit Sahrawat, Maths & Computing, IIT (BHU)