首页 > 解决方案 > Haskell 中的 splitList 函数

问题描述

在我的作业中,有一个问题,我复制在下面。

编写一个函数 splitList ,它提供了将至少两个元素的列表拆分为两个非空部分的所有方法。类型声明为:

splitList :: [a] -> [([a],[a])]

电话splitList [1..4]应给出:

Prelude> splitList [1..4]
[([1],[2,3,4]),([1,2],[3,4]),([1,2,3],[4])] 

这是我写的代码:

splitList :: Eq a=> [a]->[([a],[a])]
splitList []=[([],[])]
splitList (x:xs) = [([x],xs)]++[(splitList xs)] 

但它给出了一个错误:Couldn't match expected type `([a], [a])' with actual type `[([a], [a])]'

splitList (x:xs) = [([x],xs)]++[(splitList xs)]

我在错误突出显示的地方加粗。

标签: algorithmhaskell

解决方案



推荐阅读