8. ◎ module Fooにhogeとbarがあるとき
宣言 インポートされるもの 参照の方法
import Foo hoge, bar hoge, bar
Foo.hoge, Foo.bar
import Foo () (なし)
import Foo (hoge) hoge hoge, Foo.hoge
import Foo hiding (hoge) bar bar, Foo.bar
import qualified Foo hoge, bar Foo.hoge, Foo.bar
import Foo as F hoge, bar hoge, bar
F.hoge, F.bar
import qualified Foo as F hoge, bar F.hoge, F.bar
12. ◎ 単語の個数を調べる
ghci> :t wordNums
wordNums :: String -> [(String,Int)]
ghci> wordNums "a haskell learn haskell a a learn good"
[("a",3),("good",1),("haskell",2),("learn",2)]
13. ◎ 文字列を空白で区切られた文字列のリストに変換する
ghci> :t words
words :: String -> [String]
ghci> words “hey these are the words”
[“hey”, “these”, “are”, “the”, “words”]
ghci> words “hey these are the words”
[“hey”, “these”, “are”, “the”, “words”]
14. ◎ リストの同じ要素をグループ化する
ghci> :t group
group :: Eq a => [a] -> [[a]]
ghci> group [1,1,1,2,2,2,3,3,2,2,5,7]
[[1,1,1],[2,2,2],[3,3],[2,2],[5],[7]]
ghci> group [“hoge”, “var”, “var”, “hoge”, “hoge”]
[[“hoge”], [“var”, “var”], [“hoge”, “hoge”]]
47. ◎ 先頭にモジュール宣言を書く
-- Foo.hs
module Foo where
◦ モジュール名とファイル名は同じにしなければならない
◎ モジュールの中身を
◦ 全て公開したい(エクスポート)
module Foo where
◦ 一部だけ公開したい
◦ module Foo (hoge, var) where
48. ◎ test1/Nums.hs
module Nums where
double x = x + x
quadruple x = double x + double x
◎ test1/Main.hs
import Nums
double2 = Nums.double 2
quadruple4 = Nums.quadruple 4
49. ◎ test1/Nums.hs
module Nums (double) where
double x = x + x
quadruple x = double x + double x
◎ test1/Main.hs
import Nums
double2 = Nums.double 2 -- OK
quadruple4 = Nums.quadruple 4 -- コンパイルエラー