为什么这是一个非详尽的搜索模式?

Ste*_*rad 2 recursion haskell

为什么这不是穷举,不放弃一个元素意味着这应该最终停止?

do_something :: [(String, String, Int)] -> String
do_something [(a, b, c)] = func(a, b, c) ++ do_something( drop 1 [(a, b, c)])
Run Code Online (Sandbox Code Playgroud)

bhe*_*ilr 7

您必须(String, String, Int)在声明的列表中指定每个案例do_something.您必须给出传递给的参数何时do_something为空或包含多于1个元素的定义.在未指定这些情况时,编译器不知道自动执行的操作.

查看它的另一种方法是函数声明中的模式匹配与使用case语句相同:

do_something :: [(String, String, Int)] -> String
do_something xs = case xs where
    [(a, b, c)] -> func (a, b, c) ++ do_something (drop 1 [(a, b, c)])
    -- What about the other cases?
    -- otherwise -> ???
Run Code Online (Sandbox Code Playgroud)

此外,在这种情况下,将函数指定为更好

do_something (x:xs) = func x ++ do_something xs
do_something [] = ???
Run Code Online (Sandbox Code Playgroud)

因为这实际上是递归地定义函数.表达式drop 1 [(a, b, c)]与写入相同[],因此您当前的定义等同于

do_something [(a, b, c)] = func (a, b, c) ++ do_something []
Run Code Online (Sandbox Code Playgroud)