Haskell中空列表的模式匹配

Haskell中空列表的模式匹配,haskell,Haskell,由于我是哈斯克尔的新手,我对每件事都不太熟悉。我尝试使用模式匹配来实现一个函数,但这不起作用,因为它会出现以下异常:Prelude.head empty list。这是代码示例: swapListToTowers::[[Bool]]->[[Bool]] swapListToTowers [] = [] swapListToTowers xs = [head x| x <-xs]:swapListToTowers (cutOfFirstElements xs) cutOfFirst

由于我是哈斯克尔的新手,我对每件事都不太熟悉。我尝试使用模式匹配来实现一个函数,但这不起作用,因为它会出现以下异常:Prelude.head empty list。这是代码示例:

swapListToTowers::[[Bool]]->[[Bool]]
swapListToTowers [] = []
swapListToTowers xs = [head x| x <-xs]:swapListToTowers (cutOfFirstElements xs)


cutOfFirstElements::[[Bool]]->[[Bool]]
cutOfFirstElements [] = []
cutOfFirstElements xs = [tail x | x <- xs]
swaplisttowers::[[Bool]]->[[Bool]]
swapListToTowers[]=[]
swapListToTowers xs=[head x | x[[Bool]]
截止日期要素[]=[]

cutOfFirstElements xs=[tail x | x这两个函数都将列表作为参数列表。空列表匹配
[]
仅匹配外部列表,但不匹配外部列表非空但内部列表不是空的情况;例如
[]]

我认为您缺少与内部列表匹配的模式,因此当您理解列表时,没有检查您的内部列表是否为空

我不确定你的函数是用来做什么的,但是你可能想要像这样拆分你的函数,这样你就可以自己处理空的子列表了

swapListToTowers::[[Bool]]->[[Bool]]
swapListToTowers [] = []
swapListToTowers xs = swapListToTowersInner (head xs) : ...

swapListToTowersInner :: [Bool] -> [Bool]
swapListToTowersInner [] = []
swapListToTowersInner xs = head xs

... similarly here for cutOfFirstElements

你的职能目标是什么?你想做什么?