## 2016年8月1日月曜日

７つの言語 ７つの世界 (Bruce A. Tate (著)、まつもとゆきひろ (監訳)、田和 勝 (翻訳)、オーム社)の第8章(Haskell)、8.3(2日目: スポックの大きな強み)、セルフスタディ2日目を取り組んでみる。

セルフスタディ2日目.

コード(Emacs)

``` module Main where import Data.Char l1 = [5, 1, 4, 2, 3] l2 = l1 ++ l1 sort [] = [] sort (x:xs) = sort (filter (\y -> y < x) xs) ++ [x] ++ sort (filter (\y -> x <= y) xs) sort1 [] _ = [] sort1 (x:xs) cmp = sort1 (filter (\y -> cmp y x) xs) cmp ++ [x] ++ sort1 (filter (\y -> not (cmp y x)) xs) cmp stringToNumber s = read (filter (\ch -> ch /= ',' && ch /= '\$') s) :: Float delaySeqSkip2 x = [x, x + 2..] delaySeqSkip4 x = [x, x + 4..] delaySeqSkip8 x y = zipWith (+) (zipWith (+) (delaySeqSkip2 x) (delaySeqSkip4 y)) [0, 2..] half = (/ 2) newline = (++ "\n") ```

```\$ ghci
GHCi, version 7.8.3: http://www.haskell.org/ghc/  :? for help
[1 of 1] Compiling Main             ( sample1.hs, interpreted )
*Main> sort l1
[1,2,3,4,5]
*Main> sort l2
[1,1,2,2,3,3,4,4,5,5]
*Main> sort1 l1 (<)
[1,2,3,4,5]
*Main> sort1 l1 (<=)
[1,2,3,4,5]
*Main> sort1 l1 (>)
[5,4,3,2,1]
*Main> sort1 l1 (>=)
[5,4,3,2,1]
*Main> sort1 l2 (<)
[1,1,2,2,3,3,4,4,5,5]
*Main> sort1 l2 (<=)
[1,1,2,2,3,3,4,4,5,5]
*Main> sort1 l2 (>)
[5,5,4,4,3,3,2,2,1,1]
*Main> sort1 l2 (>=)
[5,5,4,4,3,3,2,2,1,1]
*Main> stringToNumber "012345"
12345.0
*Main> stringToNumber "\$2,345,678.99"
2345679.0
*Main> take 5 (delaySeqSkip2 10)
[10,12,14,16,18]
*Main> take 5 (delaySeqSkip4 10)
[10,14,18,22,26]
*Main> take 5 (delaySeqSkip8 4 6)
[10,18,26,34,42]
*Main> half 10
5.0