Test0

Plain source file: Test0.hs (2010-10-22)

module Test0 where
--default(Int)

import qualified Data.Map as M
import qualified Data.ByteString.Char8 as BS

data Nat = Zero | Succ {pred::Nat} deriving Eq

add Zero     b = b
add (Succ a) b = Succ (add a b)


z = Zero
s n = Succ n

one = s z
two = s one
three = Succ two

four = add two two
five = add two three
ten = add five five
twenty = add ten ten

no = five == four
yes = add two three == five

count Zero = []
count (Succ n) = Succ n:count n

int :: Nat->Int
int Zero = 0
int (Succ n) = 1+int n

test1 = map int (count five)

hello = "Hejdå!"
--hello = 'H':'e':'l':'l':'o':[]

hellohello = cycle hello

yes2 = hello==hello

f 'a' = 'A'
g "a" = "A"

pair x = (,) x

(a,b) = pair 'a' 'b'

double x = x+x

fac :: Int->Int
fac 0 = 1
fac n = n * fac(n-1)

fib :: Integer->Integer
fib n = if n<2
        then 1
        else fib(n-1)+fib(n-2)
             
n = length hello

uncons [x] = (x)


class (Show a,Read a) => Text a where
  textid :: a -> a

instance Text Int where
  textid = read . show

instance Text a => Text [a] where
  textid = map textid

six = textid [n]

infinite = True : infinite

numbers = 0:[n+1|n<-numbers]
odds = [1,3..]

primes = sieve [2..]

sieve (p:ns) = p:sieve [n|n<-ns,n `mod` p/=0]

primes2 = sieve2a 2 (repeat True)

sieve2a n (False:bs) = sieve2a (n+1) bs
sieve2a n (True:bs) = n:sieve2a (n+1) (mark n bs)
  where
    mark 1 (b:bs) = False:mark n bs
    mark n (b:bs) = b:mark (n-1) bs

sieve2b n (False:bs) = sieve2b (n+1) bs
sieve2b n (True:bs) = n:sieve2b (n+1) (mark bs)
  where
    mask = tail $ cycle (False:replicate (n-1) True)
    mark bs = zipWith (&&) bs mask

-- Primtalen är 2 samt alla udda tal utom de sammansatta talen):
-- (All credit till Richard Bird).
primes3 = 2 : 3 : 5 : ([7,9..] `but` compos (tail primes3))
 where

  compos (x:xs) = x*x : [ i*x | i <- [x+2,x+4..]] `merge` compos xs

  (x:xs) `but` (y:ys) =
   case x `compare` y of
     LT -> x : (xs `but` (y:ys))
     EQ -> xs `but` ys
     GT -> (x:xs) `but` ys

  (x:xs) `merge` (y:ys) =
   case x `compare` y of
     LT -> x : (xs `merge` (y:ys))
     EQ -> x : (xs `merge` ys)
     GT -> y : ((x:xs) `merge` ys)

  lmerge (x:xs) ys = x:merge xs ys


xs :: Int -> [(BS.ByteString,Int)]
xs n = M.toList (M.fromList [(BS.pack ("a"++show i),i*i)|i<-[1..n]])

more [([_],_)] = undefined

ys = show [50..58]

newtype In a = In {out::a}

out2 x = out x
replace y x = x{out=y}

c x = case x of
        y -> (x,y)

main = (xs,ys,test1,numbers,odds,primes,primes2,primes3,fac,fib,
        (n,six,c,out2,replace),
        (concatMap,(,,),(,,,)))

Index

(HTML for this module was generated on 2010-10-23. About the conversion tool.)