This is a read-only archive of lispforum.com. The forum was locked to new users and posts and is preserved here as static HTML from a database snapshot taken on 2019-09-07.

C# in front of aritmethic operation + let issue

2 posts · 816 views

(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

kapalua wrote:This code always produces (NIL) as output. Can't figure out why....
It would be more obvious if you indented it properly:
(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.