diff --git a/lab04/ho.hs b/lab04/ho.hs new file mode 100644 index 0000000..f1a09ef --- /dev/null +++ b/lab04/ho.hs @@ -0,0 +1,77 @@ +import qualified Data.Map as Map + +m = Map.empty +m' = Map.insert 3 "hello" m +s = case (Map.lookup 3 m') of + Just s' -> s' + Nothing -> error "not found" + + +addNums x y = x + y + +inc :: Int -> Int +inc = addNums 1 + +incList :: [Int] -> [Int] +incList [] = [] +incList (x:xs) = inc x : (incList xs) + +decList :: [Int] -> [Int] +decList [] = [] +decList (x:xs) = (x-1) : (decList xs) + + +applyFun2List :: (a->b) -> [a] -> [b] +applyFun2List _ [] = [] +applyFun2List f (x:xs) = (f x) : (applyFun2List f xs) + +incList' = applyFun2List inc +--incList' = map inc + + +removeNegatives :: [Integer] -> [Integer] +removeNegatives [] = [] +removeNegatives (x:xs) = + let rest = removeNegatives xs in + if x >= 0 then + x : rest + else + rest + +--filter +removeBadElems :: (a -> Bool) -> [a] -> [a] +removeBadElems _ [] = [] +removeBadElems pred (x:xs) = + let rest = removeBadElems pred xs in + if pred x then + x : rest + else + rest + + + +addListOfNums :: Num a => a -> [a] -> a +addListOfNums accum [] = accum +addListOfNums accum (x:xs) = addListOfNums (accum + x) xs + +decListOfNums :: Num a => a -> [a] -> a +decListOfNums accum [] = accum +decListOfNums accum (x:xs) = addListOfNums (accum - x) xs + +addListOfNumStrings :: Int -> [String] -> Int +addListOfNumStrings accum [] = accum +addListOfNumStrings accum (s:xs) = addListOfNumStrings (accum + (read s)) xs + + +--foldl +foldTogether :: (a -> b -> a) -> a -> [b] -> a +foldTogether _ accum [] = accum +foldTogether f accum (x:xs) = foldTogether f (f accum x) xs + +addListOfNums' :: Num a => [a] -> a +addListOfNums' = foldTogether (+) 0 + + + + + diff --git a/lab04/lab.lhs b/lab04/lab.lhs new file mode 100644 index 0000000..afb045b --- /dev/null +++ b/lab04/lab.lhs @@ -0,0 +1,46 @@ +> import Data.List + +Experiment with foldl, foldr, and foldl' + +First, implement your own version of the foldl function, +defined as myFoldl + +> myFoldl :: (a -> b -> a) -> a -> [b] -> a +> myFoldl _ _ _ = error "TBD" + + +Next, define a function to reverse a list using foldl. + +> myReverse :: [a] -> [a] +> myReverse _ = error "TBD" + + +Now define your own version of foldr, named myFoldr + +> myFoldr :: (a -> b -> b) -> b -> [a] -> b +> myFoldr _ _ _ = error "TBD" + + +Now try using foldl (the library version, not yours) to sum up the numbers of a large list. +Why is it so slow? + +Instead of foldl, try using foldl'. +Why is it faster? +(Read http://www.haskell.org/haskellwiki/Foldr_Foldl_Foldl%27 for some hints) + + +For an extra challenge, try to implement foldl in terms of foldr. +See http://www.haskell.org/haskellwiki/Foldl_as_foldr for details. + + +Next, using the map function, convert every item in a list to its absolute value + +> listAbs :: [Integer] -> [Integer] +> listAbs _ = error "TBD" + +Finally, write a function that takes a list of Integers and returns the sum of +their absolute values. + +> sumAbs :: [Integer] -> Integer +> sumAbs _ = error "TBD" + diff --git a/lab04/mapFilter.signed.zip b/lab04/mapFilter.signed.zip new file mode 100644 index 0000000..9261a23 Binary files /dev/null and b/lab04/mapFilter.signed.zip differ diff --git a/lab04/perhaps.hs b/lab04/perhaps.hs new file mode 100644 index 0000000..0db6670 --- /dev/null +++ b/lab04/perhaps.hs @@ -0,0 +1,9 @@ +data Perhaps a = + PerhapsSo a + | PerhapsNot + deriving Show + +instance Functor Perhaps where + fmap f (PerhapsSo x) = PerhapsSo (f x) + fmap f PerhapsNot = PerhapsNot + diff --git a/lab04/tailRecursion.hs b/lab04/tailRecursion.hs new file mode 100644 index 0000000..bbcf89b --- /dev/null +++ b/lab04/tailRecursion.hs @@ -0,0 +1,14 @@ +fact :: Integer -> Integer +fact 1 = 1 +fact n = n * (fact $ n - 1) + + +fact' :: Integer -> Integer -> Integer +fact' 0 acc = acc +fact' n acc = fact' (n - 1) (n * acc) + +fact2 :: Integer -> Integer -> Integer +fact2 n acc = if n == 0 + then acc + else fact2 (n - 1) (n * acc) + diff --git a/lab04/tree.hs b/lab04/tree.hs new file mode 100644 index 0000000..23f0909 --- /dev/null +++ b/lab04/tree.hs @@ -0,0 +1,36 @@ +--This works, but it is not generic +data TreeStringInt = + EmptyTSI + | NodeTSI String Int TreeStringInt TreeStringInt + deriving (Show) + +findTsi :: String -> TreeStringInt -> Maybe Int +findTsi _ EmptyTSI = Nothing +findTsi s (NodeTSI key i left right) = + if key == s then + Just i + else if s < key then + findTsi s left + else + findTsi s right + + + +--Using type parameters, we can create a BST in a more generic form +data Tree k v = + Empty + | Node k v (Tree k v) (Tree k v) + deriving (Show) + +--findT :: k -> Tree k v -> Maybe v +findT :: Ord k => k -> Tree k v -> Maybe v +findT _ Empty = Nothing +findT s (Node key val left right) = + if key == s then + Just val + else if s < key then + findT s left + else + findT s right + +