Browse Source

Merge branch 'exo'

until slide 50
DricomDragon 5 years ago
parent
commit
b6fae2771b
3 changed files with 39 additions and 0 deletions
  1. 13 0
      anagram.hs
  2. 13 0
      fold.hs
  3. 13 0
      prime.hs

+ 13 - 0
anagram.hs

@@ -0,0 +1,13 @@
+import Data.List
+
+sweep::(String -> [String]) -> String -> String -> [String]
+sweep f l stack = case stack of
+	[] -> [""]
+	cur:substack -> concat [map (\xs -> cur:xs) (f (delete cur l)), sweep f l substack]
+
+ana::String -> [String]
+ana l = sweep (ana) l l
+
+-- Teacher version
+anac "" = [""]
+anac xs = concat (map (\c -> map (c:) (anac (delete c xs))) xs)

+ 13 - 0
fold.hs

@@ -0,0 +1,13 @@
+-- Exercice from slide 46
+
+fsum::Num a => [a] -> a
+fsum = foldl (+) 0
+
+fmax::(Ord a, Num a) => [a] -> a
+fmax = foldl max 0
+
+for::[Bool] -> Bool
+for = foldl (||) False
+
+fany::(a->Bool)->[a]->Bool
+fany p xs = for $ map p xs

+ 13 - 0
prime.hs

@@ -0,0 +1,13 @@
+-- Exercice from slide 42/82
+
+dividers n = [k | k <- takeWhile (\k -> k * k <= n) primeinf, rem n k == 0]
+dividersf n = filter (\k -> rem n k == 0) [2..(n-1)]
+
+primer n = [i | i <- [2..(n-1)], null (dividers i)]
+primerf n = filter (\i -> null (dividersf i)) [2..(n-1)]
+
+square n = n * n
+
+-- Exercice from slide 50
+primeinf = 2:[i | i <- [3..], null (dividers i)]
+