2016年8月2日火曜日

開発環境

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

セルフスタディ2日目.

コード(Emacs)

module Main where
import Data.Char

myGcd x 0 = x
myGcd x y = if mod x y == 0
            then y
            else gcd y (mod x y)

primeFilter (x:xs) = x:(primeFilter (filter (\y -> mod y x /= 0) xs))
primes = primeFilter [2..]

delimiters = " \t\n,."

width = 50
stringToLines1 "" "" _  line _ i lines =
  lines ++ ('\n':((show i) ++ ':':line))
  
stringToLines1 "" word n line m i lines =
  if n + m < width
  then lines ++ '\n':((show i) ++ ':':line ++ word)
  else lines ++ '\n':((show i) ++ ':':line ++ '\n':(show (i + 1)) ++ ':':word)
stringToLines1 (x:xs) word n line m i lines = 
  if elem x delimiters
  then if n + 1 + m < width
       then stringToLines1 xs "" 0 (line ++ word ++ [x]) (n + 1 + m) i lines
       else stringToLines1 xs "" 0 (word ++ [x]) (n + 1) (i + 1)
            (if (null lines)
             then (show i) ++ ':':line
             else (lines ++ '\n':(show i) ++ ':':line))
  else stringToLines1 xs (word ++ [x]) (n + 1) line m i lines

stringToLines s = stringToLines1 s "" 0 "" 0 1 ""

s = concat
    ["Beautiful is better than ugly.",
     "Explicit is better than implicit.",
     "Simple is better than complex.",
     "Complex is better than complicated.",
     "Flat is better than nested.",
     "Sparse is better than dense.",
     "Readability counts.",
     "Special cases aren't special enough to break the rules.",
     "Although practicality beats purity.",
     "Errors should never pass silently.",
     "Unless explicitly silenced.",
     "In the face of ambiguity, refuse the temptation to guess.",
     "There should be one-- and preferably only one --obvious way to do it.",
     "Although that way may not be obvious at first unless you're Dutch.",
     "Now is better than never.",
     "Although never is often better than *right* now.",
     "If the implementation is hard to explain, it's a bad idea.",
     "If the implementation is easy to explain, it may be a good idea.",
     "Namespaces are one honking great idea -- let's do more of those!\n"]

入出力結果(GHCI, Terminal, REPL(Read, Eval, Print, Loop))

$ ghci
GHCi, version 7.8.3: http://www.haskell.org/ghc/  :? for help
Loading package ghc-prim ... linking ... done.
Loading package integer-gmp ... linking ... done.
Loading package base ... linking ... done.
Prelude> :load sample2
[1 of 1] Compiling Main             ( sample2.hs, interpreted )
Ok, modules loaded: Main.
*Main> myGcd 10 0
10
*Main> myGcd 0 10
10
*Main> myGcd 12345 54321
3
*Main> myGcd 4 10
2
*Main> myGcd 10 4
2
*Main> take 10 primes
[2,3,5,7,11,13,17,19,23,29]
*Main> take 100 primes
[2,3,5,7,11,13,17,19,23,29,31,37,41,43,47,53,59,61,67,71,73,79,83,89,97,101,103,107,109,113,127,131,137,139,149,151,157,163,167,173,179,181,191,193,197,199,211,223,227,229,233,239,241,251,257,263,269,271,277,281,283,293,307,311,313,317,331,337,347,349,353,359,367,373,379,383,389,397,401,409,419,421,431,433,439,443,449,457,461,463,467,479,487,491,499,503,509,521,523,541]
*Main> putStr (stringToLines s)
1:Beautiful is better than ugly.Explicit is better 
2:than implicit.Simple is better than complex.
3:Complex is better than complicated.Flat is 
4:better than nested.Sparse is better than dense.
5:Readability counts.Special cases aren't special 
6:enough to break the rules.Although practicality 
7:beats purity.Errors should never pass silently.
8:Unless explicitly silenced.In the face of 
9:ambiguity, refuse the temptation to guess.There 
10:should be one-- and preferably only one 
11:--obvious way to do it.Although that way may not 
12:be obvious at first unless you're Dutch.Now is 
13:better than never.Although never is often better 
14:than *right* now.If the implementation is hard 
15:to explain, it's a bad idea.If the 
16:implementation is easy to explain, it may be a 
17:good idea.Namespaces are one honking great idea 
18:-- let's do more of those!
*Main> putStr $ stringToLines s
1:Beautiful is better than ugly.Explicit is better 
2:than implicit.Simple is better than complex.
3:Complex is better than complicated.Flat is 
4:better than nested.Sparse is better than dense.
5:Readability counts.Special cases aren't special 
6:enough to break the rules.Although practicality 
7:beats purity.Errors should never pass silently.
8:Unless explicitly silenced.In the face of 
9:ambiguity, refuse the temptation to guess.There 
10:should be one-- and preferably only one 
11:--obvious way to do it.Although that way may not 
12:be obvious at first unless you're Dutch.Now is 
13:better than never.Although never is often better 
14:than *right* now.If the implementation is hard 
15:to explain, it's a bad idea.If the 
16:implementation is easy to explain, it may be a 
17:good idea.Namespaces are one honking great idea 
18:-- let's do more of those!
*Main> :q
Leaving GHCi.
$

0 コメント:

コメントを投稿