計算機科学のブログ

Haskellを使用するための準備 - コードの記述と操作

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

コード first_prog.hs

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 9.4.8: 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 /Users/kamimura/.ghc/ghci.conf
Prelude
λ> 2 ** 123
1.0633823966279327e37
it :: Floating a => a
(0.02 secs, 98,280 bytes)
Prelude
λ> :l first_prog.hs 
[1 of 2] Compiling Main             ( first_prog.hs, interpreted )
Ok, one module loaded.
(0.02 secs,)
*Main
λ> main
"Who is the email for?"
reader
"What is the Title?"
haskell
"Who is tho Author?"
kamimura
"Dear reader,\nThanks for buying haskell\nthanks,\nkamimura"
it :: ()
(9.05 secs, 230,680 bytes)
*Main
λ> :l first_prog.hs 
[1 of 2] Compiling Main             ( first_prog.hs, interpreted )
Ok, one module loaded.
(0.01 secs,)
*Main
λ> toPart "reader"
"Dear reader,\n"
it :: [Char]
(0.01 secs, 78,256 bytes)
*Main
λ> :l first_prog.hs 
[1 of 2] Compiling Main             ( first_prog.hs, interpreted )
Ok, one module loaded.
(0.03 secs,)
*Main
λ> bodyPart "haskell"
"Thanks for buying haskell.\n"
it :: [Char]
(0.01 secs, 90,800 bytes)
*Main
λ> :l first_prog.hs 
[1 of 2] Compiling Main             ( first_prog.hs, interpreted )
Ok, one module loaded.
(0.02 secs,)
*Main
λ> main
"Who is the email for?"
reader
"What is the Title?"
haskell
"Who is tho Author?"
kamimura
"Dear reader,\nThanks for buying haskell.\nthanks,\nkamimura"
it :: ()
(15.42 secs, 227,224 bytes)
*Main
λ> :l first_prog.hs 
[1 of 2] Compiling Main             ( first_prog.hs, interpreted )
Ok, one module loaded.
(0.01 secs,)
*Main
λ> fromPart "kamimura"
"Thanks,\nkamimura"
it :: [Char]
(0.00 secs, 80,536 bytes)
*Main
λ> main
"Who is the email for?"
reader
"What is the Title?"
haskell
"Who is the Author?"
kamimura
"Dear reader,\nThanks for buying haskell.\nThanks,\nkamimura"
it :: ()
(6.90 secs, 230,424 bytes)
*Main
λ> :l first_prog.hs 
[1 of 2] Compiling Main             ( first_prog.hs, interpreted )
Ok, one module loaded.
(0.01 secs,)
*Main
λ> createEmail "recipient" "title" "author"
"Dear recipient,\nThanks for buying title.\nThanks,\nauthor"
it :: [Char]
(0.01 secs, 119,960 bytes)
*Main
λ> :l first_prog.hs 
[1 of 2] Compiling Main             ( first_prog.hs, interpreted )
Ok, one module loaded.
(0.01 secs,)
*Main
λ> main
"Who is the email for?"
recipient
"What is the Title?"
title
"Who is the Author?"
author
"Dear recipient,\nThanks for buying title.\nThanks,\nauthor"
it :: ()
(11.78 secs, 227,488 bytes)
*Main
λ> :q
Leaving GHCi.
% ghc first_prog.hs -o email
[2 of 2] Linking email
ld: warning: ignoring duplicate libraries: '-lm'
% ./email 
"Who is the email for?"
reader
"What is the Title?"
haskell
"Who is the Author?"
kamimura
"Dear reader,\nThanks for buying haskell.\nThanks,\nkamimura"
%