小编ADW*_*ADW的帖子

我的函数返回的最后一个列表有错误

我刚刚开始学习Haskell,我打算编写一个返回到最后一个列表2的函数。

lastButOne x = if ((==) (length x) 2)
               then (head x)
               else (tail x)
Run Code Online (Sandbox Code Playgroud)

这是错误的:

    ? Occurs check: cannot construct the infinite type: a ~ [a]
    ? In the expression: (tail x)
      In the expression:
        if ((==) (length x) 2) then (head x) else (tail x)
      In an equation for ‘lastButOne’:
          lastButOne x = if ((==) (length x) 2) then (head x) else (tail x)
    ? Relevant bindings include
        x :: [a] (bound at D:\\tool\8.6.3\test\lBoErr.hs:1:12)
        lastButOne :: [a] -> …
Run Code Online (Sandbox Code Playgroud)

haskell

2
推荐指数
1
解决办法
45
查看次数

标签 统计

haskell ×1