Search code examples
racketconditional-statementsempty-list

empty? does not stop Racket from running expression


I'm trying to have function get a number out of a structure that is inside a list. When the list is empty, the function should output +inf.0.

However, when I input an empty list like this: (function (list empty)), the +inf.0 part is ignored and the else expression is executed anyway leading to the error that struct-xyz gets an empty list or that first doesn't allow empty lists. What am I doing wrong?

(define (function datalist)) 
(cond 
 [(empty? datalist) +inf.0]
 [else(struct-xyz (first datalist))]))

This is all in the Beginning Student Teaching Language.


Solution

  • You should call (function empty), not (function (list empty)). (function (list empty)) is calling your function on a list that is not empty, it contains one element (another list, which is empty).