简体   繁体   English

解释递归 Haskell 列表 function 是如何工作的?

[英]Explain how a recursive Haskell list function works?

I know what the following function does I would just like an explanation of how it works and the calculations that take place:我知道下面的 function 做了什么我只想解释它是如何工作的以及发生的计算:

sponge :: Int -> [a] -> [a]
sponge 0 xs = xs
sponge n [] = []
sponge n (x:xs) = sponge (n-1) xs

I just seem to have lost the plot with it all now:(我现在似乎失去了 plot :(

Any help to get me back on track would be much appreciated: :)任何能让我重回正轨的帮助将不胜感激::)

It's a recursive function over two variables.它是两个变量上的递归 function。 You can break it apart line-by-line to understand it:您可以将其逐行分解以理解它:

sponge :: Int -> [a] -> [a]

Two arguments, one an Int , one a list of some elements.两个 arguments,一个是Int ,一个是一些元素的列表。

sponge 0 xs = xs

The base case.基本情况。 If the Int argument is zero, just return the list argument unmodified.如果Int参数为零,则只返回未修改的列表参数。

sponge n [] = []    

Another base case, if the list is empty, immediately return the empty list.另一种基本情况,如果列表为空,则立即返回空列表。

sponge n (x:xs) = sponge (n-1) xs

Finally, the inductive step.最后是归纳步骤。 If the list is non-empty (ie made up of at least one element and a tail, denoted by x:xs ), then the result is sponge called on n-1 and the tail of the list.如果列表非空(即由至少一个元素和一个尾部组成,用x:xs表示),则结果是调用n-1和列表尾部的sponge

So what will this function do?那么这个function会做什么呢? It will return the tail of the list after dropping n elements.删除n元素后,它将返回列表的尾部。 It is the same as the drop function:drop function相同:

> drop 10 [1..20]
[11,12,13,14,15,16,17,18,19,20]

And

> sponge 10 [1..20]
[11,12,13,14,15,16,17,18,19,20]

In fact, we can ask QuickCheck to confirm:其实我们可以通过 QuickCheck 来确认:

> quickCheck $ \n xs -> sponge n xs == drop n xs
*** Failed! Falsifiable (after 7 tests and 5 shrinks):    
-1
[()]

Ah.啊。 They're different.他们是不同的。 When n is negative: So we can modify the property relating the two functions:n为负时:因此我们可以修改与两个函数相关的属性:

> quickCheck $ \n xs -> n >= 0 ==> sponge n xs == drop n xs
+++ OK, passed 100 tests.

So your function behaves like drop, for cases when n is positive.因此,对于n为正数的情况,您的 function 的行为类似于 drop。


Here's a trace of the intermediate values of n and xs , obtained via the hood debugger :这是通过hood 调试器获得的nxs的中间值的跟踪:

在此处输入图像描述

It takes two parameters, as you can see: an Int and a list.如您所见,它有两个参数:一个 Int 和一个列表。 It pattern-matches to distinguish three cases: 1) the Int is zero;它通过模式匹配来区分三种情况: 1) Int 为零; 2) the list is empty; 2)列表为空; or, 3) the Int is not zero nor is the list empty.或者,3) Int 不为零,列表也不为空。

In case 1 it returns the list;在案例 1 中,它返回列表; in case 2, it returns the empty list (which is what the second parameter was anyway);在情况 2 中,它返回空列表(无论如何,这是第二个参数); in case 3, it recursively calls itself with original Int parameter minus 1 and the original list minus its first element .在第 3 种情况下,它使用原始 Int 参数减去 1和原始列表减去其第一个元素递归调用自身。

It looks a lot like "drop" from the Prelude.它看起来很像 Prelude 中的“drop”。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM