Syntax error when using parentheses instead of brackets

Viewed 31

I thought that brackets and parentheses were interchangeable in Racket.

However, when I run the following code, I get the syntax error, else: not allowed as an expression in: (else (cons (car lat) (rember a (cdr lat))))

#lang racket

(define rember
  (lambda (a lat)
    (cond
      ((null? lat) (quote ()))
      ((eq? a (car lat) (cdr lat))
      (else
        (cons (car lat) (rember a (cdr lat))))))))

The error goes away when I replace the parentheses in cond with brackets:

#lang racket

(define rember
  (lambda (a lat)
    (cond
      [(null? lat) (quote ())]
      [(eq? a (car lat)) (cdr lat)]
      [else
        (cons (car lat) (rember a (cdr lat)))])))

So it appears that there is a syntactic difference between parentheses and brackets. Is this the case or am I missing something?

1 Answers

You forgot one parenthesis in ((eq? a (car lat) (cdr lat)). If you rewrite that correctly, both versions will work:

(define rember
  (lambda (a lat)
    (cond
      ((null? lat) (quote ()))
      ((eq? a (car lat)) (cdr lat))
      (else
        (cons (car lat) (rember a (cdr lat)))))))
Related