5 Replies - 2153 Views - Last Post: 22 January 2013 - 03:11 PM

#1 NecroWinter  Icon User is offline

  • D.I.C Regular

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

trouble sorting numbers

Posted 17 January 2013 - 07:44 PM

Im basically doing two calculations (quadratic formula) and I want to sort them. I am very very new to lisp and so much of the documentation just goes way beyond what I understand at this point, regardless, I still have an assignment to do and I pretty much have it done, I just need some help sorting the results.

Im aware of the sort function, but I cant seem to get it to work.

Im trying to follow this guide: http://www.n-a-n-o.c...utorial-22.html

and this is my attempt
edit: there seems to be a bug in DIC. I dont have />, i have )

(defun roots (a b c)

(sort '(
(/(+ (* -1 B )(sqrt(- (expt b 2) (* 4 a c ) )))(* 2 a))
(/(- (* -1 B )(sqrt(- (expt b 2) (* 4 a c ) )))(* 2 a))
#'<)
)

)






this is the error I get



*** - EVAL: too few arguments given to SORT: (SORT '((/ (+ (* -1 B)/>/>/>/>/>/>/> (SQRT (- (EXPT B 2) (* 4 A C)))) (* 2 A)) (/ (- (* -1 B)/>/>/>/>/>/>/> (SQRT (- (EXPT B 2) (* 4 A C)))) (* 2 A)) #'<))
The following restarts are available:
ABORT :R1 Abort debug loop
ABORT :R2 Abort debug loop
ABORT :R3 Abort debug loop
ABORT :R4 Abort debug loop
ABORT :R5 Abort debug loop
ABORT :R6 Abort debug loop
ABORT :R7 Abort debug loop
ABORT :R8 Abort debug loop
ABORT :R9 Abort debug loop
ABORT :R10 Abort debug loop
ABORT :R11 Abort debug loop
ABORT :R12 Abort debug loop
ABORT :R13 Abort debug loop
ABORT :R14 Abort debug loop
ABORT :R15 Abort debug loop
ABORT :R16 Abort debug loop
ABORT :R17 Abort debug loop
ABORT :R18 Abort debug loop
ABORT :R19 Abort debug loop
ABORT :R20 Abort debug loop
ABORT :R21 Abort debug loop
ABORT :R22 Abort debug loop
ABORT :R23 Abort debug loop
ABORT :R24 Abort main loop

This post has been edited by NecroWinter: 17 January 2013 - 08:27 PM


Is This A Good Question/Topic? 0
  • +

Replies To: trouble sorting numbers

#2 NecroWinter  Icon User is offline

  • D.I.C Regular

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

Re: trouble sorting numbers

Posted 17 January 2013 - 10:33 PM

So im trying it a different way, and now I get another cryptic problem with very little documentation to help me

(defun roots (a b c)




(let x (/(+ (* -1 b )(sqrt(- (expt b 2) (* 4 a c ) )))(* 2 a)))
(let y (/(- (* -1 b )(sqrt(- (expt b 2) (* 4 a c ) )))(* 2 a)))


(if (< x y) cons (x y))
(if (> x y) cons (y x))
)



im just calculating the quadratic equation, x will be for the plus and y for the negative

i get this error:

*** - LET: variable list ends with the atom X

This post has been edited by NecroWinter: 17 January 2013 - 10:34 PM

Was This Post Helpful? 0
  • +
  • -

#3 sepp2k  Icon User is offline

  • D.I.C Lover
  • member icon

Reputation: 2128
  • View blog
  • Posts: 3,260
  • Joined: 21-June 11

Re: trouble sorting numbers

Posted 18 January 2013 - 05:17 AM

View PostNecroWinter, on 18 January 2013 - 03:44 AM, said:

there seems to be a bug in DIC. I dont have />, i have )


Yes, that bug's annoying. You can get around that bug by disabling smilies when posting.

Quote


(defun roots (a b c)

(sort '(
(/(+ (* -1 b) (sqrt(- (expt b 2) (* 4 a c ) )))(* 2 a))
(/(- (* -1 b) (sqrt(- (expt b 2) (* 4 a c ) )))(* 2 a))
#'<)
)

)



this is the error I get

*** - EVAL: too few arguments given to SORT:


You have two problems there: First, as the error message is telling you, you're calling sort with too few arguments. Namely you're passing it a single list, while it expects a list and a function. Now you say "Stop talking nonsense, fool! I did pass it a function - are you blind?", but if you look closely at where the parentheses open and close in your code, you've put the function inside the list to be sorted, not as a second argument to sort.

