(方案)我如何划分这些列表?
如何在方案中实现此功能:
*INPUT: ((1 . 1) (1 . 7))
*OUTPUT: (((1 . 2) (1 . 4) (1 . 6)) ((1 . 3) (1 . 5) (1 . 7)))
基本上我有一个包含两个元素的列表。每个元素也是一个包含两个元素的列表,两个元素都是整数 >= 0 和 <= 0。 8
我必须创建这个:
input ((a1 . b) (a1 . c))
output: (if (and (= a1 a2) (odd? b))
While < b c
(list (a1 . b+1) (a1 . b+3) (a1 . b+n)...))
(list (a2 . b) (a2 . b+2) (a2 . b+4)...)
How do I implement this in scheme:
*INPUT: ((1 . 1) (1 . 7))
*OUTPUT: (((1 . 2) (1 . 4) (1 . 6)) ((1 . 3) (1 . 5) (1 . 7)))
basically i have one list with two elements. Each element is also a list with two elements, both integers >= 0 and < 8
I have to create this:
input ((a1 . b) (a1 . c))
output: (if (and (= a1 a2) (odd? b))
While < b c
(list (a1 . b+1) (a1 . b+3) (a1 . b+n)...))
(list (a2 . b) (a2 . b+2) (a2 . b+4)...)
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
“划分列表”
注意:
(a . b)
不是一个正确的列表。这是一个缺点或一对。请参阅下面的不同处理,其中 input 是一个列表,但 left 和 right 是 cons es:
给定输入:
((a1.b)(a2.c))
左边是:
(汽车输入)
右边是:
(cadr 输入)
a1:
(左车)
a2:
(汽车右侧)
b:
(cdr左)
c:
(cdr right)
生成奇数对和偶数对列表:
创建一个起始例程(此处称为夏威夷)来分割输入如上所述。这个启动器将调用一个递归列表生成器,在本例中称为 Hawaii:
正如您所注意到的,Hawaii 是用两个空列表调用的,结果的初始值设定项:奇数列表甚至成对。
Hawaii 例程从完成开始向后计数,根据当前数字是奇数还是偶数开始附加到偶数 的相应奇数 列表中。追加后,它再次调用自身(使用较低的计数),直到当前计数开始。
Hawaii 的第一行和最后一行:
要完成 Hawaii 循环,请添加三行(每个“...”各 1 行):
1. 计算next,即下一次迭代中current的值。
2. 计算olist:如果当前为奇数,则olist = new-el + 赔率;否则,这只是赔率,再次。
3. elist 相同。将 new-el 附加到偶数或仅传递偶数,具体取决于当前是否为偶数。
"Dividing the list"
Note:
(a . b)
is not a proper list. It's a cons or a pair.See the different handling below where input is a list, but left and right are cons es:
Given input:
((a1 . b) (a2 . c))
left is :
(car input)
right is:
(cadr input)
a1:
(car left)
a2:
(car right)
b:
(cdr left)
c:
(cdr right)
Generation of list of odd and even pairs:
create a starter routine(called Hawaiian here) that splits out the input as above. This starter will call a recursive list generator, called Hawaii in this example:
As you noticed, Hawaii is called with two empty lists, initializers for the results: the list of odd and even pairs.
The Hawaii routine counts backward from finish to start appending to the appropriate list odds of evens depending on whether the current number is odd or even. After appending, it calls itself again(with a lower count) until the current count is the start.
The first and last lines of Hawaii:
To finish the Hawaii rountine, add three lines(1 at each '...'):
1. Calculate next, the value of current in the next iteration.
2. Calculate olist: If current is odd then olist = new-el + odds; otherwise it just odds, again.
3. Same for elist. Append new-el to evens or just pass evens along depending on whether current is even or not.