Page 1 of 1

C# in front of aritmethic operation + let issue

Posted: Thu Mar 18, 2010 9:03 am
by kapalua
(defun x (a b c)
(let (square (sqrt (- (* b b) (* (* 4 a) c)))) (list square))

This code always produces (NIL) as output. Can't figure out why....

(defun x (a b c)
(sqrt (- (* b b) (* (* 4 a) c))))

This code returns ,with 1 2 3 as input, this strange looking output: #C(0 2.828427)

Re: C# in front of aritmethic operation + let issue

Posted: Thu Mar 18, 2010 10:33 am
by ramarren
kapalua wrote:This code always produces (NIL) as output. Can't figure out why....
It would be more obvious if you indented it properly:

Code: Select all

(defun x (a b c)
  (let (square
        (sqrt (- (* b b) (* (* 4 a) c))))
    (list square)))
You are binding two variables, 'square' to NIL and 'sqrt' to some value, and then return the list with one element, the value of 'square', which is NIL. The Hyperspec shows the syntax for all standard operators, including LET. It is a good idea to learn to read the notation, which is pretty similar to a variant of Backus-Naur Form.
kapalua wrote: This code returns ,with 1 2 3 as input, this strange looking output: #C(0 2.828427)
'#C(...)' is a syntax for complex number literals. A square root of a negative number is a complex number. The specification even specifies the branch cut.