# [plt-scheme] Teaching Scheme

And, for good measure, a Haskell version of the same program:
doorOpen :: Int -> Bool
doorOpen door = doh door door
doh :: Int -> Int -> Bool
doh door 0 = True
doh door pass =
if (door `rem` (pass+1)) == pass
then not (doh door (pass-1))
else doh door (pass-1)
doors :: [Bool]
doors = [doorOpen n | n <- [0..]]
printDoor :: (Int,Bool) -> IO ()
printDoor (door,open) =
putStrLn ("Door #" ++ (show door) ++ " is " ++
if open then "open.." else "closed..")
printUpTo :: Int -> IO ()
printUpTo n =
mapM_ printDoor (zip [0..(n-1)] doors)
You can call printUpTo 50, then printUpTo 20, then printUpTo 300, and it'll compute the right answer for each of the numbers 0..299 only once -- not the numbers 0..19 three times, 20..49 twice, and 50..299 once each.
Stephen Bloch
sbloch at adelphi.edu