「48時間でSchemeを書こう/練習問題の解答」の版間の差分

削除された内容 追加された内容
マークアップの改善
1 行
= 最初の一歩練習問題1 =
 
</ol>
== 二つのコマンドライン引数を取る ==
<li>
<syntaxhighlight lang="haskell">
import System
9 ⟶ 10行目:
putStrLn ("Hello, " ++ args !! 0 ++ " " ++ args !! 1)
</syntaxhighlight>
</li>
 
<li>
== コマンドライン引数と簡単な計算 ==
<syntaxhighlight lang="haskell">
import System
20 ⟶ 22行目:
 
<code>$</code>演算子によって括弧の数を減らしています。最後の行は<code>$</code>を使わずに<code>print ((read (args !! 0)) + (read $ args !! 1))</code>とも書けます。
</li>
 
<li>
== 名前のエコー ==
<syntaxhighlight lang="haskell">
import System
30 ⟶ 33行目:
putStrLn $ "あなたの名前は" ++ name ++ "ですね!"
</syntaxhighlight>
</li>
</ol>
 
= 練習問題2 =
51 ⟶ 56行目:
</ol>
</li>
</ol>
 
=== Exercise 2 ===
 
<li>
We need to create a new parser action that accepts a backslash followed by either another backslash or a doublequote. This action needs to return only the second character.
 
<syntaxhighlight lang="haskell">
escapedChars :: Parser Char
escapedChars :: Parser Char
escapedChars = do char '\\' -- a backslash
x <- oneOf "\\\"" -- either backslash or doublequote
return x -- return the escaped character
</syntaxhighlight>
 
Once that is done, we need to make some changes to parseString.
69 ⟶ 74行目:
char '"'
return $ String x
</li>
 
<li>
=== Exercise 3 ===
 
escapedChars :: Parser Char
escapedChars = do char '\\'
81 ⟶ 86行目:
'r' -> '\r'
't' -> '\t'
</li>
 
<li>
=== Exercise 4 ===
 
First, it is necessary to change the definition of symbol.
 
147 ⟶ 152行目:
bin2dig' old xs
 
</li>
=== Exercise 5 ===
 
<li>
data LispVal = Atom String
| List [LispVal]
178 ⟶ 184行目:
<|> try parseBool -- these can all start with the hash char
<|> try parseCharacter
</li>
 
<li>
=== Exercise 6 ===
 
A possible solution for floating point numbers:
 
193 ⟶ 199行目:
| Float Double
to the LispVal type.
</li>
 
<li>
=== Exercise 7 ===
 
'''Ratio''', using Haskell's Ratio type:
 
229 ⟶ 235行目:
| Complex (Complex Double)
to the LispVal type.
</li>
</ol>
 
==Section 4 - Recursive Parsers: Adding lists, dotted lists, and quoted datums==