2016-07-26 20 views
0

私は、ホスト名に基づいてリクエストをルーティングする簡単な方法を構築しようとしています。ここでは関数は次のとおりです。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の式を使用することから来ていると信じて残されています。これはなぜですか?私を助けてください。

答えて

3

|ggetMessageよりもさらにインデントされていることを確認してください - 例えば:これは正しく解析されない

import System.IO 

handleAccept :: Handle -> String -> IO() 
handleAccept handle hostname = do 
    putStrLn $ "Handling request from " ++ (getMessage hostname) 
    return() 
    where getMessage x 
      | x == "hello" = "hello route" 
      | x == "LOL" = "what's so funny?" 
      | otherwise = x 

更新:

wherePlusGuards :: String -> Bool 
wherePlusGuards x = getX x 
    where getX x 
     | x == "hello" = True 
     | otherwise = False 

は、それがもたらしエラー:parse error (possibly incorrect indentation or mismatched brackets)

注:私はタブを使用していません。 Haskell2010レポートから

Note 1. A nested context must be further indented than the enclosing context (n > m). If not, L fails, and the compiler should indicate a layout error.

参考:https://www.haskell.org/onlinereport/haskell2010/haskellch10.html

10.3(レイアウト)、注1

+0

なぜその問題でしょうか?パーサが探しているインデント数は何ですか? – dopatraman

+0

答えが更新されました - 'wherePlusGuards'の解析が大丈夫ですか?それは私のために解析されません。 – ErikR

+0

Haskellレポートのリファレンスが追加されました。興味深いのは – ErikR

関連する問題