lab04b: WIP
This commit is contained in:
@@ -7,18 +7,22 @@ defined as myFoldl
|
||||
|
||||
> myFoldl :: (a -> b -> a) -> a -> [b] -> a
|
||||
> myFoldl _ _ _ = error "TBD"
|
||||
> myFoldl f acc [] = acc
|
||||
> myFoldl f acc (x:xs) = f acc $ myFoldl f x xs
|
||||
|
||||
|
||||
Next, define a function to reverse a list using foldl.
|
||||
|
||||
> myReverse :: [a] -> [a]
|
||||
> myReverse _ = error "TBD"
|
||||
> myReverse = foldl (\acc x -> x : acc) []
|
||||
|
||||
|
||||
Now define your own version of foldr, named myFoldr
|
||||
|
||||
> myFoldr :: (a -> b -> b) -> b -> [a] -> b
|
||||
> myFoldr _ _ _ = error "TBD"
|
||||
> myFoldr f acc [] = acc
|
||||
> myFoldr f acc (x:xs) = f x $ myFoldr f acc xs
|
||||
|
||||
|
||||
Now try using foldl (the library version, not yours) to sum up the numbers of a large list.
|
||||
|
||||
Reference in New Issue
Block a user