问题描述
我正在尝试使用lisp的排序来通过函数对列表进行排序,但不知道如何执行此操作。
我在2D空间中有一个起始点,带有x和y坐标。然后我有一个N其他点的列表,我有一个功能,计算2点之间的距离。
我现在想要的是一个包含所有N点的列表,并且按距离从起点到所有其他点升序排列。
我想我可以使用sort函数并且传递一个函数作为参数(计算距离函数)但是我不知道该怎么做,并且在网络上的研究没有帮助。
任何想法?
问候
code>:key with ) lambda(p)(dist p start-point)))
这会将<$ c $根据到起点的距离,以递增顺序(使用
。>
来递减)来指定c> list
I am trying to use lisp's sort to sort a list via a function but dont have a clue how to do this.I have a start-point in 2D Space with x and y coordinates. Then i have a List of N-other points and i have a function that calculates the distance between 2 points.What I want now is a list, that contains all the N-Points and is sorted by distance ascending from the start-point to all other points.
I think I can use the sort-function and pass a function as argument (the calculate-distance function) But i dont know how to do it and researches on the web did not help.
Any ideas?
Regards
Use :key
with sort
:
(sort list #'< :key (lambda (p) (dist p start-point)))
This will sort the list
of points in the increasing order (use >
for decreasing) based on the distance to start-point
.
这篇关于通过函数lisp排序列表的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!