簡體   English   中英

在LISP中按索引號從最大到最小對列表進行排序?

[英]Sorting a list from max to min by of index number in LISP?

我正在嘗試編寫一個接受列表的函數,將其從高到低排序,然后給出列表中元素從高到低排序的原始位置。 另外,我希望返回的列表中不要重復。

所以..

(LargestNumberIndex '( 1 23 101 5 6 5 )

應該回來

(2 1 4 3 5 0)

(2 1 4 3 3 0)

目前,我的函數如下所示:

    (defun LargestNumberIndex (listofnums Indexes) 
  ;; make a copy of the list to sort
  (setf listcopy (copy-list listofnums))
  ;; sort list from high to low
  (setf sortlist (sort listcopy #'>))
  ;; compare the elements from both lists to determine the
  ;; position     
  (loop for i from 0 below Indexes collect
       (position (nth i sortlist) listofnums :test #'equal))

  ))

我不知道要添加什么才能使其正常工作。 有人有什么想法嗎?

備注

  • 不要將SETF或SETQ用於局部變量(使用LET)
  • 縮進並格式化代碼
  • 名稱中使用破折號,請勿使用CamelCase
  • LOOP可以遍歷列表:不要像以前那樣使用NTH

裝飾,分類,未裝飾

(也稱為Schwartzian變換

(defun largest-number-index (list)
  (mapcar #'second
          (stable-sort
            (loop
              for index from 0
              for element in list
              collect (list element index))
            #'>
            :key #'first)))

(largest-number-index '(1 23 101 5 6 5))
=> (2 1 4 3 5 0)

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM