使用Haskell生成1到10的简单列表
我是哈斯克尔(Haskell)的新来者,可以帮助我如何生成1到10的清单。
我试图这样做:
seqList :: Integer -> [Integer]
seqList 1 = [1]
seqList n = n : seqList(n-1)
结果10到1,而不是1到10
和第二个问题 我们可以使函数作为价值吗?
numList :: [Integer]
numList = [1,2..10]
totJum :: Int
totJum = length numList
takeNum :: Int->[Integer]
takeNum totJum
| totJum >= 10 = take 5 numList
| totJum == 10 = numList
使用此代码,如果数字的长度与条件匹配,我想调用输出。
I am new in Haskell, could guys help me how to generating list from 1 to 10.
I tried to make like this:
seqList :: Integer -> [Integer]
seqList 1 = [1]
seqList n = n : seqList(n-1)
The result 10 to 1, not 1 to 10
And second question
can we make function as value.
numList :: [Integer]
numList = [1,2..10]
totJum :: Int
totJum = length numList
takeNum :: Int->[Integer]
takeNum totJum
| totJum >= 10 = take 5 numList
| totJum == 10 = numList
With this code, i want to call output if the length from numlist matches the condition.
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
对于第一个,您可以使用累加器:用于产生值的变量,并在递归调用中每次增加,因此:
我在
…零件作为锻炼。
您不应将
totjum
用作参数,而应仅在功能正文中使用它,因此请注意,您不涵盖
totjum
更少的情况比或10。在这种情况下,函数因此会误差。因此,您可能需要添加否则
子句。For the first one you can work with an accumulator: a variable you use to yield a value and each time increment in the recursive call, so:
where I leave filling in the
…
parts as an exercise.You should not use
totJum
as a parameter, but just use it in the body of the function, so:Note however that here you do not cover the case where
totJum
is less than or 10. In that case the function will thus error. You thus might want to add anotherwise
clause.