代码之家  ›  专栏  ›  技术社区  ›  Hynek -Pichi- Vychodil Paulo Suassuna

where子句中的Haskell解析器错误

  •  5
  • Hynek -Pichi- Vychodil Paulo Suassuna  · 技术社区  · 17 年前

    你怎么了 rs

    palindrome :: [a] -> [a]
    
    palindrome xs = con xs rs
        where con a b = rev (rev a []) b
            rs = rev xs                        -- here
            where rev [] rs = rs
                rev (x:xs) rs = rev xs (x:rs)
    

    我只是在学习Haskell,但它的语法规则让我困惑。错误消息是

    [1 of 1] Compiling Main             ( pelindrome.hs, interpreted )
    
    pelindrome.hs:5:8: parse error on input `rs'
    
    2 回复  |  直到 14 年前
        1
  •  14
  •   Calibre Johannes Schaub - litb    9 年前

    你的缩进是错误的,我想你只能有一个 where 在那里(我可能大错特错,我不是哈斯凯尔人)。还缺少一个调用的参数 rev

    palindrome :: [a] -> [a]
    palindrome xs = con xs rs
        where con a b = rev (rev a []) b
              rs = rev xs []                       -- here
              rev [] rs = rs
              rev (x:xs) rs = rev xs (x:rs)
    
    main = print (palindrome "hello")
    

    "helloolleh"
    

    我现在要试着去理解它。无论如何,玩得开心!

    编辑:现在对我来说很有意义。我认为这是正确的版本。有关Haskell缩进规则,请阅读 Haskell Indentation

        2
  •  0
  •   Hynek -Pichi- Vychodil Paulo Suassuna    17 年前

    @利特:你可以用这种方式重写con

    palindrome :: [a] -> [a]
    palindrome xs = con xs rs
        where con [] b = b
              con (x:xs) b = x:con xs b
              rs = rev xs []                       -- here
              rev [] rs = rs
              rev (x:xs) rs = rev xs (x:rs)
    

    推荐文章