計算機科学のブログ

Haskellを使用するための準備 GHCi、計算、ファイルの読み込み、コンパイル、実行可能ファイル、名前

入門Haskellプログラミング (Will Kurt(著)、株式会社クイープ(監修、翻訳)、翔泳社)のLESSON 1(Haskellを使用するための準備)、1.6(練習問題)のQ1.1、Q1.2の解答を求めてみる。

コード

main :: IO ()
main = do
    print "Who is the email for?"
    recipient <- getLine 
    print "What is the Title?"
    title <- getLine
    print "Who is the Author?"
    author <- getLine
    print (createEmail recipient title author)

toPart :: [Char] -> [Char]
toPart recipient = "Dear " ++ recipient ++ ",\n"

bodyPart :: [Char] -> [Char]
bodyPart bookTitle = "Thanks for buying " ++ bookTitle ++ "\n"

fromPart :: [Char] -> [Char]
fromPart author = "thanks, \n" ++ author

createEmail :: [Char] -> [Char] -> [Char] -> [Char]
createEmail recipient bookTitle author = toPart recipient ++
  bodyPart bookTitle ++
  fromPart author

入出力結果(Terminal, Zsh)

% ghci
GHCi, version 8.10.4: https://www.haskell.org/ghc/  :? for help
macro 'doc' overwrites builtin command.  Use ':def!' to overwrite.
(0.00 secs, 0 bytes)
(0.00 secs, 0 bytes)
Loaded GHCi configuration from /.../.ghc/ghci.conf
Prelude
λ> 2 ** 123
1.0633823966279327e37
it :: Floating a => a
(0.05 secs, 93,472 bytes)
Prelude
λ> :l first_prog.hs
[1 of 1] Compiling Main             ( first_prog.hs, interpreted )

first_prog.hs:14:5: error:
    parse error (possibly incorrect indentation or mismatched brackets)
   |
14 |     )
   |     ^
Failed, no modules loaded.
(0.00 secs,)
Prelude
λ> :l first_prog.hs
[1 of 1] Compiling Main             ( first_prog.hs, interpreted )
Ok, one module loaded.
(0.05 secs,)
*Main
λ> me
mempty     messyMain
λ> messyMain 
"Whjo is the emal for?"
Happy Reader
"What is the Title?"
Learn Haskell
"Who is the Author?"
Will Kurt
"Dear Happy Reader,\nThanks for buying Learn Haskell\nthanks, \nWill Kurt"
it :: ()
(17.39 secs, 262,080 bytes)
*Main
λ> :l first_prog.hs
[1 of 1] Compiling Main             ( first_prog.hs, interpreted )
Ok, one module loaded.
(0.00 secs,)
*Main
λ> toPart "Happy Reader"
"Dear Happy Reader,\n"
it :: [Char]
(0.00 secs, 78,400 bytes)
*Main
λ> :l first_prog.hs
[1 of 1] Compiling Main             ( first_prog.hs, interpreted )
Ok, one module loaded.
(0.00 secs,)
*Main
λ> bodyPart "Learn Haskell"
"Thanks for buying Learn Haskell\n"
it :: [Char]
(0.00 secs, 90,104 bytes)
*Main
λ> fromPart "Will Kurt"
"thanks, \nWill Kurt"
it :: [Char]
(0.00 secs, 77,040 bytes)
*Main
λ> :l first_prog.hs
[1 of 1] Compiling Main             ( first_prog.hs, interpreted )
Ok, one module loaded.
(0.00 secs,)
*Main
λ> createEmail "Happy Reader" "Learn Haskell" "Will Kurt"
"Dear Happy Reader,\nThanks for buying Learn Haskell\nthanks, \nWill Kurt"
it :: [Char]
(0.00 secs, 127,688 bytes)
*Main
λ> :l first_prog.hs
[1 of 1] Compiling Main             ( first_prog.hs, interpreted )
Ok, one module loaded.
(0.01 secs,)
*Main
λ> main
"Who is the emal for?"
Happy Reader
"What is the Title?"
Learn Haskell
"Who is the Author?"
Will Kurt
"Dear Happy Reader,\nThanks for buying Learn Haskell\nthanks, \nWill Kurt"
it :: ()
(21.03 secs, 263,936 bytes)
*Main
λ> :q
Leaving GHCi.
% ghc first_prog.hs -o email
[1 of 1] Compiling Main             ( first_prog.hs, first_prog.o )
Linking email ...
% ./email 
"Who is the emal for?"
Happy Reader
"What is the Title?"
Learn Haskell
"Who is the Author?"
^C
% ghc first_prog.hs -o email
[1 of 1] Compiling Main             ( first_prog.hs, first_prog.o )
Linking email ...
% ./email                   
"Who is the email for?"
Happy Reader
"What is the Title?"
Learn Haskell
"Who is the Author?"
Will Kurt
"Dear Happy Reader,\nThanks for buying Learn Haskell\nthanks, \nWill Kurt"
%