Haskell 中的解析错误

发布于 2024-10-05 03:49:07 字数 245 浏览 1 评论 0原文

以下代码:

import IO
import System(getArgs)

main = do
    args <- getArgs
    let l = length args
    if l == 0
        putStrLn "foo"
    else
        putStrLn "bar"

生成 if-else 子句的解析错误。我尝试过使用大括号但没有成功。 帮助!

The following code:

import IO
import System(getArgs)

main = do
    args <- getArgs
    let l = length args
    if l == 0
        putStrLn "foo"
    else
        putStrLn "bar"

generates a parse error for the if-else clause. I have tried using curly braces to no avail.
Help!

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(2

别靠近我心 2024-10-12 03:49:07

只是为了证明我对马克的回答的评论

import System.Environment (getArgs)

main :: IO ()
main = do
    args <- getArgs
    let l = length args
    if l == 0
      then putStrLn "foo"
      else putStrLn "bar"

是合法的哈斯克尔。


使用 GHC 7.0 的 {-# LANGUAGE RebindableSyntax #-}< /a> 扩展名,你甚至可以逃脱

class IfThenElse a b c d | a b c -> d where
    ifThenElse :: a -> b -> c -> d
instance IfThenElse Bool a a a where
    ifThenElse True = const
    ifThenElse False = flip const
instance (Monad m, IfThenElse a (m b) (m b) (m b))
      => IfThenElse (m a) (m b) (m b) (m b) where
    ifThenElse = liftM ifThenElse

main =
    if liftM null getArgs
      then putStrLn "foo"
      else putStrLn "bar"

(无耻地模仿 blog.n-sch.de。)

Just to demonstrate my comment to Mark's answer,

import System.Environment (getArgs)

main :: IO ()
main = do
    args <- getArgs
    let l = length args
    if l == 0
      then putStrLn "foo"
      else putStrLn "bar"

is legal Haskell.


With GHC 7.0's {-# LANGUAGE RebindableSyntax #-} extension, you can even get away with

class IfThenElse a b c d | a b c -> d where
    ifThenElse :: a -> b -> c -> d
instance IfThenElse Bool a a a where
    ifThenElse True = const
    ifThenElse False = flip const
instance (Monad m, IfThenElse a (m b) (m b) (m b))
      => IfThenElse (m a) (m b) (m b) (m b) where
    ifThenElse = liftM ifThenElse

main =
    if liftM null getArgs
      then putStrLn "foo"
      else putStrLn "bar"

(Shamelessly aped from blog.n-sch.de.)

孤君无依 2024-10-12 03:49:07

两个问题:

  1. 您缺少 let-in 子句(参见(并接受)@ephemient 的回答)

  2. 你错过了then if-then-else 子句的一部分

所以它可能看起来像:

import IO import System(getArgs)

main = do
    args <- getArgs
    let l = length args in
        if l == 0 then
            putStrLn "foo"
        else
            putStrLn "bar"

Two issues:

  1. You're missing the in part of the let-in clause (See (and accept) @ephemient's answer)

  2. You're missing the then part of the if-then-else clause

So it could look like:

import IO import System(getArgs)

main = do
    args <- getArgs
    let l = length args in
        if l == 0 then
            putStrLn "foo"
        else
            putStrLn "bar"
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文