[英]How can I sum after convert that from string to int list values?
I have basic problem with Haskell but I'm pretty new that's way struggled extremely already I have got limited knowledge related haskell (before this I did deal with C root with languages) so far I explaıned myself我对 Haskell 有基本问题,但我很新,这已经非常困难了
Imagine that we have such that list on String format in haskell ["2","76","564"] so I desire to convert Integer [2,76,564] something like that.(I did )想象一下,我们在 haskell ["2","76","564"] 中有这样的字符串格式列表,所以我希望转换 Integer [2,76,564] 类似的东西。(我做了)
after I need to sum of all list element在我需要对所有列表元素求和之后
2+76+564 = 642 stopped in this part 2+76+564 = 642 停在这部分
convert:: [String] -> [Int]
convert = map read
convert (xs:x)= x + convert xs
How can I calculate after that I convert part.转换部分后如何计算。
Your convert
does not makes much sense: it has two clauses where the first clause has no parameter and the second one has one parameter.您的
convert
没有多大意义:它有两个子句,第一个子句没有参数,第二个子句有一个参数。 It is sufficient to implement convert
as: convert
实现为:
convert :: [String] -> [Int]
convert = map read
You can then calculate the sum with sum:: (Foldable f, Num a) => fa -> a
:然后,您可以使用
sum:: (Foldable f, Num a) => fa -> a
计算总和:
convertSum :: [String] -> Int
convertSum = sum . convert
We can also implement convertSum
with recursion in that case it looks like:在这种情况下,我们还可以使用递归实现
convertSum
:
convertSum :: [String] -> Int
convertSum [] = …
convertSum (x:xs) = read x + …
where I leave implementing the …
parts as an exercise.我把实现
…
部分作为练习。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.