私は、ホスト名に基づいてリクエストをルーティングする簡単な方法を構築しようとしています。ここでは関数は次のとおりです。haskell HTTPサーバでルーティングを実装する
ここに呼び出されhandleAccept :: Handle -> String -> IO()
handleAccept handle hostname = do
putStrLn $ "Handling request from " ++ (getMessage hostname)
request <- fmap (parseRequest . lines) (hGetContents handle)
respond request handle
return()
where getMessage x
| x == "hello" = "hello route"
| x == "LOL" = "what's so funny?"
| otherwise = x
:
main = withSocketsDo $ do
sock <- listenOn (PortNumber 9000)
putStrLn "Listening on port 9000"
forever $ do
(handle, hostname, port) <- accept sock
handleAccept handle hostname
hClose handle
私がコンパイルしようとすると、私はこのエラーを取得:handleAccept
にこの行で
parse error (possibly incorrect indentation or mismatched brackets)
:
where getMessage x
| x == "hello" = "hello route"
あるようですどこのステートメントでガードを使用して問題になるのですか?だから私はこのダミー関数を試しました:
wherePlusGuards :: String -> Bool
wherePlusGuards x = getX x
where getX x
| x == "hello" = True
| otherwise = False
これはうまくコンパイルされます。私は、問題がどこのステートメントと警備員の内部でのdo
の式を使用することから来ていると信じて残されています。これはなぜですか?私を助けてください。
なぜその問題でしょうか?パーサが探しているインデント数は何ですか? – dopatraman
答えが更新されました - 'wherePlusGuards'の解析が大丈夫ですか?それは私のために解析されません。 – ErikR
Haskellレポートのリファレンスが追加されました。興味深いのは – ErikR