From 2b722cf3bdd267c999f25c4f1c2ebb4cc4d27cb4 Mon Sep 17 00:00:00 2001 From: "Matthias P. Braendli" Date: Mon, 2 Dec 2013 22:56:10 +0100 Subject: add other haskell files --- LearnYouAHaskell.pdf | Bin 0 -> 1366517 bytes ch03_BogusPattern.hs | 9 +++++++++ ch03_localfunction.hs | 5 +++++ ch04_suffixtree.hs | 28 ++++++++++++++++++++++++++++ ex69.hs | 37 +++++++++++++++++++++++++++++++++++++ foo.hs | 21 +++++++++++++++++++++ listlen.hs | 15 +++++++++++++++ qsort.hs | 3 +++ 8 files changed, 118 insertions(+) create mode 100644 LearnYouAHaskell.pdf create mode 100644 ch03_BogusPattern.hs create mode 100644 ch03_localfunction.hs create mode 100644 ch04_suffixtree.hs create mode 100644 ex69.hs create mode 100644 foo.hs create mode 100644 listlen.hs create mode 100644 qsort.hs diff --git a/LearnYouAHaskell.pdf b/LearnYouAHaskell.pdf new file mode 100644 index 0000000..0e97bfe Binary files /dev/null and b/LearnYouAHaskell.pdf differ diff --git a/ch03_BogusPattern.hs b/ch03_BogusPattern.hs new file mode 100644 index 0000000..34288d3 --- /dev/null +++ b/ch03_BogusPattern.hs @@ -0,0 +1,9 @@ +data Fruit = Apple | Orange + deriving Show + +whichFruit f = case f of + "apple" -> Just Apple + "orange" -> Just Orange + _ -> Nothing + + diff --git a/ch03_localfunction.hs b/ch03_localfunction.hs new file mode 100644 index 0000000..61ecc25 --- /dev/null +++ b/ch03_localfunction.hs @@ -0,0 +1,5 @@ +pluralise :: String -> Int -> String +pluralise word counts = plural counts + where plural 0 = "no " ++ word ++ "s" + plural 1 = "one " ++ word + plural n = show n ++ " " ++ word ++ "s" diff --git a/ch04_suffixtree.hs b/ch04_suffixtree.hs new file mode 100644 index 0000000..a677ea4 --- /dev/null +++ b/ch04_suffixtree.hs @@ -0,0 +1,28 @@ +import Data.List (tails) + +suffixes :: [a] -> [[a]] +suffixes xs@(_:xs') = xs : suffixes xs' +-- as pattern. +-- binds xs to the value on the right of the @ +suffixes _ = [] + +-- same thing without as-pattern +-- This will do a copy of the data, the as-pattern doesn't + +suffixesNoAs (x:xs) = (x:xs) : suffixesNoAs xs +suffixesNoAs _ = [] + +-- even better +suffixes2 :: [a] -> [[a]] +suffixes2 xs = init (tails xs) + + +compose :: (b -> c) -> (a -> b) -> a -> c +compose f g x = f (g x) + +suffixes3 xs = compose init tails xs + +suffixes4 = compose init tails + +-- no need to invent the compose function +suffixes5 = init . tails diff --git a/ex69.hs b/ex69.hs new file mode 100644 index 0000000..04596be --- /dev/null +++ b/ex69.hs @@ -0,0 +1,37 @@ + +-- compute length of list + + +listlen :: [a] -> Int +listlen2 :: [a] -> Int -> Int + +listlen ( x:xs ) = listlen2 xs 1 +listlen2 ( x:xs ) n = listlen2 xs (n + 1) +listlen2 [] n = n + +myReverse (x:xs) = myReverse xs ++ [x] +myReverse [] = [] + +makePalin x = x ++ myReverse x + +isPalin a = a == myReverse a + + + +-- in python: ",".join(["abc", "def"]) +join :: String -> [String] -> String + +join sep (x:xs) | xs /= [] = x ++ sep ++ join sep xs +join sep [x] = x + + +-- a tree + +data Tree a = Node a (Tree a) (Tree a) + | Empty + deriving Show + +--height :: Tree -> Int + +height (Node _ left right) = max ((height left) + 1) ((height right) + 1) +height Empty = 0 diff --git a/foo.hs b/foo.hs new file mode 100644 index 0000000..0cdf3b6 --- /dev/null +++ b/foo.hs @@ -0,0 +1,21 @@ +foo :: [(Int, Int)] +foo = [(8, 14), (4,2), (3,4)] + +findmin :: (Int, Int) -> (Int, Int) -> (Int, Int) +findmin (a,b) (c,d) + | a < c = (a,b) + | otherwise = (c,d) + +minf :: [(Int, Int)] -> (Int, Int) +minf (x:xs) = foldr findmin x xs + +minf2 :: [(Int, Int)] -> Int +minf2 xs = b + where (a,b) = minf xs + + + +data Bar = Bar Int Int deriving (Eq,Show) + +instance Ord Bar where + (Bar _ s1) `compare` (Bar _ s2) = s1 `compare` s2 diff --git a/listlen.hs b/listlen.hs new file mode 100644 index 0000000..cb60aef --- /dev/null +++ b/listlen.hs @@ -0,0 +1,15 @@ + +-- compute length of list + + +listlen :: [a] -> Int +listlen2 :: [a] -> Int -> Int + +listlen ( x:xs ) = listlen2 xs 1 +listlen2 ( x:xs ) n = listlen2 xs (n + 1) +listlen2 [] n = n + +myReverse (x:xs) = myReverse xs ++ [x] +myReverse [] = [] + +makePalin x = x ++ myReverse x diff --git a/qsort.hs b/qsort.hs new file mode 100644 index 0000000..f349b82 --- /dev/null +++ b/qsort.hs @@ -0,0 +1,3 @@ + +qsort (x:xs) = qsort ( filter (< x) xs) ++ [x] ++ qsort ( filter (> x) xs ) +qsort [] = [] -- cgit v1.2.3