Haskell Examples Using Infinite Lists

  1. Fibonacci Numbers
  2. Padovan Numbers
  3. Tribonacci Numbers
  4. Lucas Numbers
  5. Perrin Numbers
  6. Generalized Two Term Recurrances
  7. Sofo Numbers

     More Haskell Examples

  1. Phone Number Word Generator
  2. Continued Fractions
  3. Lindenmayer System


Fibonacci in Haskell -  F(n) = F(n-1) + F(n-2)

module Main where
f = 1 : 1 : zipWith ( + ) f (tail f)
f_print n = print( show n ++ "th Fibonacci number is " ++ show (f!!n))

main = f_print  20

<Back>

Padovan in Haskell  -  F(n) = F(n-2) + F(n-3)

module Main where
f = 1 : 1 : 1 : (zipWith ( + ) f  ( tail f))

f_print n = print( show n ++ "th Padovan number is " ++ show ( f!!n) )
main = f_print 20

<back>

Tribonacci in Haskell - F(n) = F(n-1) + F(n-2) + F(n-3)

module Main where

f = 0 : 1 : 1 : zipWith ( + ) f  ( zipWith ( + ) ( tail f) (tail ( tail f )))
f_print n = print( show n ++ "th Tribonacci number is " ++ show (f!!n))

main = f_print 20
 
       <back>

Lucas Numbers in Haskell - F(n) = F(n-1) + F(n-2)

module Main where
f = 2 : 1 : zipWith ( + ) f (tail f)
f_print n = print( show n ++ "th Lucas number is " ++ show (f!!n))

main = f_print 20

<back>

              Perrin Numbers in Haskell - F(n) = F(n-2) + F(n-3)

module Main where
f = 3 : 0 : 2 : (zipWith ( + ) f  ( tail f))
f_print n = print( show n ++ "th Perrin number is " ++ show ( f!!n) )

main = f_print 20

<back>

       Sofo Numbers in Haskell - F(n) = 2*F(n-2)+F(n-3)

module Main where
f = 1 : -2 : 4 : zipWith ( + ) f  ( zipWith ( + ) ( map ( 0*) (tail f) ) (map (
    (-2)*) ( tail ( tail f )) ) )

f_print n = print( show n ++ "th Sofo sequence number is " ++ show (f!!n))

main = f_print 20
 
                     <back>

              Phone Number Word Generator

module Main where
-- create all possible words made from the digits of a phone number

import Char
mper :: (String,Char) -> String
mper (x,y) = x ++ [y]

cart2 :: [String] -> String -> [( String, Char)]
cart2 [] ys = [([],y) | y <- ys]
cart2 xs ys = [(x,y) | x <- xs, y <-ys ]
mperlist :: [(String,Char)] -> [String]
mperlist [] = []
mperlist (x:xs) = mper x : mperlist xs

xtup :: [( Char, String)]
xtup = [('0',"_"),('1',"_"),('2',"abc"),('3',"def"),('4',"ghi"),('5',"jkl"),
       ('6',"mno"),('7',"pqrs"),('8',"tuv"),('9',"wxyz")]

expandnum:: Char -> String
expandnum a = if isDigit a  then expnd else error "Non numeric char in number"
    where
       expnd =  head (  [ y | (x,y) <- xtup , x == a ])
      

expandphone :: String -> [String]
expandphone [] = [".",".","."]
expandphone ( x:xs) =  mperlist( cart2 (expandphone xs) ( expandnum x ) )

prntlst :: [String] -> IO()
prntlst [] = return ()
prntlst (x:xs) = do print ( x )
                    prntlst xs

explist n = prntlst ( xlist n )  where
       xlist n =   expandphone (reverse n )

main = explist  "927633"

                              <back>


              Generalized Two Term Recurrances

module Main where


                              <back>


              Continued Fractions

invmuladd :: Float -> (Float,Float)  -> Float
invmuladd a (b,c)  = (c/a) + b

cfrac :: [(Float,Float)] -> Float
cfrac [] = 1.0
cfrac x = foldl (invmuladd ) 1.0 x


prf  = print("result =" ++ show(cfrac [(1.0,2.0),(1.0,7.0),(1.0,4.0)]) )


main = prf
                              <back>

              Simple Lindenmayer System


module Main where

firsteq:: Char->(Char,String)->Bool
firsteq a (b,c) = (a == b)


assoc:: [(Char,String)] ->Char->String
assoc a b  = snd( head( filter (firsteq b)  a ))

linditer:: [(Char,String)]->String->String
linditer a b = concat ( map (assoc a)  b )


linden:: Int->[(Char,String)]->String->String
linden 0 a b = b
linden n a b = linditer  a ( linden (n-1) a b)

lindentst = linden 15 [('A',"B"),('B',"C"),('C',"AB")] "A"

prlind = print ( "this is the result =  " ++ lindentst ++ " " ++ show( length( lindentst)  ))

main = prlind
                            <back>