Real World Haskell Capter 1. Getting Started 読んだ
Basic interaction: using ghci as a calculator
--0 を False それ以外を True のようなものとして扱わない 1 && True {- <interactive>:0:1: No instance for (Num Bool) arising from the literal `1' Possible fix: add an instance declaration for (Num Bool) In the first argument of `(&&)', namely `1' In the expression: 1 && True In an equation for `it': it = 1 && True -} -- not equal 2 /= 3 -- => True not True -- => True --演算子の優先順位を調べる :info (*) -- [infixr|infixl] n * と出て, nの値が高い方が, 優先度が高い. ただこれ知ってても人間馬鹿だし, 括弧書いてたほうがいい時もあるよね ※ [右|左]結合演算子は[infixr|infixl] {- class (Eq a, Show a) => Num a where ... (*) :: a -> a -> a ... -- Defined in GHC.Num infixl 7 * -} -- 変数に値を束縛する let e = exp 1 -- 破壊的変更はできない
Lists
-- 全ての要素の型は同じでないといけない [1, 2, 3] ["foo", "bar"] [1..5] -- => [1, 2, 3, 4, 5] [1.0,1.25..2.0] -- => [1.0, 1.25, 1.5, 1.75, 2.0] [1,4..15] -- => [1, 4, 7, 10, 13] [5,4..1] -- => [5, 4, 3, 2, 1] [1..] のように書くと無限にリストを作る [1..n] の n が小数の場合 1 ~ n + 0.5 を列挙する -- 連結 [1, 2] ++ [2, 4] -- => [1, 2, 2, 4] [] ++ [False, True] ++ [True] -- => [False, True, True] -- リストの先頭に要素を追加 1: [2, 3] -- => [1, 2, 3]
Strings and characters
--1文字と2以上の文字列を区別する, 1文字はシングルクオーテーションで囲まれる(囲める?) "aa" -- => aa 'a' -- => 'a' --文字列は単に個々の文字のリスト ['h', 'e', 'l', 'l', 'o'] == "hello" -- => True "" == [] -- => True -- 連結 'a':"bc" -- => "abc" "foo" ++ "bar" -- => "foobar"
First steps with types
--型の情報を続いて表示する (:unset +t で元に戻る) :set +t 'a' {- 'a' it :: Char -} --型を表示 :type 'a' {- 'a' :: Char -} -- 3 + 2 という式を評価するとInteger, 評価するまでは numericであるということしか分からない :type 3 + 2 {- 3 + 2 :: (Num t) => t -}
まとめ
難しくない英語だけど, 内容が難しくなってきたら意味分からんことになりそうで日本語の書籍も持っておきたい.
ちなみにghciの設定ファイルは ~/.ghci みたい