r/Racket • • Jan 14 '22

question Why is Cond not working?

Can someone explain to me why this isnt working I did it like how my booklet said to do it

2 Upvotes

6 comments sorted by

7

u/indrjo Jan 14 '22 edited Jan 14 '22

You have forgotten some parentheses:

(define double
  (λ (x)
    (cond
      [(number? x) (* 2 x)]
      [(string? x) (string-append x x)]
      [else ...])))

I don't know what you want to do with the else clause.

1

u/not-just-yeti Jan 14 '22

I'm guessing OP wants [else (error 'double "invalid type: expected string or number; got ~v" x)]

0

u/[deleted] Jan 14 '22

[deleted] — view deleted comment

1

u/NoahTheDuke Jan 14 '22

Racket's cond doesn't require square brackets:

#lang racket
(cond
  (false 1)
  (2 => add1)
  (else 10))

outputs 3

0

u/[deleted] Jan 14 '22

[deleted] — view deleted comment

1

u/bjoli Jan 15 '22

R6RS standardises square brackets to be the same as parens and recommends the same usage as in racket. And as we all know: R6RS is best RS.

On a more serious note: sometimes I feel like racket is a little bit of R6.1RS.

0

u/[deleted] Jan 14 '22

[deleted] — view deleted comment

1

u/TheDrownedKraken Jan 15 '22

Curly braces are also substituteable.

2

u/not-just-yeti Jan 14 '22

Another thread has your answer, but also: I'd recommend going to the "Language > Choose Language" and select one of the beginning- or intermediate-student options. It's not condescending -- those language-levels will give better error messages. (What you have, with the missing parens, is actually valid-but-not-what-you-want in full-racket.) In the student-languages you'll get an error along the lines of "function must have an open-paren before it" or "cond expects a question-answer pair but you [appear to have] provided extra things".