From db32368e34cb40fc1c3f0147f492cc58a5c20363 Mon Sep 17 00:00:00 2001 From: Lilly Date: Wed, 5 Oct 2022 14:48:03 +0100 Subject: [PATCH 1/9] Complete chapter 2 --- src/Chapter2.hs | 62 +++++++++++++++++++++++++++++++++++-------------- 1 file changed, 45 insertions(+), 17 deletions(-) diff --git a/src/Chapter2.hs b/src/Chapter2.hs index f7c02ce2..92ecfd34 100644 --- a/src/Chapter2.hs +++ b/src/Chapter2.hs @@ -186,32 +186,43 @@ Evaluate the following expressions in GHCi and insert the answers. Try to guess first, what you will see. >>> [10, 2] ++ [3, 1, 5] +[10, 2, 3, 1, 5] >>> [] ++ [1, 4] -- [] is an empty list +[1, 4] >>> 3 : [1, 2] +[3, 1, 2] >>> 4 : 2 : [5, 10] -- prepend multiple elements +[4, 2, 5, 10] >>> [1 .. 10] -- list ranges +[1,2,3,4,5,6,7,8,9,10] >>> [10 .. 1] +[] >>> [10, 9 .. 1] -- backwards list with explicit step +[10,9,8,7,6,5,4,3,2,1] >>> length [4, 10, 5] -- list length +3 >>> replicate 5 True +[True,True,True,True,True] >>> take 5 "Hello, World!" +"Hello" >>> drop 5 "Hello, World!" +", World!" >>> zip "abc" [1, 2, 3] -- convert two lists to a single list of pairs +[('a', 1), ('b', 2), ('c', 3)] >>> words "Hello Haskell World!" -- split the string into the list of words - - +["Hello","Haskell","World!"] 👩‍🔬 Haskell has a lot of syntax sugar. In the case with lists, any list literal like "[3, 1, 2]" is syntax sugar for prepending elements @@ -336,7 +347,7 @@ from it! ghci> :l src/Chapter2.hs -} subList :: Int -> Int -> [a] -> [a] -subList = error "subList: Not implemented!" +subList a b xs = drop a . take b $ xs {- | =⚔️= Task 4 @@ -348,9 +359,10 @@ Implement a function that returns only the first half of a given list. >>> firstHalf "bca" "b" -} --- PUT THE FUNCTION TYPE IN HERE -firstHalf l = error "firstHalf: Not implemented!" +firstHalf :: [a] -> [a] +firstHalf l = take len l + where len = (length l) `div` 2 {- | =🛡= Pattern matching @@ -501,7 +513,10 @@ True >>> isThird42 [42, 42, 0, 42] False -} -isThird42 = error "isThird42: Not implemented!" + +isThird42 :: Integral a => [a] -> Bool +isThird42 (_ : x : _) = x == 42 +isThird42 _ = False {- | @@ -605,9 +620,10 @@ Implement a function that duplicates each element of the list "aabbaacc" -} -duplicate :: [a] -> [a] -duplicate = error "duplicate: Not implemented!" +-- Couldn't figure out how to do it with a pattern match? Not sure if this is right +duplicate :: [a] -> [a] +duplicate = concatMap (replicate 2) {- | =⚔️= Task 7 @@ -621,7 +637,11 @@ Write a function that takes elements of a list only in even positions. >>> takeEven [2, 1, 3, 5, 4] [2,3,4] -} -takeEven = error "takeEven: Not implemented!" + +takeEven (x : y ) = x : go y + where go (y:x) = takeEven x + go [] = [] +takeEven [] = [] {- | =🛡= Higher-order functions @@ -727,8 +747,9 @@ value of the element itself 🕯 HINT: Use combination of 'map' and 'replicate' -} + smartReplicate :: [Int] -> [Int] -smartReplicate l = error "smartReplicate: Not implemented!" +smartReplicate l = map (\x -> replicate x x) l {- | =⚔️= Task 9 @@ -741,7 +762,8 @@ the list with only those lists that contain a passed element. 🕯 HINT: Use the 'elem' function to check whether an element belongs to a list -} -contains = error "contains: Not implemented!" +contains :: Integral a => a -> [[a]] -> [[a]] +contains n xs = filter (\ys -> n `elem` ys) xs {- | @@ -780,14 +802,16 @@ nextInt = add 1 Let's now try to eta-reduce some of the functions and ensure that we mastered the skill of eta-reducing. -} + divideTenBy :: Int -> Int -divideTenBy x = div 10 x +divideTenBy = div 10 --- TODO: type ;) -listElementsLessThan x l = filter (< x) l +listElementsLessThan :: (Num a, Ord a) => a -> [a] -> [a] +listElementsLessThan x = filter (< x) -- Can you eta-reduce this one??? -pairMul xs ys = zipWith (*) xs ys +pairMul :: Num a => [a] -> [a] -> [a] +pairMul xs = (zipWith (*) xs) {- | =🛡= Lazy evaluation @@ -842,7 +866,9 @@ list. 🕯 HINT: Use the 'cycle' function -} -rotate = error "rotate: Not implemented!" +rotate n xs = if (n /= (abs n)) then + [] + else take (length xs) $ drop n $ (cycle xs) {- | =💣= Task 12* @@ -858,8 +884,10 @@ and reverses it. function, but in this task, you need to implement it manually. No cheating! -} -rewind = error "rewind: Not Implemented!" +rewind :: [a] -> [a] +rewind (x:xs) = rewind xs ++ [x] +rewind [] = [] {- You did it! Now it is time to open pull request with your changes From 92f21a2009a1b6ff1ab14acbd2a3962bd1a6c5cc Mon Sep 17 00:00:00 2001 From: Lilly Date: Wed, 5 Oct 2022 14:55:27 +0100 Subject: [PATCH 2/9] Update Chapter2.hs --- src/Chapter2.hs | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/src/Chapter2.hs b/src/Chapter2.hs index 92ecfd34..bf0de427 100644 --- a/src/Chapter2.hs +++ b/src/Chapter2.hs @@ -749,7 +749,7 @@ value of the element itself -} smartReplicate :: [Int] -> [Int] -smartReplicate l = map (\x -> replicate x x) l +smartReplicate l = concatMap (\x -> replicate x x) l {- | =⚔️= Task 9 From f49ac11f749ea7420788f522c8913c20e5caf3f0 Mon Sep 17 00:00:00 2001 From: Lilly Date: Wed, 5 Oct 2022 15:01:13 +0100 Subject: [PATCH 3/9] Update Chapter2.hs --- src/Chapter2.hs | 22 +++++++++++----------- 1 file changed, 11 insertions(+), 11 deletions(-) diff --git a/src/Chapter2.hs b/src/Chapter2.hs index bf0de427..e3cee315 100644 --- a/src/Chapter2.hs +++ b/src/Chapter2.hs @@ -136,43 +136,43 @@ functions in GHCi and insert the corresponding resulting output below: List of booleans: >>> :t [True, False] - +[True, False] :: [Bool] String is a list of characters: >>> :t "some string" - +"c" :: String Empty list: >>> :t [] - +[] :: [a] Append two lists: >>> :t (++) - +(++) :: [a] -> [a] -> [a] Prepend an element at the beginning of a list: >>> :t (:) - +(:) :: a -> [a] -> [a] Reverse a list: >>> :t reverse - +reverse :: [a] -> [a] Take first N elements of a list: >>> :t take - +ake :: Int -> [a] -> [a] Create a list from N same elements: >>> :t replicate - +replicate :: Int -> a -> [a] Split a string by line breaks: >>> :t lines - +lines :: String -> [String] Join a list of strings with line breaks: >>> :t unlines - +unlines :: [String] -> String -} @@ -186,7 +186,7 @@ Evaluate the following expressions in GHCi and insert the answers. Try to guess first, what you will see. >>> [10, 2] ++ [3, 1, 5] -[10, 2, 3, 1, 5] +[10,2,3,1,5] >>> [] ++ [1, 4] -- [] is an empty list [1, 4] From a6a9a653b911b1c60afce517d5bd73f55556861e Mon Sep 17 00:00:00 2001 From: Lilly Date: Wed, 5 Oct 2022 15:07:01 +0100 Subject: [PATCH 4/9] fix doctests --- src/Chapter2.hs | 10 +++++----- 1 file changed, 5 insertions(+), 5 deletions(-) diff --git a/src/Chapter2.hs b/src/Chapter2.hs index e3cee315..b55926ac 100644 --- a/src/Chapter2.hs +++ b/src/Chapter2.hs @@ -140,7 +140,7 @@ List of booleans: String is a list of characters: >>> :t "some string" -"c" :: String +"some string" :: String Empty list: >>> :t [] @@ -189,13 +189,13 @@ to guess first, what you will see. [10,2,3,1,5] >>> [] ++ [1, 4] -- [] is an empty list -[1, 4] +[1,4] >>> 3 : [1, 2] -[3, 1, 2] +[3,1,2] >>> 4 : 2 : [5, 10] -- prepend multiple elements -[4, 2, 5, 10] +[4,2,5,10] >>> [1 .. 10] -- list ranges [1,2,3,4,5,6,7,8,9,10] @@ -219,7 +219,7 @@ to guess first, what you will see. ", World!" >>> zip "abc" [1, 2, 3] -- convert two lists to a single list of pairs -[('a', 1), ('b', 2), ('c', 3)] +[('a',1),('b',2),('c',3)] >>> words "Hello Haskell World!" -- split the string into the list of words ["Hello","Haskell","World!"] From 4f71b401359c43e01ffd023cf18cddc621156953 Mon Sep 17 00:00:00 2001 From: Lilly Date: Wed, 5 Oct 2022 15:19:51 +0100 Subject: [PATCH 5/9] Update Chapter2.hs --- src/Chapter2.hs | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/src/Chapter2.hs b/src/Chapter2.hs index b55926ac..18f01ded 100644 --- a/src/Chapter2.hs +++ b/src/Chapter2.hs @@ -347,7 +347,7 @@ from it! ghci> :l src/Chapter2.hs -} subList :: Int -> Int -> [a] -> [a] -subList a b xs = drop a . take b $ xs +subList a b xs = drop a . take (b + 1) $ xs {- | =⚔️= Task 4 @@ -515,7 +515,7 @@ False -} isThird42 :: Integral a => [a] -> Bool -isThird42 (_ : x : _) = x == 42 +isThird42 (_ : _ : x : _) = x == 42 isThird42 _ = False From ce8d66bc6249150a4fd953a3e6007fd6001fbb9b Mon Sep 17 00:00:00 2001 From: Lilly Date: Wed, 5 Oct 2022 15:27:12 +0100 Subject: [PATCH 6/9] Update Chapter2.hs --- src/Chapter2.hs | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/src/Chapter2.hs b/src/Chapter2.hs index 18f01ded..0f26088c 100644 --- a/src/Chapter2.hs +++ b/src/Chapter2.hs @@ -140,7 +140,7 @@ List of booleans: String is a list of characters: >>> :t "some string" -"some string" :: String +"some string" :: [Char] Empty list: >>> :t [] From 1bd50057bc72c7ffebd4f0a9a09f393e2058ffdd Mon Sep 17 00:00:00 2001 From: Lilly Date: Wed, 5 Oct 2022 15:35:05 +0100 Subject: [PATCH 7/9] Update Chapter2.hs --- src/Chapter2.hs | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/src/Chapter2.hs b/src/Chapter2.hs index 0f26088c..ab3497d7 100644 --- a/src/Chapter2.hs +++ b/src/Chapter2.hs @@ -160,7 +160,7 @@ reverse :: [a] -> [a] Take first N elements of a list: >>> :t take -ake :: Int -> [a] -> [a] +take :: Int -> [a] -> [a] Create a list from N same elements: >>> :t replicate From cd371655faaba32c766e108ceb7d2d5a7ad86e63 Mon Sep 17 00:00:00 2001 From: Lilly Date: Wed, 5 Oct 2022 17:40:05 +0100 Subject: [PATCH 8/9] Update Chapter2.hs --- src/Chapter2.hs | 3 ++- 1 file changed, 2 insertions(+), 1 deletion(-) diff --git a/src/Chapter2.hs b/src/Chapter2.hs index ab3497d7..fded3c7b 100644 --- a/src/Chapter2.hs +++ b/src/Chapter2.hs @@ -347,7 +347,8 @@ from it! ghci> :l src/Chapter2.hs -} subList :: Int -> Int -> [a] -> [a] -subList a b xs = drop a . take (b + 1) $ xs +subList a b xs = if a < b then [] else + drop a . take (b + 1) $ xs {- | =⚔️= Task 4 From 878e071e7ada1309f3e172468f7147eba6939697 Mon Sep 17 00:00:00 2001 From: Lilly Date: Wed, 5 Oct 2022 17:48:05 +0100 Subject: [PATCH 9/9] Update Chapter2.hs --- src/Chapter2.hs | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/src/Chapter2.hs b/src/Chapter2.hs index fded3c7b..bc4b2f21 100644 --- a/src/Chapter2.hs +++ b/src/Chapter2.hs @@ -347,8 +347,8 @@ from it! ghci> :l src/Chapter2.hs -} subList :: Int -> Int -> [a] -> [a] -subList a b xs = if a < b then [] else - drop a . take (b + 1) $ xs +subList a b xs = if a > b then [] else + drop a . take (b + 1) $ xs {- | =⚔️= Task 4