Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Lower case an entire string in Racket

Is there a way to turn all of the characters in a string into lower case in Racket?

The only way I can think of is turning char-downcase but it won't work with strings

I am using the beginner language so I can't use some functions

like image 717
user2113651 Avatar asked Apr 03 '13 05:04

user2113651


1 Answers

In practice, you'd use the string-downcase procedure for this:

(string-downcase "ABCDE")
=> "abcde"

But you're working with the beginner's language, so here's the general idea for solving it - I'll give you some hints, it's better if you try to solve the problem by yourself. First, let's split the problem in two parts: one procedure that converts the string into a list of characters, calls a helper procedure that performs the actual transformation and finally turns the converted list back into a string:

(define (lowercase str)
  (<???>           ; convert the list of chars into a string
   (convert        ; call the helper procedure
    (<???> str)))) ; convert the string into a list of chars

The convert procedure is a helper, which does the heavy lifting and converts to lowercase each character in a list of characters:

(define (convert strlst)
  (if <???>                  ; if the list of chars is empty
      <???>                  ; return the empty list
      (cons                  ; else `cons`
       (<???> <???>)         ; convert to lowercase the first char in list
       (convert <???>))))    ; advance recursion over list

The key parts of the solution are the procedures for manipulating strings and characters, click on the links and study the documentation.

like image 120
Óscar López Avatar answered Sep 21 '22 00:09

Óscar López