[英]following the class correctly but the code is not accepted
I want it to show the desired n final numbers, but the GHCi is giving an error in the code我希望它显示所需的n 个最终数字,但 GHCi 在代码中给出错误
nLast :: int -> [a] -> [a]
nLast 0 _ = []
nLast _ [] = []
nLast n (x:xs) = nultimos (n-1) xs
You can work with two enumerators over the list.您可以在列表中使用两个枚举器。 You give one of the enumeraters a head start of n
items and each time you let both the enumerators make one hope.你给其中一个枚举者一个n
项的领先优势,每次你让两个枚举者都产生一个希望。 If the first runner reaches the empty list, then we know that the second runner has n
items they still need to enumerate over.如果第一个跑步者到达空列表,那么我们知道第二个跑步者还有n
项目需要枚举。
We thus can implement this with:因此,我们可以通过以下方式实现:
nLast :: Int -> [a] -> [a]
nLast n ls = go (drop n ls) ls
where go (_:xs) (_:ys) = …
go [] ys = …
here you still need to fill in the …
parts.在这里你仍然需要填写…
部分。 I leave this as an exercise.我把它留作练习。
In the above example there is a pattern that we do not cover, a pattern where the first list is a non-empty list (_:_)
and the second is empty []
.在上面的示例中,有一个我们没有涵盖的模式,其中第一个列表是非空列表(_:_)
,第二个是空[]
。 We know that this can never happen, it might still be better however to add a clause for this.我们知道这永远不会发生,但是为此添加一个子句可能会更好。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.