lab04b: init

This commit is contained in:
2026-02-11 11:27:00 -08:00
parent 9a8d2abfdb
commit 2943b49182
6 changed files with 182 additions and 0 deletions

77
lab04/ho.hs Normal file
View File

@@ -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

46
lab04/lab.lhs Normal file
View File

@@ -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"

BIN
lab04/mapFilter.signed.zip Normal file

Binary file not shown.

9
lab04/perhaps.hs Normal file
View File

@@ -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

14
lab04/tailRecursion.hs Normal file
View File

@@ -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)

36
lab04/tree.hs Normal file
View File

@@ -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