dan*_*s92 1 recursion haskell tuples
我想在Haskell中定义一个简单的函数:
nzp :: [Int] -> (Int,Int,Int)
Run Code Online (Sandbox Code Playgroud)
接受整数列表作为输入并返回三元组(a,b,c),其中a是列表中小于0的数字量,b是等于0的量,c是高于零的量.例如,
nzp [3,0,-2,0,4,5] = (1,2,3)
Run Code Online (Sandbox Code Playgroud)
我必须递归地定义这个函数,我只能遍历列表一次.我怎样才能做到这一点?我似乎无法掌握递归创建元组的概念.
最关心的问候
以下是一些指示:
要使用递归来构建值,您需要将值的先前版本作为参数传递,因此请写入
nzp_helper :: [Int] -> (Int,Int,Int) -> (Int, Int, Int)
Run Code Online (Sandbox Code Playgroud)列表为空时确定答案是什么
nzp_helper [] runningTotals = -- what's the answer if the list is empty?
Run Code Online (Sandbox Code Playgroud)当列表中有某些内容时,确定答案是什么
nzp_helper (i:is) (negatives, positives, zeros) =
| i < 0 = -- write stuff here
| i == 0 = -- I hope you're getting the idea
Run Code Online (Sandbox Code Playgroud)通过定义nzp使用来完成整个过程nzp_helper.
nzp is = nzp_helper is -- one final argument - what?
Run Code Online (Sandbox Code Playgroud)| 归档时间: |
|
| 查看次数: |
2164 次 |
| 最近记录: |