2 Replies - 1294 Views - Last Post: 05 February 2013 - 11:41 AM

#1 NecroWinter  Icon User is offline

  • D.I.C Regular

Reputation: 38
  • View blog
  • Posts: 321
  • Joined: 21-October 11

Common Lisp: Sorting Structures that hold two numbers

Posted 05 February 2013 - 11:20 AM

Hello, I have a simple question, how do I sort a list of the following structure based on the exp field:

(defstruct term coef exp)


heres my attempt at sorting
(sort (list (make-term :coef 3 :exp 4)(make-term :coef 3 :exp 5)) #'<)




I realize the above code wont work but I dont know how else to approach it, I really hope I can avoid writing a custom function for this

Is This A Good Question/Topic? 0
  • +

Replies To: Common Lisp: Sorting Structures that hold two numbers

#2 sepp2k  Icon User is offline

  • D.I.C Lover
  • member icon

Reputation: 2153
  • View blog
  • Posts: 3,313
  • Joined: 21-June 11

Re: Common Lisp: Sorting Structures that hold two numbers

Posted 05 February 2013 - 11:30 AM

In your attempt you're passing in < as the comparison function. But < only compares numbers, so that won't work. You need to write a comparison function for terms, i.e. a function term< (term1 term2) that returns whether term1 is less than term2. Then you can call (sort list-of-terms #'term<).
Was This Post Helpful? 2
  • +
  • -

#3 NecroWinter  Icon User is offline

  • D.I.C Regular

Reputation: 38
  • View blog
  • Posts: 321
  • Joined: 21-October 11

Re: Common Lisp: Sorting Structures that hold two numbers

Posted 05 February 2013 - 11:41 AM

thanks, got it
Was This Post Helpful? 0
  • +
  • -

Page 1 of 1