2006-12-03
■ 問題7.10

___1 my single ___2 me ___3
-> tell me about your single
___1 i am ____2
-> i am sorry to hear you are ___2
___1 am i ___2
-> do you believe you are ___2
___1 you ___2 me
-> why do you think i ___2 you
___1
-> in what way
で、こんな感じに。
getWhyMessage mes = "why do you think i " ++ mes ++ " you" getDoYouMessage mes = "do you believe you are " ++ mes getSorryMessage mes = "i am sorry to hear you are " ++ mes getTellMeMessage mes = (++) "tell me about your" $ getPart "" mes where getPart mes (x:xs) = if x == "me" then mes else getPart (unwords [mes, x] ) xs getMessage [] = return "in what way" getMessage (x:xs) | x == "my" = if elem "me" xs then return $ getTellMeMessage xs else getMessage [] | x == "i" = if head xs == "am" then return $ getSorryMessage $ unwords $ tail xs else getMessage [] | x == "am" = if head xs == "i" then return $ getDoYouMessage $ unwords $ tail xs else getMessage [] | x == "you" = if last xs == "me" then return $ getWhyMessage $ unwords $ init xs else getMessage [] | otherwise = getMessage xs echoLines = do line <- getLine if line == "exit" then return "" else do message <- getMessage $ words line putStrLn message echoLines return "" main = do echoLines
$ ./main well my friend made me come here tell me about your friend made he says i am depressed i am sorry to hear you are depressed i think i need help in what way oh am i making sense so far do you believe you are making sense so far you are making fun of me why do you think i are making fun of you
ホントは、入力のところに">"のようにプロンプトをだしたかったんだけど、getLineの前にputStrをおいても出力されるのが、応答メッセージのあとになっちゃったんだよね。flushしなきゃいかんのかなぁ。
コメントを書く