POV-Ray : Newsgroups : povray.off-topic : I found this interesting : Re: [Caution: long monad ramble] Server Time
1 Oct 2024 15:22:28 EDT (-0400)
  Re: [Caution: long monad ramble]  
From: Orchid XP v8
Date: 12 Apr 2008 02:50:59
Message: <48005bd3@news.povray.org>
Darren New wrote:
> Orchid XP v8 wrote:
>>> figure out how to make while() into a function that takes two lambdas. 
>>
>> There's takeWhile, dropWhile and span which chop up segments of lists. 
>> There's "unfoldr" which builds lists. There's "until", which repeats a 
>> computation until a condition holds, and returns the final result. And 
>> so on...
> 
> Erlang has all that stuff. They aren't "while" loops, tho, they're "for" 
> loops. No way to stop foldr() early, no way to have it get its stuff by 
> (say) reading lines from a file instead of a list, etc.

foldr is a for-loop, but unfoldr *is* a while-loop, as are the others I 
quoted.

> No way to say "while you haven't gotten to a directory entry that starts 
> with the letter 'T' in /tmp, ...."

do
   fs0 <- getDirectoryContents "/tmp"
   let fs1 = sort fs0
   let fs2 = takeWhile (\f -> head f /= 'T') fs1
   mapM (stuff) fs2

>>   return 5 >>= print
> 
> Odd name. Not sure what that means. :-)

Definitely an odd choice of name! "insert" or something would at least 
make more sense. (That's what it does after all - insert a pure value 
into a monadic wrapper...)

>> The "fail" method takes a text message 
>> and aborts the computation. For Maybe or lists, the message is 
>> ignored, but for the IO monad it throws an exception. 
> 
> And Erlang crashes the process out, which sends "I crashed" messages to 
> anyone "linked" to that process, which in turn either crashes them out 
> or just queues a message saying they crashed.

Haskell has a function called "error" which takes a message and then 
throws an exception containing that message. Unless exceptions are 
caught somewhere [which can only happen in the IO monad], the default 
behaviour is for the program to exit and write a message to stdout.

"fail" is different to "error" in that it is specific to a particular 
monad. So for the IO monad, fail = error. But for Maybe, fail just 
returns Nothing. And for some other user-defined monad, it could do 
something else instead.

factors k = do
   x <- [1..10]
   y <- [1..10]
   if x*y == k then return (x,y) else fail "not a factor"

That does the same as the other example, while being [debatably] clearer.

>>> Wow. You have your own newsgroup. :-)
>> Where *have* you been dear boy? ;-)
> 
> Well, I remember it being created as a joke, but I never subscribed. :-)

Better yet: As far as anybody knows, messages never expire! >:-)

-- 
http://blog.orphi.me.uk/
http://www.zazzle.com/MathematicalOrchid*


Post a reply to this message

Copyright 2003-2023 Persistence of Vision Raytracer Pty. Ltd.