「ed」のような非常に単純なエディターを作成しようとしています。このプログラムでは、実行するアクションで文字列コマンドを変換するコントロールを構築するためのマッピングを使用しようとしています。コードの一部を次に示します。
commands :: Map String ([Handle] -> IO ())
commands = fromAscList [
("o",\list -> print "Insert name of the file to be opened" >> getLine >>= \nomefile ->
openFile nomefile ReadWriteMode >>= \handle -> editor (handle:list)),
("i",\list -> case list of { [] -> print "No buffer open" ; handle:res -> write handle } >> editor list),
("q",\list -> if list == [] then return () else mapM_ hClose list >> return ())
]
editor :: [Handle] -> IO()
editor list = do
command <- getLine
let action = lookup command commands
case action of
Nothing -> print "Unknown command" >> editor list
Just act -> act list
問題は、ghci または実行可能ファイルでエディター機能を実行するときに、「o」と入力すると、ファイルを開く関数の呼び出しではなく、「不明なコマンド」というメッセージが表示されることです。Map の代わりに連想リストを使用して同じコードを試しましたが、この場合は機能します。では、ここで何が問題になるのでしょうか?
さらに奇妙なのは、ghci のマッピング コマンドでキーを呼び出すと、文字列 "o" を含むリストも返されることです。
助けてくれてありがとう。