Skip to content

Latest commit

 

History

History
128 lines (102 loc) · 1.92 KB

ch03.adoc

File metadata and controls

128 lines (102 loc) · 1.92 KB

3 String: Simple Operations with Text

3.4 Top-level versus local definitions

Exercises: Scope

  1. Yes, y in scope for z

  2. No, h is not in scope for g

  3. No, d is local to area and thus out of scope in r = d / 2

  4. Yes, r and p are in scope for area

3.5 Types of concatenation functions

Exercises: Syntax Errors

  1. Does nto compile because infix operator is used as prefix

    ++ [1, 2, 3] [4, 5, 6]
    -- fix
    (++) [1, 2, 3] [4, 5, 6]
  2. Does not compile due to incorrect symbol surrounding strings

    '<3' ++ ' Haskell'
    --fix
    "<3" ++ " Haskell"
  3. Compiles. Result: "<3 Haskell"

    concat ["<3", " Haskell"]

3.8 Chapter Exercises

Reading syntax

Fixing Errors:

concat [[1, 2, 3], [4, 5, 6]]
-- Working

++ [1, 2, 3] [4, 5, 6]
-- Incorrect (infix used as prefix). Fix:
(++) [1, 2, 3] [4, 5, 6]

(++) "hello" " world"
-- Working

["hello" ++ " world]
-- Incorrect (missing closing "). Fix:
["hello" ++ " world"]

4 !! "hello"
-- Incorrect (order of arguments). Fix:
"hello" !! 4

(!!) "hello" 4
-- Working

take "4 lovely"
-- Incorrect (placement of opening "). Fix:
take 4 "lovely"

take 3 "awesome"
-- Working

Pair code with result

-- a) - d)
concat [[1 * 6], [2 * 6], [3 * 6]]
[6,12,18]

-- b) - c)
"rain" ++ drop 2 "elbow"
"rainbow"

-- c) - e)
10 * head [1, 2, 3]
10

-- d) - a)
(take 3 "Julie") ++ (tail "yes")
"Jules"

-- e) - b)
concat [tail [1, 2, 3], tail [4, 5, 6], tail [7, 8, 9]]
[2,3,5,6,8,9]

Building functions

  1. String/list manipulation functions (includes 1 and 2)

link:ch03_3.8_1.hs[role=include]
  1. thirdLetter

link:ch03_3.8_2.hs[role=include]
  1. Letter index

link:ch03_3.8_3.hs[role=include]
  1. Reverse function using drop and take (for fixed input)

link:ch03_3.8_4.hs[role=include]
  1. Extension of previous

link:ch03_3.8_5.hs[role=include]