Misc 01

Explode, implode, and a few exercises.

New functions:

  • explode: Break a string in to a list of one letter strings.
  • implode: Take a list of one letter strings and join them together.
(explode "peace") => (list "p" "e" "a" "c" "e")
(implode (list "w" "a" "r")) => "war"

Usually you would write a helper function that works on one letter strings, then use these functions to convert to ordinary strings.

Exercises Revised

A 1-String is a String of length 1. Examples: "a", "4", "$".

  1. Write is-digit?: 1-String -> boolean

     (check-expect (is-digit? "3") #true)
     (check-expect (is-digit? "w") #false)
    
  2. Write only-digits: (Listof 1-String) -> (Listof 1-String).

     (check-expect (only-digits (list "1" "w" "3"))
                   (list "1" "3"))
    
  3. Write d-in-s: String -> String. The digits-in-string function gives a string containing only the digits in its input.

     (check-expect (d-in-s "x1w3") "13")
    
  4. Write fln: 1-String -> Number. The “first and last number” function makes a number out of the first and last digits you find. Expect more than one step to do this. (Use helper functions.)

     (check-expect (fln (list "1" "x" "3" "5" "y"))
                   15)
    
     ;; tricky case for experts
     (check-expect (fln (list "x" "6" "y"))
                   66)
    

Original (edit after Spring 2024)

  1. only-digits: Extract the digits from a string.

     (check-expect (only-digits "wall20green98") "2098")
    
  2. same-ends?: True if a string starts and ends with the same letter.

     (check-expect (same-ends? "rover") #true)
     (check-expect (same-ends? "gloves") #false)
    
  3. palindrome?: True if a string reads the same forwards and backwards. Please don’t use reverse.

     (check-expect (palindrome? "noon") #true)
     (check-expect (palindrome? "rotator") #true)
     (check-expect (palindrome? "kaybk") #false)
    
Last modified April 26, 2024: Miscellaneous projects. (fd2412a)