Search code examples
schemeracketuppercaselowercase

How do I find the number of lower case letters in Scheme Racket?


I'm trying to find the number of lower/upper case letters in a string, but there is a problem with my code:

(define case
  (lambda (list)
    (if(char-lower-case? (car list))
       (case (cdr list))
       (+ 1 (case (cdr list)))
       )
    ))

(case (string->list "ScheMe"))

How can I solve this problem?


Solution

  • In your function you have two problems:

    1. case is a predefined operator in racket/scheme

    2. You don't test for an empty list.

    Moreover, you use the parameter list, which is a primitive operator and should not be used as variable name.

    Here is a working function:

    (define (case1 lst)
        (cond ((null? lst) 0)
              ((char-lower-case? (car lst)) (case1 (cdr lst)))
              (else (+ 1 (case1 (cdr lst))))))
    
    (case1 (string->list "ScheMe"))