Sunday, December 27th

Learning Haskell

During Christmas break my project is to learn me Haskell. I've procured the book Real World Haskell and am about halfway through it. RWH is a pretty good book but it could do with another editing pass. For example, the '$' operator is used without ever having been explained.

And I found an interesting paper The Genuine Sieve of Eratosthenes which shows that a common Functional Programming example isn't really true to Eratosthenes' original algorithm.

One form of the standard FP Sieve goes like this (in Haskell):

primes = sieve [2..]
sieve (p : xs) = p : sieve [x | x <− xs, x ‘mod‘ p > 0]


The paper then goes on to show several FP forms that are true to the original algorithm.

I thought this would be a good learning exercise, so I coded up my own version of the Sieve in purely functional Haskell:


-- Find primes up to n
primes :: Int -> [Int]
primes n = drop 1 $ takeWhile (< n ) $ filter (/=0) $ sieve [0..] 1 $ ceiling $ sqrt $ fromIntegral n

-- Mark composites up to n^2. All composite numbers will be set to zero
sieve :: [Int] -> Int -> Int-> [Int]
sieve a n limit | n >= limit = a
sieve a n limit = sieve (a `land` (mask n)) (next_candidate) limit
-- Next candidate will be next non-zero element in the list
where next_candidate = fromJust (find (>n) a)

-- Generate a list of Booleans. False at multiples of n, starting at n^2
mask :: Int -> [Bool]
mask 1 = True : mask 1
mask n = (replicate (n^2) True) ++ (rep n)
where rep n = False:(replicate (n-1) True) ++ (rep n)

-- Merge together a boolean list and an integer list
land :: [Int] -> [Bool] -> [Int]
land (a:as) (b:bs) = (if b then a else 0) : land as bs
land _ _ = []


This Haskell code is some of my first, so please be gentle with your comments.

The interesting thing here is the use of Haskell's lazy evaluation. Here, 'lazy' means an expression isn't evaluated until it's needed. For example, the Haskell construct [1..] generates an infinite list of integers. If you attempt to use the entire infinite list to, say, find all the odd integers, Haskell will happily comply: odd [1..] and will take an infinite amount of time.

If, however, you just take a finite set of the result: take 10 odd [1..] then Haskell will return only the first 10 odd integers. This is a powerful concept.


Jim on 12.27.09 @ 10:13 AM ET [link]


Saturday, December 26th

Rosetta Code


Rosetta Code is an interesting resource for programming language comparisons. The site poses programming problems and users contribute their solutions in their language of choice. The result is a nice side-by-side comparison of languages in various problem domains.
Jim on 12.26.09 @ 12:08 PM ET [link]


Let me Google that for ya!

Has someone asked you a dumb question that they could have easily answered themselves with a visit to the planet's most popular search engine? Something like "What's the capital of Venezuela?".

Well, now you can thumb your nose in their face while providing the answer: What's the capital of Venezuela?.

All thanks to Let Me Google That for You. Simply type your dumb question into the lmgtfy.com page and it will generate a URL that you can send to your annoying friend. This URL will start a little animation then show the actual Google results.
Jim on 12.26.09 @ 10:59 AM ET [link]


Wednesday, December 23rd

Barb's Big Adventure

For Barb's 0x32'th birthday I took her Indoor Skydiving at SkyVenture in Nashua, NH. It was a complete surprise to her, but luckily she loved it. I didn't tell her where we were going - we just got in the car and arrived at Nashua.

The kids and I took some videos, and their staff photographer took some still photos. I The glass walls prevented me from getting good stills, but the staff photographer had glass-free access to the chamber.

I booked 10 minutes of flight time, which doesn't sound like a lot but is pretty much the limit a neophyte can handle. A colleague of mine did 5 minutes and said he couldn't handle any more, mostly due to the arched position you need to be in.

Barb took the whole 10 minute flight time in 5 two-minute chunks. An instructor was in the chamber with her the whole time, often positioning her arms and legs, and occasionally taking her up 15 or 20 feet into the top of the chamber.

At the end Barb felt exhilarated but was sore for several days. She'd always wanted to skydive, but didn't want to do the "jumping out of an airplane" part. Maybe someday I can give it a shot.
Jim on 12.23.09 @ 09:42 AM ET [link]



Email: jim@jimandbarb.DELETETHISPART.net
(please remove the DELETETHISPART before sending me mail!)