Question

This is day 1 of Lisp for me. I am trying to eventually write an if else statement...hopefully sometime this year. I am not sure why this is giving me an error?

(cond (< 1 2) (print "hey"))

Why is this crashing? It says the variable '<' is unbound? I don't get Lisp at all... Thanks in advance.

Was it helpful?

Solution

cond takes a list of tests and clauses

(cond (<test>  <if test is true>)
      (<test2> <if test2 is true>)
      ...)

I think what you meant to write is

(cond ((< 1 2) (print "hey"))) ;; if 1 is less than 2, print "hey"

What you've actually got in your question is

(cond (< 1 2)        ;; if `<` is bound as a variable, return 2
      (print "hey")) ;; if `print` is bound as a variable, return "hey"

Neither of those symbols are defined in the variable namespace by default, so you'll get an error.

If you only have one form to dispatch on, and only want to do something if it's true, it's more common to use when than cond.

(when (< 1 2) (print "hey"))
Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top