Arcane
Arcane

Reputation: 699

Condition that checks wheter statement is false

I am kind of struggling with this language. I am trying to make a condition, that executes only, when it is false. I have a function that returns false, but I have no idea how to do it. See code below:

(define p (make-queue))
(enqueue! p 1)
(enqueue! p 1)
(enqueue! p 2)
(enqueue! p 3)
(memq 5 (queue->list p))
(cond
  [(false? (memq 4 (queue->list p))) "yaay"]
  )

Basically I am creating a queue, then I am asking with memq, if there is a desired value in the list, which in my case returns false. And now I need to execute the "yaay" part. How can I achieve that? I tried asking for false, I tried (= (#f) (memq 4 (queue->list p))) or some simple ifs, but that does not work either

Upvotes: 1

Views: 48

Answers (1)

Óscar López
Óscar López

Reputation: 236004

The usual way to check the condition you want would be:

(cond
  [(not (memq 4 (queue->list p))) "yaay"])

Remember that in Scheme the only false value is #f, everything else is considered true - including null, '(), 0, "", etc.

In this case, memq will return the list that starts with the searched element if it was found, or #f otherwise, and not negates the result, meaning that the condition is true only if the element isn't in the list.

One last thing - memq uses eq? for comparing, it's a better idea to use member, which uses equal? and is more general (and I think this is the reason why your code doesn't behave as expected); you should read in the documentation the difference between equal? and eq? to understand why this matters.

Upvotes: 3

Related Questions