summaryrefslogtreecommitdiffstats
diff options
context:
space:
mode:
authorMatthias P. Braendli <matthias.braendli@mpb.li>2013-12-02 22:56:10 +0100
committerMatthias P. Braendli <matthias.braendli@mpb.li>2013-12-02 22:56:10 +0100
commit2b722cf3bdd267c999f25c4f1c2ebb4cc4d27cb4 (patch)
tree95a2fa4d0aac52f4a82e234276ce7ede15a8e8ff
parent40860a46fa0a22cc376b015e36744c8573f92654 (diff)
downloadhaskell-2b722cf3bdd267c999f25c4f1c2ebb4cc4d27cb4.tar.gz
haskell-2b722cf3bdd267c999f25c4f1c2ebb4cc4d27cb4.tar.bz2
haskell-2b722cf3bdd267c999f25c4f1c2ebb4cc4d27cb4.zip
add other haskell files
-rw-r--r--LearnYouAHaskell.pdfbin0 -> 1366517 bytes
-rw-r--r--ch03_BogusPattern.hs9
-rw-r--r--ch03_localfunction.hs5
-rw-r--r--ch04_suffixtree.hs28
-rw-r--r--ex69.hs37
-rw-r--r--foo.hs21
-rw-r--r--listlen.hs15
-rw-r--r--qsort.hs3
8 files changed, 118 insertions, 0 deletions
diff --git a/LearnYouAHaskell.pdf b/LearnYouAHaskell.pdf
new file mode 100644
index 0000000..0e97bfe
--- /dev/null
+++ b/LearnYouAHaskell.pdf
Binary files 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 [] = []