From 2943b4918270abc6b3bf1924f5d0090cd75c8df7 Mon Sep 17 00:00:00 2001 From: Yuri Tatishchev Date: Wed, 11 Feb 2026 11:27:00 -0800 Subject: [PATCH] lab04b: init --- lab04/ho.hs | 77 +++++++++++++++++++++++++++++++++++++ lab04/lab.lhs | 46 ++++++++++++++++++++++ lab04/mapFilter.signed.zip | Bin 0 -> 2005 bytes lab04/perhaps.hs | 9 +++++ lab04/tailRecursion.hs | 14 +++++++ lab04/tree.hs | 36 +++++++++++++++++ 6 files changed, 182 insertions(+) create mode 100644 lab04/ho.hs create mode 100644 lab04/lab.lhs create mode 100644 lab04/mapFilter.signed.zip create mode 100644 lab04/perhaps.hs create mode 100644 lab04/tailRecursion.hs create mode 100644 lab04/tree.hs 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 0000000000000000000000000000000000000000..9261a23502a600cfd7d283493a714def0df706a8 GIT binary patch literal 2005 zcmZ{ldpHvg7ss)Qa@$)o<@!^T+fXRCS8jzy?%6P9rd%`LS6Y~cOu65h zEV771Y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WK;a| zWK6R}wfv381fBH#ez&cLJzzn?I=u~%cgYyE?RLV2R_dF2~b1cb1V7mU(#A)4WJr3;KG z?|097T6+4^0N1C#zND=CD|Cqo&|OQJ8*$_x|E@Z@h^a~lLgt&@m*Yy_QT7*q^BJMp zPF7@xJ+^P3ux^CVVa7e_TU~=;ueU;Q*Nz4ncdxBp51D;g;E8cd_+rxj%zMl$8E>D$ zJh`we*lKQWLJc8E;Wt@7wRc4Jkcqry=qK34CG)@@GKEFJLVvmG?>OCa)4%I?Z~foR f->>fB_Aea~`5Sy)97Ol+4MlbXyPF-w_PRd-_vWjz literal 0 HcmV?d00001 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 + +