I'm learning Lisp now, and I'm trying to do an exercise that asks me to get the maximum value of a list, the syntax is totally different from most programming languages I've learned, so I'm having some difficulties.
My code:
(defun test(y)
(cond
((and (first y) (> (second y)) (> (third y)))
(format t "numero maximo ~d" (first y))
((and (second y) (> (first y)) (> (third y)))
(t (format t "numero maximo ~d" (second y))
((and (third y) (> (second y)) (> (first y)))
(t (format t "numero maximo ~d" (third y))
))
I'm receiving this error: incomplete s-expression in region
Your code is too complex, it tries to take elements from a list, compare them, and print something. Like in other languages, use smaller functions and, particularly with a new language, test often in order to avoid having to debug something too large.
Your code, automatically indented with Emacs, looks as follows:
And the editor complains about unbalanced parentheses:
(> (second y)), the>function is given only one argumentAll your
condclauses are in fact nested inside the first clause. Using an editor that highlights matching parentheses helps a lot here. The syntax should be:If your test involves calling predicates, then it looks like:
But note that you do not need to put comments for closing delimiters, the indentation and the automatic highlighting of parentheses should help you avoid mistakes.
Let's try a rewrite.
First of all, you can write a function that just compares numbers, not thinking about lists or formatting; here is a very straightforward
max-of-3implementation (without cheating and calling the built-inmaxfunction):Evaluate the function, and test it on multiple inputs, for example in the REPL:
Then, you can build up the other function, for your list:
If you need to do more error checking ahead of time, like checking that the lists is well-formed, you should probably define other auxiliary functions.