NOTE: use Perl; is on undef hiatus. You can read content, but you can't post it. More info will be forthcoming forthcomingly.
All the Perl that's Practical to Extract and Report
Stories, comments, journals, and other submissions on use Perl; are Copyright 1998-2006, their respective owners.
Ouch. Head hurt. Need beer. (Score:2)
Can you give an example of how this might make some code cleaner or more correct?
Reply to This
Re: (Score:1)
I had to chuckle at your subject. I certainly know the feeling. The upshot is that you can use this to write recursive anonymous closures (avoiding all side-effects).
I did this as an excercise. I am tiptoeing into the functional world proper lately, but my stance to it remains ambivalent. Perl isn’t quite the most beautiful language, but I find it a whoooole lot easier to read than any Lisp. I doubt that will ever change, irrespective of practice – I find your lack of syntax disturbing. This
Re: (Score:1)
Re: (Score:1)
Ah yes, d’uh.
But didn’t you mean your do {} block to return $tmp rather than $f? $f is the weak reference used inside the function, while $tmp is the strong reference you pass back out to the surrounding expression, no?
Re: (Score:1)
Re: (Score:1)
Ah, when you copy a weak reference, the new reference is strong. Learn something new every day…
In any case, the necessary code is rather unwieldy – enough so that I’d rather just use a Y combinator…
Re: (Score:1)
Not precisely... Weaken works on the variable, not the value. This should work:
Re: (Score:1)
Re: (Score:1)
I have used closures + fixed point combintators several times for traversal functions when a named function just was not appropriate. I didn't know about the Y combinator at the time, but if I had, then I would have been able to clean up my code by using it to package up the closure+fixed-point combo into a single CODE ref.
But aside from that fringe usage, it is not really that useful in day to day life. In languages like Scheme and Haskell where the purity is highly valued it tends to come in handy, b