Re: 'CALL-ME' Math problem?
On 02/03/2021 09:12, ToddAndMargo via perl6-users wrote: Math problem: x = 60÷5(7−5) raku -e 'say 60÷5(7−5)' No such method 'CALL-ME' for invocant of type 'Int' in block at -e line 1 Seems raku does not like the (). How do I fix this and maintain the flow and look of the equation? -T The correct answer is 24 The correct answer can be found with the following code. class SuperInt { has $.left-factor is rw; has $.right-factor is rw; method new(\l, \r) { my \SELF = self.CREATE; SELF.left-factor = l; SELF.right-factor = r; SELF } } multi sub infix:<÷>(Numeric:D \l, SuperInt:D \r) { l ÷ r.left-factor * r.right-factor } Int.^add_method('CALL-ME', my method (\SELF: \v) { SuperInt.new(SELF, v) }); Int.^compose; say 60÷5(7−5); Thought, I do have the hunch that this might break with a slightly more complex examle. Have -Ofun gfldex
Re: 'CALL-ME' Math problem?
Not in this case. The error happens at run-time. Syntactically the expression is a valid one because `5(...)` is interpreted as an invocation. Raku implements invocation protocol a part of which is method 'CALL-ME' defined on a class: class Foo { method CALL-ME(|c) { say "Foo invoked with {c.raku}" } } Foo(42, :foo(13)); Foo.new().(); Note that the protocol works for both a type object and its instantiation. This is what is basically happens in the case of `5(...)`: it falls back to `Int` and tries to invoke `CALL-ME` on it. One could say that it makes no sense, but an example with adding a fallback method has been already provided here. And here is another one follows: my $foo = 5 but role { method CALL-ME(|c) { say "Int with ", c.raku } }; $foo(13); The currently produced error, unfortunately, is confusing for beginners. But as soon as one gains more experience with Raku it makes clear sense instantly. Best regards, Vadim Belman > On Mar 2, 2021, at 9:26 AM, Parrot Raiser <1parr...@gmail.com> wrote: > >> Doing so would, of course, be a very bad idea. But still, you _could_. > > Something of an understatement, I think. :-)* > > Seriously, this made me wonder if inscrutable error messages might be > clarifed by a (reverse) trace of the last few steps in parsing. That > would show you what the compiler thought ir was doing. > > Would that be a) reasonably practical to implement, and b) > sufficiently useful to justify the effort? >
Re: 'CALL-ME' Math problem?
On Tue, Mar 2, 2021 at 4:08 PM Matthew Stuckwisch wrote: > But why do that when you can add a CALL-ME to the number classes that does > multiplication? > > Int.^add_fallback( > {$^i.defined && $^m eq 'CALL-ME'}, > -> $, $ { * * * } > ); > > say 5(4); # 20 > say 5(5)² # 625… oops! but say 5(5²) # 125 ok > On Tue, Mar 2, 2021, 09:08 Daniel Sockwell > wrote: > >> Kevin Pye wrote: >> > Just because mathematics allows an implied multiplication doesn't mean >> Raku does -- in fact I can't >> > think of any programming language which does. >> >> As a (potentially) interesting side note, while Raku doesn't provide >> implied multiplication, it _is_ >> one of the few programming languages that would let you implement >> something very similar yourself: >>sub infix:«\c[INVISIBLE TIMES]» { $^a × $^b } >> >> This would let you write `say 60÷5(7−5)` (with an invisible character >> between the `5` and the `(` ) >> and get the expected result. >> >> Doing so would, of course, be a very bad idea. But still, you _could_. >> >> Source: >> >> https://perl6advent.wordpress.com/2017/12/01/the-grinch-of-perl-6-a-practical-guide-to-ruining-christmas/ >> > -- Fernando Santagata
Re: 'CALL-ME' Math problem?
But why do that when you can add a CALL-ME to the number classes that does multiplication? Int.^add_fallback( {$^i.defined && $^m eq 'CALL-ME'}, -> $, $ { * * * } ); say 5(4); # 20 On Tue, Mar 2, 2021, 09:08 Daniel Sockwell wrote: > Kevin Pye wrote: > > Just because mathematics allows an implied multiplication doesn't mean > Raku does -- in fact I can't > > think of any programming language which does. > > As a (potentially) interesting side note, while Raku doesn't provide > implied multiplication, it _is_ > one of the few programming languages that would let you implement > something very similar yourself: >sub infix:«\c[INVISIBLE TIMES]» { $^a × $^b } > > This would let you write `say 60÷5(7−5)` (with an invisible character > between the `5` and the `(` ) > and get the expected result. > > Doing so would, of course, be a very bad idea. But still, you _could_. > > Source: > > https://perl6advent.wordpress.com/2017/12/01/the-grinch-of-perl-6-a-practical-guide-to-ruining-christmas/ >
Re: 'CALL-ME' Math problem?
> Doing so would, of course, be a very bad idea. But still, you _could_. Something of an understatement, I think. :-)* Seriously, this made me wonder if inscrutable error messages might be clarifed by a (reverse) trace of the last few steps in parsing. That would show you what the compiler thought ir was doing. Would that be a) reasonably practical to implement, and b) sufficiently useful to justify the effort?
Re: 'CALL-ME' Math problem?
Kevin Pye wrote: > Just because mathematics allows an implied multiplication doesn't mean Raku > does -- in fact I can't > think of any programming language which does. As a (potentially) interesting side note, while Raku doesn't provide implied multiplication, it _is_ one of the few programming languages that would let you implement something very similar yourself: sub infix:«\c[INVISIBLE TIMES]» { $^a × $^b } This would let you write `say 60÷5(7−5)` (with an invisible character between the `5` and the `(` ) and get the expected result. Doing so would, of course, be a very bad idea. But still, you _could_. Source: https://perl6advent.wordpress.com/2017/12/01/the-grinch-of-perl-6-a-practical-guide-to-ruining-christmas/
Re: 'CALL-ME' Math problem?
>> >> Hi All, >> >> Math problem: >> x = 60÷5(7−5) >> >> raku -e 'say 60÷5(7−5)' >> No such method 'CALL-ME' for invocant of type 'Int' >> in block at -e line 1 >> >> Seems raku does not like the (). How do I fix this >> and maintain the flow and look of the equation? >> >> -T >> >> The correct answer is 24 >> >> On 3/2/21 12:17 AM, Richard Hainsworth wrote: no operator between '5' & '(' That is mathematical shorthand. Introducing such shorthand into a programming language leads to ambiguities. I'm not aware of any formal system that implies a '*' in such a situation. Richard On 02/03/2021 08:12, ToddAndMargo via perl6-users wrote: $ raku -e 'say 60÷5*(7−5)' 24 :-)
Re: 'CALL-ME' Math problem?
On Tue, 2 Mar 2021, 08:13 ToddAndMargo via perl6-users, mailto:perl6-us...@perl.org>> wrote: Hi All, Math problem: x = 60÷5(7−5) raku -e 'say 60÷5(7−5)' No such method 'CALL-ME' for invocant of type 'Int' in block at -e line 1 Seems raku does not like the (). How do I fix this and maintain the flow and look of the equation? -T The correct answer is 24 On 3/2/21 12:16 AM, Simon Proctor wrote: You need to put in the multiplication sign. Hi Simon and Richard, Now I feel dumb! $ raku -e 'say 60÷5*(7−5)' 24 Thank you! The formula is an example of left to right precedence. -T
Re: 'CALL-ME' Math problem?
Just because mathematics allows an implied multiplication doesn't mean Raku does -- in fact I can't think of any programming language which does. You need to stick an explicit multiplication operator ('*' or '×') between the 5 and the left parenthesis. The reason for the error message is that adding parentheses after something is interpreted as trying to invoke a function. Callable objects such as functions have a CALL-ME method which is what is invoked to call the function. An integer lacks such a method, since it isn't callable. On Tue, 2 Mar 2021 at 19:13, ToddAndMargo via perl6-users < perl6-us...@perl.org> wrote: > > Hi All, > > Math problem: > x = 60÷5(7−5) > > raku -e 'say 60÷5(7−5)' > No such method 'CALL-ME' for invocant of type 'Int' >in block at -e line 1 > > Seems raku does not like the (). How do I fix this > and maintain the flow and look of the equation? > > -T > > The correct answer is 24 > > >
'CALL-ME' Math problem?
Hi All, Math problem: x = 60÷5(7−5) raku -e 'say 60÷5(7−5)' No such method 'CALL-ME' for invocant of type 'Int' in block at -e line 1 Seems raku does not like the (). How do I fix this and maintain the flow and look of the equation? -T The correct answer is 24