Real World Haskell - 第1章

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

始めよう

Chapter 1. Getting Started

GHC インストール

GHC 6.10.1Windows XP にインストールした。

単純な計算
  • 計算式入れると電卓のように計算してくれる
    • infix form

Prelude> 2 + 2
4

    • prefix form

Prelude> (+) 2 2
4

  • "The - operator is Haskell's only unary operator, and we cannot mix it with infix operators."
  • Boolean logic
    • The values of Boolean logic in Haskell are True and False.
      • 小文字で打ったらエラーになった
    • 零、非零は True, False の代わりにならない
    • ==, <, >= などの比較式は True, False を返す
    • C の != は /=
    • C の ! は not
  • :info command

Prelude> :info (+)

  • Lists

Prelude> [1, 2, 3]
[1,2,3]

    • Commas are separators, not terminators
    • enumeration notation

Prelude> [1.0,1.25..2.0]
[1.0,1.25,1.5,1.75,2.0]

  • Operators on lists

Prelude> [3,1,3] ++ [3,7]
[3,1,3,3,7]
Prelude> 1 : [2.3]
[1,2,3]

  • Strings and characters

Prelude> "Hello, Haskell!!"
"Hello, Haskell!!"
Prelude> putStrLn "Here's a newline -->\n<-- See?"
Here's a newline --><-- See?
Prelude> 'a'
'a'

    • 文字列は文字のリスト (a text string is simply a list of individual characters.)

Prelude> 'a':"bc"
"abc"
Prelude> "" == []
True

  • 型名は大文字で始まる
  • 変数名は小文字で始まる

型を表示してみる。

Prelude> :set +t
Prelude> 'c'
'c'
it :: Char
Prelude> :unset +t
Prelude> :type 'a'
'a' :: Char

it は (Haskell でなく) ghci の特殊変数

  • Integer のサイズはメモリ容量で制限される

参考