Skip to content

Commit

Permalink
[kowainik#9] Fixes to Chapter Two (kowainik#24)
Browse files Browse the repository at this point in the history
Resolves kowainik#9
  • Loading branch information
chshersh authored Sep 30, 2020
1 parent 040c7cf commit e2bebab
Show file tree
Hide file tree
Showing 2 changed files with 31 additions and 21 deletions.
46 changes: 28 additions & 18 deletions src/Chapter2.hs
Original file line number Diff line number Diff line change
Expand Up @@ -134,25 +134,35 @@ functions in the upcoming exercises. Remember, Hoogle is your friend!
Explore lists by checking types of various list expressions and
functions in GHCi and insert the corresponding resulting output below:
>>> :t [True, False] -- list of booleans
List of booleans:
>>> :t [True, False]
>>> :t "some string" -- string is a list of characters
String is a list of characters:
>>> :t "some string"
>>> :t [] -- empty list
Empty list:
>>> :t []
>>> :t (++) -- append two lists
Append two lists:
>>> :t (++)
>>> :t (:) -- prepend an element at the beginning of a list
Prepend an element at the beginning of a list:
>>> :t (:)
>>> :t reverse -- reverse a list
Reverse a list:
>>> :t reverse
>>> :t take -- take first N elements of a list
Take first N elements of a list:
>>> :t take
>>> :t replicate -- create list from N same elements
Create list from N same elements:
>>> :t replicate
>>> :t lines -- split a string by line breaks
Split a string by line breaks:
>>> :t lines
>>> :t unlines -- join a list of strings with line breaks
Join a list of strings with line breaks:
>>> :t unlines
-}
Expand Down Expand Up @@ -223,11 +233,11 @@ Haskell are immutable! Woohoo! But what does it mean for us?
It means that when you apply a function to some variable, the value is
not changed. Instead, you create a new value each time.
ghci> import Data.List (sort) -- sort is not in Prelude
ghci> x = [3, 1, 2] -- you can assign values to variables in GHCi
ghci> sort x
>>> import Data.List (sort) -- sort is not in Prelude
>>> x = [3, 1, 2] -- you can assign values to variables in GHCi
>>> sort x
[1,2,3]
ghci> x
>>> x
[3,1,2]
The 'sort' function returns a new sorted list. It doesn't change the
Expand Down Expand Up @@ -306,7 +316,7 @@ Remember that each function returns a new list.
[]
♫ NOTE: When implementing, think about various corner cases. You
should return an empty list when inputs are not reasonable.
should return an empty list when given numbers are negative.
And also don't forget to check the 'Data.List' module. It is full of
yummy functions.
Expand All @@ -325,7 +335,7 @@ subList = error "subList: Not implemented!"
Implement a function that returns only the first half of a given list.
>>> firstHalf [3, 4, 1, 2]
[3, 4]
[3,4]
>>> firstHalf "bca"
"b"
-}
Expand Down Expand Up @@ -599,7 +609,7 @@ Write a function that takes elements of a list only on even positions.
least 2. Alternatively, you can use the "Recursive go" pattern.
>>> takeEven [2, 1, 3, 5, 4]
[2, 3, 4]
[2,3,4]
-}
takeEven = error "takeEven: Not implemented!"

Expand Down Expand Up @@ -803,7 +813,7 @@ Infinity Stone!
infinite lists.
-}

{-
{- |
=⚔️= Task 11
Rotating a list by a single element is the process of moving the first
Expand Down
6 changes: 3 additions & 3 deletions test/Test/Chapter2.hs
Original file line number Diff line number Diff line change
Expand Up @@ -21,7 +21,7 @@ chapter2normal = describe "Chapter2Normal" $ do
it "range within" $ subList 2 5 [0..7] `shouldBe` [2..5]
it "range 0 .. len" $ subList 0 10 [0..10] `shouldBe` [0..10]
it "range negative" $ subList (-1) 5 [0..5] `shouldBe` []
it "range overflow" $ subList 0 5 [0, 1] `shouldBe` []
it "range overflow" $ subList 0 5 [0, 1] `shouldBe` [0, 1]
it "range x > y" $ subList 5 2 [0..5] `shouldBe` []
it "range equal" $ subList 0 0 [0..3] `shouldBe` [0]
describe "Task4: firstHalf" $ do
Expand Down Expand Up @@ -58,14 +58,14 @@ chapter2normal = describe "Chapter2Normal" $ do
it "one with elem, one without" $ contains 0 [[5, 0, 1], [1..4]] `shouldBe` [[5, 0, 1]]
it "one with elem, one without" $ contains 0 [[1..4], [5, 0, 1]] `shouldBe` [[5, 0, 1]]
it "all without" $ contains 0 [[1..4], [5,4..1]] `shouldBe` ([] :: [[Int]])
it "all with" $ contains 5 [[1..5], [6,5..1]] `shouldBe` ([] :: [[Int]])
it "all with" $ contains 5 [[1..5], [6,5..1]] `shouldBe` [[1..5], [6,5..1]]
describe "Task11: rotate" $ do
it "empty list" $ rotate 5 emptyInts `shouldBe` emptyInts
it "empty list with 0" $ rotate 0 emptyInts `shouldBe` emptyInts
it "list rotate 0" $ rotate 0 [1..5] `shouldBe` [1..5]
it "list rotate len" $ rotate 5 [1..5] `shouldBe` [1..5]
it "list rotate n" $ rotate 3 [1..5] `shouldBe` [4,5,1,2,3]
it "list rotate len + n" $ rotate 8 [1..5] `shouldBe` [1..5]
it "list rotate len + n" $ rotate 8 [1..5] `shouldBe` [4,5,1,2,3]
it "empty on negative" $ rotate (-5) [1..5] `shouldBe` emptyInts

chapter2advanced :: Spec
Expand Down

0 comments on commit e2bebab

Please sign in to comment.