Haskell 列表理解 0 和 1
我正在尝试编写一个
row :: Int -> Int -> [Int]
row n v
返回 n
个整数列表的函数,所有 0
,除了第 v
个元素,它需要成为 1
。
例如,
row 0 0 = []
row 5 1 = [1,0,0,0,0]
row 5 3 = [0,0,1,0,0]
我是 Haskell 的新手,对此有很多困难。特别是我不知道如何让它重复0
。我理解构建列表的概念,例如 [1..n]
,但我只是得到 [1,2,3,4,5]
任何帮助我们将不胜感激。谢谢。
I am trying to write a function
row :: Int -> Int -> [Int]
row n v
that returns a list of n
integers, all 0
's, except for the v
th element, which needs to be a 1
.
For instance,
row 0 0 = []
row 5 1 = [1,0,0,0,0]
row 5 3 = [0,0,1,0,0]
I am new to Haskell and having a lot of difficulty with this. In particular I can't figure out how to make it repeat 0
's. I understand the concept of building a list from let's say [1..n]
, but I just get [1,2,3,4,5]
Any help with this would be greatly appreciated. Thank you.
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(8)
尝试:
Try:
这里有一个“monadic”解决方案:
replicate
函数多次重复给定值,例如replicate (v-1) 0
给出一个v- 列表1 零。
uncurry
用于修改replicate
以接受一个元组而不是两个单个参数。有趣的运算符>>=
是 monad 的核心;对于列表,它与带有翻转参数的concatMap
相同。Here a "monadic" solution:
The
replicate
function repeats a given value a number of times, e.g.replicate (v-1) 0
gives a list ofv-1
zeros. Theuncurry
is used to modify thereplicate
in order to accept a tuple instead of two single arguments. The funny operator>>=
is the heart of a monad; for lists it is the same asconcatMap
with flipped arguments.提供全面的列表:
或使用
fromEnum
(感谢 dave4420)With a comprehensive list :
Or using
fromEnum
(thanks dave4420)这也应该有效:
This should also work:
还有另一种解决方案,递归地构建列表:
还有一个更具可读性的解决方案,其中“取”零:
And yet another solution, recursively building up the list:
And a more readable one, where zeros are "taken":
具有两个临时变量 c 和 lst 的简单递归循环。 c 用于计数,lst 是我们必须返回的列表。
~
~
A simple recursive loop with two temporary variables c and lst . c is for counting and lst is list which we have to return.
~
~
haskell 的乐趣在于它可以让你以表达算法的方式编写程序。所以尝试一下:
首先创建一个从 1,2 到 n 的列表。
然后检查该数字是否可以被 v 整除,如果是,则将 1 插入到输出列表中,如果不是 0。
示例:
HTH Chris
the fun with haskell is that it let's you write your program very much the way you would express the algorithm. So try:
At first you create a list from 1,2 to n.
Then you check if the number is divisible by v, if it is, 1 is inserted in the output list, if not 0.
Examples:
HTH Chris
我喜欢基于 Chris 的解决方案演示一种自上而下的方法:
这种风格强调函数式编程的思想,即写下某个值(如
row n v
)应该是什么,而不是试图写下函数的作用。回想起一个关于鲜为人知的编程语言萨特的众所周知的笑话,人们可以说在纯函数式编程中,函数什么也不做,它们只是做。I like to demonstrate a top down approach, based on Chris's solution:
This style emphasizes the idea in functional programming that one writes down what a certain value like
row n v
is supposed to be, rather than trying to write down what a function does. In reminiscence of a well known joke about the lesser known pragramming language Sartre one could say that in pure functional programming functions do nothing, they just are.