Matching Question

Hi,

I am a bit confused why the following code doesn't produce an output:

(match #f
    [else
     (cond
       [#f 'not-evaluated]
       [else 'also-not-evaluated])])

I would expect it to output 'also-not-evaluated

Thanks

The else in match is treated as a variable, which match binds. What you wrote is equivalent to this:

(match #f
  [foo
   (cond
    [#f 'not-evaluated]
    [foo 'also-not-evaluated])])

And since foo is bound to #f, none of the cond branches are taken and the whole cond form evaluated to (void).

I recommend using _ instead of else when you want to express a catch-all fallback branch with match. The _ form is treated specially by match and interpreted as "match anything, bind nothing". Like this:

(match #f
  [_
   (cond
    [#f 'not-evaluated]
    [else 'this-is-evaluated-now])])

One « précision » to add: cond recognizes else by binding; if it isn’t bound to the same thing as what it knows from racket/base, it won’t treat it as an else (or conversely if you give it something else that is bound to that, cond does see the else).

and if i remember well of Lisp , the ancestor of Scheme, in Lisp there is no else ,we simply replace it by T or t, meaning True when there is a need of an else endind the cond. It is then a bit rustic but more simple to understand:

;; la recherche de l'element a un niveau arbitraire
(defun member1 (ele liste)
  (cond
   ((atom liste) NIL)
   ((equal (car liste) ele) liste)
   (T (memb1 ele (member1 ele (car liste)) (cdr liste)))))