Instantly share code, notes, and snippets.

bpk-t/TFWH_1.6_2.md

Created March 7, 2017 14:29
Show Gist options
• Save bpk-t/008e2641f87866e2d53ab7bb04ecba93 to your computer and use it in GitHub Desktop.

練習問題E

``````Prelude> 1 + (2 + 3)
6
Prelude> (1 + 2) + 3
6
Prelude> [1] ++ ([2] ++ [3])
[1,2,3]
Prelude> ([1] ++ [2]) ++ [3]
[1,2,3]
Prelude> 1 - (2 - 3)
2
Prelude> (1 - 2) -3
-4
``````
• 加法の単位元→0
• 連接の単位元→[]
• 関数合成の単位元→id

練習問題G

``````Prelude> :m Data.Char
Prelude Data.Char> let nums = ["Two", "Three", "Four", "Five", "Six", "Seven", "Eight", "Nine"]
Prelude Data.Char> :{
Prelude Data.Char| count 0 = ""
Prelude Data.Char| count n = map toLower (nums !! (n - 1)) ++ " men, " ++ count (n - 1)
Prelude Data.Char| :}
Prelude Data.Char> line2 = "Went to mow a meadow\n"
Prelude Data.Char> line4 = line2 ++ "\n"
Prelude Data.Char> :{
Prelude Data.Char| line3 1 = "One man add his dog\n"
Prelude Data.Char| line3 n = nums !! (n - 2) ++ " men " ++ count (n - 2) ++ "One man and his dog\n"
Prelude Data.Char| :}
Prelude Data.Char> :{
Prelude Data.Char| line1 1 = "One man went to mow\n"
Prelude Data.Char| line1 n = nums !! (n - 2) ++ " men went to mow\n"
Prelude Data.Char| :}
Prelude Data.Char> verse n = line1 n ++ line2 ++ line3 n ++ line4
Prelude Data.Char> :{
Prelude Data.Char| song 0 = ""
Prelude Data.Char| song n = song (n - 1) ++ "\n" ++ verse n
Prelude Data.Char| :}

Prelude Data.Char> putStr (song 3)

One man went to mow

Two men went to mow