This just too cool to pass up:

~:$ cat count.hs
fix f      = f (fix f) -- return fixed point
-- prepend 1 to the result of adding 1 to each value
g f        = 1 : map (+1) f
counting   = fix g    -- return the "list" that is fixed
~:$ hugs
__ __ __ __ ____ ___ _________________________________________ || || || || || || ||__ Hugs 98: Based on the Haskell 98 standard
||___|| ||__|| ||__||  __||     Copyright (c) 1994-2005
||---||         ___||           World Wide Web: http://haskell.org/hugs
||   ||                         Report bugs to: [email protected]
|| || Version: March 2005 _________________________________________

Haskell 98 mode: Restart with command line option -98 to enable extensions

Type :? for help
Hugs.Base> :load count.hs
Main> take 5 counting
[1,2,3,4,5]
Main> take 10 counting
[1,2,3,4,5,6,7,8,9,10]
Main> [Leaving Hugs]
~:$


The three line program described above may be paraphrased as follows:

1. Define a function f and return a fixed point of (i.e. if fix f = x, then f(x) = x). 2. Define g to the function that takes f, adds 1 to every element of the list it returns and prepend 1 to that list
3. Call that fixed point counting
4. Now, suppose you take the "infinite" list [1, 2, 3 ...] and add 1 to each list element. The result is, of course, [2, 3, 4 ..]. If you put a 1 in front of that list, you get [1, 2, 3 ...] 5. So, the "list" returned by taking a fixed point of g is "counting to infinity"

Finally, I use the built-in take to display the first 5 or 10 elements of the list to demonstrate that it really does "count to infinity".

===
Gregory Woodhouse
[EMAIL PROTECTED]

"Good acts are like good poems. One may easily get their drift, but they are not rationally understood."
--Albert Einstein





-------------------------------------------------------
This SF.Net email is sponsored by the JBoss Inc.  Get Certified Today
Register for a JBoss Training Course.  Free Certification Exam
for All Training Attendees Through End of 2005. For more info visit:
http://ads.osdn.com/?ad_id=7628&alloc_id=16845&op=click
_______________________________________________
Hardhats-members mailing list
[email protected]
https://lists.sourceforge.net/lists/listinfo/hardhats-members

Reply via email to