The second problem is that your list doesn't contain what you think it does. Try this in the REPL: '( (/ 4 2) (* 2 3) ). You probably expect it to return a list containing the number 2 and 6, but it doesn't. It returns a list containing two sublists: one that contains the symbol / and the number 4 and 2, and one that contains the symbol * and the numbers 2 and 3. This is because ' is not a shortcut for defining lists, it's a shortcut for quoting a value. When applied to a symbol it returns that symbol as a value rather than evaluating it to the value of the variable with that name and when applied to a list, it quotes the entire contents of that list. To create an unquoted list, use the list function.

View PostNecroWinter, on 18 January 2013 - 06:33 AM, said:

(defun roots (a b c)
(let x (/(+ (* -1 b )(sqrt(- (expt b 2) (* 4 a c ) )))(* 2 a)))
(let y (/(- (* -1 b )(sqrt(- (expt b 2) (* 4 a c ) )))(* 2 a)))



Here's some documentation on let: HyperSpec. There are two important things to note here:

The first is that the first part of a let is a list of variable bindings where each binding is itself a list containing two elements (the variable name and the value). So it should be (let ((x value-of-x)) ... ), not (let x value-of-x). This also means that you don't need a second let for y, you can just put it as a second binding in the list:

(let
  ( (x (/ (+ (* -1 b) (sqrt (- (expt b 2) (* 4 a c ) ))) (* 2 a)))
    (y (/ (- (* -1 b) (sqrt (- (expt b 2) (* 4 a c ) ))) (* 2 a))) )
  ...)



The other important thing is that lets have body and the variables bound by that let, are only bound within that body. So you don't do (let ((x 41))) (+ x 1), you do (let ((x 42)) (+ x 1)). So for your code that would be:

(let
  ( (x (/ (+ (* -1 b) (sqrt (- (expt b 2) (* 4 a c ) ))) (* 2 a)))
    (y (/ (- (* -1 b) (sqrt (- (expt b 2) (* 4 a c ) ))) (* 2 a))) )
  (if (< x y) cons (x y))
  (if (> x y) cons (y x)))



Except there's still problems with that. First the syntax of an if is (if condition then-part else-part) where the else-part is optional. In your case cons is the then-part and (x y) (which is a function call that calls the non-existent function x with the argument y) is the else-part. You probably meant (cons x y) to be the then-part and the else-part to be empty. Note that this creates a cons-cell where both elements are numbers - that's not a list. If you wanted to create a list containing x and y, you should use (list x y).

Another problem I see is that you don't handle the case where x and y are equal. But the most important problem is that you now have two ifs next to each other that both don't have an else-part. If an if doesn't have an else-part and the condition is false, the if will evaluate to nil. So since the result of a function is the result of the last expression in its body, that means that if your last condition is false, the value of your function will be nil. The result of the first if will not affect the value of your function at all. The rule of thumb is, if the then-part of your if does not have any side-effects, your if should also have an else-part. So I'd do it like this:

(if (< x y)
  (list x y)
  (list y x))


This post has been edited by sepp2k: 22 January 2013 - 05:18 AM

Was This Post Helpful? 3
  • +
  • -

#4 sepp2k  Icon User is offline

  • D.I.C Lover
  • member icon

Reputation: 2128
  • View blog
  • Posts: 3,260
  • Joined: 21-June 11

Re: trouble sorting numbers

Posted 22 January 2013 - 05:19 AM

So did you understand the points I made in my post? Did you get your code running?
Was This Post Helpful? 2
  • +
  • -

#5 NecroWinter  Icon User is offline

  • D.I.C Regular

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

Re: trouble sorting numbers

Posted 22 January 2013 - 02:43 PM

yes I did, It now works, and I got full credit on what I was doing. Thank you

EDIT: Your post actually helped the first part of my next assignment, just now! lol

This post has been edited by NecroWinter: 22 January 2013 - 03:11 PM

Was This Post Helpful? 0
  • +
  • -

#6 sepp2k  Icon User is offline

  • D.I.C Lover
  • member icon

Reputation: 2128
  • View blog
  • Posts: 3,260
  • Joined: 21-June 11

Re: trouble sorting numbers

Posted 22 January 2013 - 03:11 PM

Glad to hear it.
Was This Post Helpful? 1
  • +
  • -

Page 1 of 1