Swap two elements in list in Scheme [closed]

孤街浪徒 提交于 2019-12-23 16:06:02

问题


I need to switch 2 elements on entered indexes in list in Scheme lang. For example:

(swap-index 0 3 '(1 2 3 4 5))

(4 2 3 1 5)

Can someone help? Thanks in advance! :)


回答1:


Here is solution in clojure. I hope algorithm would be helpful.

(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)



回答2:


Right now I can't think of a way to solve this one without iterating three times over the whole list, (one for each list-ref and one more for build-list.) Not the most efficient solution, but here it goes:

(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)))

I'm assuming that the indexes exist for the given list, otherwise list-ref will produce an error. The indexes can be passed in any order, meaning: idx1 can be less than, equal or greater than idx2. It works as expected, returning a new list with the modifications in place:

(swap-index 0 3 '(1 2 3 4 5))
=> '(4 2 3 1 5)



回答3:


This method traverses the list at most twice:

(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

First, find-elements looks through the list and returns a cons'd pair of the elements that we want to swap. Note: this code depends on the assumption that the indices are given in order so that the smallest is first.

Next, build-list takes the output from find-elements so that during our next traversal we can substitute the appropriate element.



来源:https://stackoverflow.com/questions/13564575/swap-two-elements-in-list-in-scheme

标签
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!