【问题标题】:Helper functions, giving a parse error辅助函数,给出解析错误
【发布时间】:2013-02-24 01:08:02
【问题描述】:

我是 Haskell 的新手。在下面的示例中,该函数从列表中删除所有出现的特定元素并返回一个新列表。此外,我正在尝试使用辅助函数来获取返回的列表并输出它的长度。

我遇到的问题是编译时出现解析错误,指向包含delete _ [] = [] 的行。

感谢您在找出错误原因方面的任何帮助。

countDelete y (x:xs) = length outputList
    where outputList = delete y (x:xs)

    delete _ [] = []
    delete y (x:xs)  |  x==y = delete y xs
                     |  otherwise = x:delete y xs

【问题讨论】:

    标签: haskell


    【解决方案1】:

    where 子句中的所有绑定必须从同一列开始,

    countDelete y (x:xs) = length outputList
      where
        outputList = delete y (x:xs)
    
        delete _ [] = []
        delete y (x:xs)
            |  x==y      = delete y xs
            |  otherwise = x:delete y xs
    

    有效。

    【讨论】:

    • 感谢您的回复。所以这基本上是由于缩进/间距错误?
    • @MiGusta 是的。 outputList 中的 odelete 中的 d 必须对齐。
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 2013-05-29
    • 2012-05-19
    • 1970-01-01
    • 2012-01-22
    • 1970-01-01
    • 2011-10-17
    • 1970-01-01
    相关资源
    最近更新 更多