查找列表中数字的位置
大家好,我有一道作业问题,一直困扰着我!我应该创建最小索引,它将采用非空列表并返回列表中最小数字的索引。 (car ls) 的索引 = 0,(car (cdr ls)) 的索引 = 1,依此类推。
需要创建一个帮助器来跟踪当前位置、最小位置、最小值和列表。到目前为止,我有这个显示基本算法的程序(未加载)。但是我很难跟踪所有内容并将其放入 chez 方案代码中。
(define index-helper
(lambda (ls current-position least-position least-value)
(if (> (car ls) least-value)
(add1 (car ls (cdr ls (add1 current-position))))
(car ls (cdr ls (add1 current-position))))))
;trace
;ls: (4231) c-pos: 0 least-value: 5 least-pos: 0
;ls: (231) c-pos: 1 least-value: 4 least-pos: 1
;ls: (31) c-pos 2 least-value: 2 least-pos: 2
;ls: 1 c-pos: 3 l-v: 2 l-pos: 2
;ls '() c-pos: 4 l-v: 1 l-pos: 4
;*least-position = current-position
我已经用谷歌搜索过这个并在 python 中找到了类似的问题,但我不理解代码,因为我是编程新手。 :P 如果有人能给我提示,我将非常感激!
Hey guys, I have a homework question that's been frustrating me to no end! I'm supposed to create index-of-least that will take a non-empty list and return the index of the smallest number in the list. The index of the (car ls) = 0, index of the (car (cdr ls)) = 1, and so on.
A helper needs to be created that will keep track of the current-position, least-position, least-value, and list. So far, I have this program (that doesn't load) that shows the basic algorithm.. But I'm having a hard time keeping track of everything and putting it into chez scheme code.
(define index-helper
(lambda (ls current-position least-position least-value)
(if (> (car ls) least-value)
(add1 (car ls (cdr ls (add1 current-position))))
(car ls (cdr ls (add1 current-position))))))
;trace
;ls: (4231) c-pos: 0 least-value: 5 least-pos: 0
;ls: (231) c-pos: 1 least-value: 4 least-pos: 1
;ls: (31) c-pos 2 least-value: 2 least-pos: 2
;ls: 1 c-pos: 3 l-v: 2 l-pos: 2
;ls '() c-pos: 4 l-v: 1 l-pos: 4
;*least-position = current-position
I already googled this and found similar questions in python, but I don't understand the code because I'm new to programming. :P
If anyone can give me a hint, I'd really appreciate it!
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
你想要两个功能。第一个函数找到最小元素
x
。第二个函数查找列表中元素x
的索引。类似于:
测试:
或者,一次性:
测试:
在
index-do
辅助函数中,首先检查中间列表是否为空;这是一种基本情况,当我们在列表中只有一个元素时,并返回其索引。下一个条件检查中间列表的下一个元素是否大于当前的
least
值,如果是,我们使用新的least
值及其索引调用 helper。当下一个元素不大于
least
时,选择最后一个条件,并使用相同的least
和ind-least 值调用辅助函数
,删除头元素的中间列表,直到列表中没有元素为止,当列表中没有元素时,我们接近基本情况。You want two functions. The first function find the least element
x
. The second function finds the index of the elementx
in the list.Something like:
Test:
Or, in one pass:
Test:
In
index-do
helper function first you check if the intermediate list is empty; this is a base case, when we have got just one element int the list, and return its index.Next condition checks if the next element of the intermediate list is greater than the current
least
value, and if so, we call helper with the new value ofleast
and its index.The last condition is selected, when the next element is not greater than the
least
, and it calls the helper function with the same values ofleast
andind-least
, and the intermediate list with head element removed until there are no elements in the list, and we approached the base case, when there are no elements in the list.名为 let 的一个很好的例子:
A good example for named let: