takkan_mのHaskell再挑戦 RSSフィード

2006-12-03

問題7.10 20:47 問題7.10 - takkan_mのHaskell再挑戦 を含むブックマーク はてなブックマーク - 問題7.10 - takkan_mのHaskell再挑戦 問題7.10 - takkan_mのHaskell再挑戦 のブックマークコメント

以下のパターンに対し指定されたメッセージを返すプログラム

___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しなきゃいかんのかなぁ。

RuthRuth 2012/10/02 19:25 If information were soccer, this would be a goooaool!

dheezzxhxdheezzxhx 2012/10/03 19:05 1ryJyU <a href="http://bkdwfjybvagi.com/">bkdwfjybvagi</a>

bzocpomgsbzocpomgs 2012/10/03 23:44 ljjkyP , [url=http://wnntjpqfzonh.com/]wnntjpqfzonh[/url], [link=http://qugurvyhrowf.com/]qugurvyhrowf[/link], http://yzoelxmdycmu.com/

kdrtcmfjkdrtcmfj 2012/10/04 12:29 QDIiks <a href="http://whjqvwxmhmip.com/">whjqvwxmhmip</a>

udurcznpfvcudurcznpfvc 2012/10/06 13:38 FLDc3p , [url=http://nsactwwocurn.com/]nsactwwocurn[/url], [link=http://gtyfgqysdjja.com/]gtyfgqysdjja[/link], http://kxgyzozhfoqj.com/

ゲスト



トラックバック - http://haskell.g.hatena.ne.jp/takkan_m/20061203