From 4e987026148fe65c323afbc93cd560c07bf06b3f Mon Sep 17 00:00:00 2001 From: Yale AI Dept Date: Wed, 14 Jul 1993 13:08:00 -0500 Subject: Import to github. --- progs/demo/pascal.hs | 24 ++++++++++++++++++++++++ 1 file changed, 24 insertions(+) create mode 100644 progs/demo/pascal.hs (limited to 'progs/demo/pascal.hs') diff --git a/progs/demo/pascal.hs b/progs/demo/pascal.hs new file mode 100644 index 0000000..a26e9c9 --- /dev/null +++ b/progs/demo/pascal.hs @@ -0,0 +1,24 @@ +{- This uses lazy evaluation to define Pascals triangle -} + +module Main where + +pascal :: [[Int]] +pascal = [1] : [[x+y | (x,y) <- zip ([0]++r) (r++[0])] | r <- pascal] + +tab :: Int -> ShowS +tab 0 = id +tab (n+1) = showChar ' ' . tab n + +showRow :: [Int] -> ShowS +showRow [] = showChar '\n' +showRow (n:ns) = shows n . showChar ' ' . showRow ns + +showTriangle 1 (t:_) = showRow t +showTriangle (n+1) (t:ts) = tab n . showRow t . showTriangle n ts + +main = appendChan stdout "Number of rows: " abort $ + readChan stdin abort $ \input -> + appendChan stdout + (showTriangle (read (head (lines input))) pascal "") + abort done + -- cgit v1.2.3