Haskell:分割函数中的错误

Haskell:分割函数中的错误,haskell,Haskell,我是哈斯克尔的新手。我想实现split函数,它将列表拆分为两部分: split 2 [1,2,3] → ([1,2], [3]) --means the first part has length 2, the second - length x-2 split 2 [1] → ([1], []) split :: Int -> [a] -> ([a],[a]) split 0 x = ([], x) split n x = splitH n x [] splitH :: In

我是哈斯克尔的新手。我想实现split函数,它将列表拆分为两部分:

split 2 [1,2,3] → ([1,2], [3]) --means the first part has length 2, the second - length x-2
split 2 [1] → ([1], [])


split :: Int -> [a] -> ([a],[a])
split 0 x = ([], x)
split n x = splitH n x []

splitH :: Int -> [a] -> [a] -> ([a], [a])
splitH n (x:xs) begin | n == 0 = (begin, xs)
                      | otherwise = splitH n-1 xs (x:begin) -- here is the error

    main = print(split 2 [1,2,3] )
但这段代码并不编译。我犯了一个错误

`Couldn't match expected type ‘([a], [a])’
            with actual type ‘[a0] -> [a0] -> ([a0], [a0])’
Relevant bindings include
  begin :: [a] (bound at jdoodle.hs:6:17)
  xs :: [a] (bound at jdoodle.hs:6:13)
  x :: a (bound at jdoodle.hs:6:11)
  splitH :: Int -> [a] -> [a] -> ([a], [a]) (bound at jdoodle.hs:6:1)
Probable cause: ‘splitH’ is applied to too few arguments
In the first argument of ‘(-)’, namely ‘splitH n’
In the expression: splitH n - 1 xs (x : begin)`

什么会导致错误

用括号括住表达式
n-1

splitH (n-1) xs (x:begin)
请参阅第7节“函数应用程序优先于运算符”,了解以下说明:

所以如果你看到这样的情况:

f a b c+g d e

您知道,您正在添加两个函数调用的结果,而不是调用一个参数为两项之和的函数


在表达式
n-1
周围加括号:

splitH (n-1) xs (x:begin)
请参阅第7节“函数应用程序优先于运算符”,了解以下说明:

所以如果你看到这样的情况:

f a b c+g d e

您知道,您正在添加两个函数调用的结果,而不是调用一个参数为两项之和的函数


对于比较,您可以查看的标准实现。对于比较,您可以查看的标准实现