Skip to content

Commit

Permalink
πŸŽ‰ chapter2
Browse files Browse the repository at this point in the history
  • Loading branch information
CypressVillage committed Jan 25, 2024
1 parent 0846d37 commit 7a65dcb
Showing 1 changed file with 56 additions and 35 deletions.
91 changes: 56 additions & 35 deletions src/Chapter2.hs
Original file line number Diff line number Diff line change
Expand Up @@ -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"
"some string" :: 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
take :: 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
-}

Expand All @@ -186,31 +186,31 @@ 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
Expand Down Expand Up @@ -336,7 +336,10 @@ from it!
ghci> :l src/Chapter2.hs
-}
subList :: Int -> Int -> [a] -> [a]
subList = error "subList: Not implemented!"
subList x y [] = []

Check warning on line 339 in src/Chapter2.hs

View workflow job for this annotation

GitHub Actions / Chapter One (3.8, 9.4.4)

Defined but not used: β€˜x’

Check warning on line 339 in src/Chapter2.hs

View workflow job for this annotation

GitHub Actions / Chapter One (3.8, 9.4.4)

Defined but not used: β€˜y’

Check warning on line 339 in src/Chapter2.hs

View workflow job for this annotation

GitHub Actions / Build Learn4Haskell (3.8, 9.4.4)

Defined but not used: β€˜x’

Check warning on line 339 in src/Chapter2.hs

View workflow job for this annotation

GitHub Actions / Build Learn4Haskell (3.8, 9.4.4)

Defined but not used: β€˜y’

Check warning on line 339 in src/Chapter2.hs

View workflow job for this annotation

GitHub Actions / Chapter Two (3.8, 9.4.4)

Defined but not used: β€˜x’

Check warning on line 339 in src/Chapter2.hs

View workflow job for this annotation

GitHub Actions / Chapter Two (3.8, 9.4.4)

Defined but not used: β€˜y’
subList x y xs
| x > y = []
| otherwise = (drop x) . (take (y+1)) $ xs

Check warning on line 342 in src/Chapter2.hs

View workflow job for this annotation

GitHub Actions / Chapter One (3.8, 9.4.4)

The tight infix use of a β€˜+’ might be repurposed as special syntax

Check warning on line 342 in src/Chapter2.hs

View workflow job for this annotation

GitHub Actions / Build Learn4Haskell (3.8, 9.4.4)

The tight infix use of a β€˜+’ might be repurposed as special syntax

Check warning on line 342 in src/Chapter2.hs

View workflow job for this annotation

GitHub Actions / Chapter Two (3.8, 9.4.4)

The tight infix use of a β€˜+’ might be repurposed as special syntax

{- |
=βš”οΈ= Task 4
Expand All @@ -349,7 +352,9 @@ Implement a function that returns only the first half of a given list.
"b"
-}
-- PUT THE FUNCTION TYPE IN HERE
firstHalf l = error "firstHalf: Not implemented!"
firstHalf :: [a] -> [a]
firstHalf l = take half l
where half = length l `div` 2


{- |
Expand Down Expand Up @@ -501,7 +506,9 @@ True
>>> isThird42 [42, 42, 0, 42]
False
-}
isThird42 = error "isThird42: Not implemented!"
isThird42 :: [Int] -> Bool
isThird42 (_:_:x:xs) = x == 42

Check warning on line 510 in src/Chapter2.hs

View workflow job for this annotation

GitHub Actions / Chapter One (3.8, 9.4.4)

Defined but not used: β€˜xs’

Check warning on line 510 in src/Chapter2.hs

View workflow job for this annotation

GitHub Actions / Build Learn4Haskell (3.8, 9.4.4)

Defined but not used: β€˜xs’

Check warning on line 510 in src/Chapter2.hs

View workflow job for this annotation

GitHub Actions / Chapter Two (3.8, 9.4.4)

Defined but not used: β€˜xs’
isThird42 _ = False


{- |
Expand Down Expand Up @@ -606,7 +613,8 @@ Implement a function that duplicates each element of the list
-}
duplicate :: [a] -> [a]
duplicate = error "duplicate: Not implemented!"
duplicate (x:xs) = x:x:duplicate xs
duplicate _ = []


{- |
Expand All @@ -621,7 +629,9 @@ 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 :: [Int] -> [Int]
takeEven (x:y:xs) = x : takeEven xs

Check warning on line 633 in src/Chapter2.hs

View workflow job for this annotation

GitHub Actions / Chapter One (3.8, 9.4.4)

Defined but not used: β€˜y’

Check warning on line 633 in src/Chapter2.hs

View workflow job for this annotation

GitHub Actions / Build Learn4Haskell (3.8, 9.4.4)

Defined but not used: β€˜y’

Check warning on line 633 in src/Chapter2.hs

View workflow job for this annotation

GitHub Actions / Chapter Two (3.8, 9.4.4)

Defined but not used: β€˜y’
takeEven x = x

{- |
=πŸ›‘= Higher-order functions
Expand Down Expand Up @@ -728,7 +738,9 @@ value of the element itself
πŸ•― HINT: Use combination of 'map' and 'replicate'
-}
smartReplicate :: [Int] -> [Int]
smartReplicate l = error "smartReplicate: Not implemented!"
smartReplicate = concat . map replicateN
where replicateN :: Int -> [Int]
replicateN n = replicate n n

{- |
=βš”οΈ= Task 9
Expand All @@ -741,7 +753,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 :: Int -> [[Int]] -> [[Int]]
contains n xs = filter (elem n) xs


{- |
Expand Down Expand Up @@ -781,13 +794,16 @@ 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 = (10 `div`)

-- TODO: type ;)
listElementsLessThan x l = filter (< x) l
listElementsLessThan :: Int -> [Int] -> [Int]
listElementsLessThan = filter . (>) -- why?
-- listElementsLessThan x l = filter (< x) l

-- Can you eta-reduce this one???
pairMul xs ys = zipWith (*) xs ys
pairMul :: [Int] -> [Int] -> [Int]
pairMul = zipWith (*)

{- |
=πŸ›‘= Lazy evaluation
Expand Down Expand Up @@ -842,7 +858,10 @@ list.
πŸ•― HINT: Use the 'cycle' function
-}
rotate = error "rotate: Not implemented!"
rotate :: Int -> [a] -> [a]
rotate n xs
| n < 0 = []
| otherwise = take (length xs) $ drop n $ cycle xs

{- |
=πŸ’£= Task 12*
Expand All @@ -858,7 +877,9 @@ 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 [] = []
rewind (x:xs) = rewind xs ++ [x]


{-
Expand Down

0 comments on commit 7a65dcb

Please sign in to comment.