0

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?

3
  • Please specify what exactly the problem is with your code.
    – honk
    Mar 30, 2016 at 9:42
  • if first letter in the string is lower case, program do not return the (case (cdr list)). I do not check all string.
    – Ersin
    Mar 30, 2016 at 9:49
  • I solved, The solution is: (define case (lambda (list) (if(null? list) 0 (if(char-upper-case? (car list)) (case (cdr list)) (+ 1 (case (cdr list))) ) ))) (case (string->list "SeheMe"))
    – Ersin
    Mar 30, 2016 at 9:51

2 Answers 2

0

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"))
0

Your code lacks a base case. (case '()) should evaluate to 0 but you get an error since you are doing car and cdr on nil.

Other things that might be wrong:

Your title indicates that you want to count lowercase letters but you increase for every uppercase.

list and case are names from the standard library. For R5RS it means undefined behaviour and for R6RS and later it means the library bindings would be unavailable. In #!racket (I guess you use this language since you tagged racket) it works as R6RS.

Your Answer

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

Not the answer you're looking for? Browse other questions tagged or ask your own question.