这让我很困惑,我知道我对haskell有一个很好的知识,但我不能让整个"计数"部分工作.
recursion :: [Int] -> Int
recursion [] = []
recursion (x:xs) = if x `mod` == 3 then.. +1 + recursion xs
Run Code Online (Sandbox Code Playgroud)
我知道它出了什么问题,整个然后+1部分但是不能做到
计算在给定列表中可以将3个整除的数字.
要编写递归解决方案,首先需要考虑基本情况.正如文森特指出的那样,你的基本案件存在严重问题.输出[]有类型[a],但应该有类型Int.问你自己:
对于递归情况,不清楚你缺少什么,但它可能只是语法.你有一般的想法.
recursion :: [Int] -> Int
recursion [] = -- base case goes here
recursion (x:xs) = if x `mod` 3 -- what is the type of (x `mod` 3)? Haskell isn't C!
then 1 + something
else somethingelse
Run Code Online (Sandbox Code Playgroud)