我需要在Scheme lang的列表中的输入的索引上切换2个元素。例如:
(swap-index 0 3'(1 2 3 4 5))
(4 2 3 1 5)
有人可以帮忙吗?提前致谢! :)
答案 0 :(得分:1)
现在我想不出一种方法来解决这个问题,而不是在整个列表上迭代三次(每个list-ref
一个,build-list
一个。)效率不高解决方案,但在这里:
(define (swap-index idx1 idx2 lst)
(define (build-list lst idx e1 e2)
(cond ((null? lst)
'())
((= idx idx1)
(cons e2 (build-list (cdr lst) (add1 idx) e1 e2)))
((= idx idx2)
(cons e1 (build-list (cdr lst) (add1 idx) e1 e2)))
(else
(cons (car lst) (build-list (cdr lst) (add1 idx) e1 e2)))))
(build-list lst 0 (list-ref lst idx1) (list-ref lst idx2)))
我假设给定列表存在索引,否则list-ref
将产生错误。索引可以按任何顺序传递,这意味着:idx1
可以小于,等于或大于idx2
。它按预期工作,返回新列表并进行修改:
(swap-index 0 3 '(1 2 3 4 5))
=> '(4 2 3 1 5)
答案 1 :(得分:0)
此方法最多遍历列表两次:
(define (swap-index index1 index2 lst)
;; FIND-ELEMENTS --
;; INPUT: count, an integer; lst, a list
;; OUTPUT: a pair of the form '(a . b)
(define (find-elements count lst)
(cond ((null? lst) '()) ; really, we should never reach this if indices are valid
((= count index1) ; found the first element, so hold on to it while we look for the next one
(cons (car lst) (find-elements (+ 1 count) (cdr lst))))
((= count index2) (car lst)) ; found the second element, return part 2 of the pair
(else ; since we only care about 2 elements we can just skip everything else
(find-elements (+ 1 count) (cdr lst)))))
;; BUILD-LIST --
;; INPUT: count, an integer; elements, a pair; lst, a list
;; OUTPUT: a new list
(define (build-list count elements lst)
(cond ((null? lst) '()) ; again, we shouldn't get here if indices are valid
((= count index1) ; reached first index, substitute 2nd element and keep going
(cons (cdr elements) (build-list (+ 1 count) elements (cdr lst))))
((= count index2) ; reached second index, substitute 1st element and stop
(cons (car elements) (cdr lst)))
(else ; everything else just gets added to the list per usual
(cons (car lst) (build-list (+ 1 count) elements (cdr lst))))))
(build-list 0 (find-elements 0 lst) lst)) ; call build-list using a call to find-elements as a parameter
首先,find-elements
查看列表并返回我们想要交换的cons
'对元素。 注意:此代码取决于假设索引按顺序给出,以便最小值是第一个。
接下来,build-list
获取find-elements
的输出,以便在下次遍历期间我们可以替换相应的元素。
答案 2 :(得分:0)
这是clojure中的解决方案。我希望算法会有所帮助。
(defn split [idx lst]
(let [lst-rest (drop idx lst)]
[(take idx lst) (first lst-rest) (rest lst-rest)]))
(defn swap-index [idx1 idx2 lst]
(let [[lst1 e1 lst] (split idx1 lst)
[lst2 e2 lst3] (split (dec (- idx2 idx1)) lst)]
(concat lst1 [e2] lst2 [e1] lst3)))
=> (swap-index 0 3 [1 2 3 4 5])
(4 2 3 1 5)