Real World Haskell - 第1章 練習問題

Real World Haskell を継続的に勉強中。

Exercises

Chapter 1. Getting Started

1. Enter the following expressions into ghci. What are their types?

* 5 + 8
* 3 * 5 + 8
* 2 + 4
* (+) 2 4
* sqrt 16
* succ 6
* succ 7
* pred 9
* pred 8
* sin (pi / 2)
* truncate pi
* round 3.5
* round 3.4
* floor 3.7
* ceiling 3.3

2. From ghci, type :? to print some help. Define a variable, such as let x = 1, then type :show bindings. What do you see?

3. The words function counts the number of words in a string. Modify the WC.hs example to count the number of words in a file.

4. Modify the WC.hs example again, to print the number of characters in a file.

1

Prelude> :set +t
Prelude> 5+8
13
it :: Integer
Prelude> 3*5+8
23
it :: Integer
Prelude> 2+4
6
it :: Integer
Prelude> (+) 2 4
6
it :: Integer
Prelude> sqrt 16
4.0
it :: Double
Prelude> succ 6
7
it :: Integer
Prelude> succ 7
8
it :: Integer
Prelude> pred 9
8
it :: Integer
Prelude> pred 8
7
it :: Integer
Prelude> sin (pi/2)
1.0
it :: Double
Prelude> truncate pi
3
it :: Integer
Prelude> round 3.5
4
it :: Integer
Prelude> round 3.4
3
it :: Integer
Prelude> floor 3.7
3
it :: Integer
Prelude> ceiling 3.3
4
it :: Integer
Prelude>

2

Prelude> let x = 1
Prelude> :show bindings
x :: Integer = _
Prelude>

3

main = interact wordCount
    where wordCount input = show (length (words input)) ++ "\n"

4

main = interact wordCount
    where wordCount input = show (length input) ++ "\n"

参考