Racket:cant return input

i have been asked to "Write a function with one argument, L, such that, if L is a number, then it is
doubled and returned, otherwise it is returned unchanged" The problem iwhenever i run the function and say type in something other than a number it does not return the input exepct return an error i want the function to return the input.it does return the double of the number but i also want it to retun whatever i put as the input.here is my code

(define L (λ (b)

        (cond
          (number? b (* 2 b))

This looks like a school assignment, but anyway...

Apart from missing parentheses at the end, there are probably some missing in the (missing) application of the number? predicate. Look at cond[1] documentation - the mistake should be obvious. Also remember that if no cond-clause matches and produces a result, the result is void - an else clause is what you are probably looking for. But right now that is not the error you see in your code. Also the error that's produced is fairly self-explanatory:

> (L "a")
; *: contract violation
;   expected: number?
;   given: "a"
; [,bt for context]

Anything you pass to your procedure gets passed on as second argument of * procedure.
Is there any reason you are using cond and not if for this one?

[1] 3.12 Conditionals: if, cond, and, and or))

@osman44 Check the chapter 4.1 Programming with Conditionals.
I Fixed-Size Data

im still confused the number? checks to see if the input is a number however i want to ignore it if its not and return the input such as S can you give a little more help

S’s reference was to htdp.org Please take a look now.

Watch John's video:

2 Likes

Hi @osman44 , allow me to offer an additional suggestion: change DrRacket to use the "Beginning Student Language" and run your program again -- if you do this, DrRacket will tell you exactly what the problem is.

Alex.

3 Likes

AMEN. Also: follow the design recipe. Here's a data definition to get you started

A maybe-number is either
- a number, or 
- something else
1 Like