How Do I Compare Numbers and Strings in Racket

If i wanted my string that I entered to be less than a certain number for example 10. If it is less than 10 i would assign it the value 25. How would I go about doing so?

(define (name n)
  (cond
    [(< (string-length nom) 10) 25]))

n not defined

1

1 Answer

You're looking for set! (for "assignment") in conjunction with when (for the condition).

#lang racket


(define (name i)
  (when (<= (string-length i) 10)
    (set! i 10))
  (displayln i))


(name "sustainability")
; => sustainability

(name "diversity")
; => 10

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge that you have read and understand our privacy policy and code of conduct.

Chloe Bennett

Chloe Bennett

Culture, Media & Entertainment Columnist

Chloe Bennett explores the intersection of pop culture, streaming entertainment, digital trends, and contemporary lifestyle. Her weekly commentary reaches thousands of culture enthusiasts.

Share this article
Twitter Facebook Pinterest