Logo

Programming-Idioms

This language bar is your friend. Select your favorite languages!
  • Scheme

Idiom #267 Pass string to argument that can be of any type

Declare an argument x to a procedure foo that can be of any type. If the type of the argument is a string, print it, otherwise print "Nothing."

Test by passing "Hello, world!" and 42 to the procedure.

(define (foo x)
  (displayln
   (if (string? x)
       x
       "Nothing")))

(foo "Hello, world!")
(foo 42)

if evaluates to a result.
(defn foo [x]
  (println (if (string? x) x "Nothing.")))

(foo "Hello, world!")
(foo 42)

New implementation...
< >
tkoenig