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", "$".
-
Write
is-digit?: 1-String -> boolean(check-expect (is-digit? "3") #true) (check-expect (is-digit? "w") #false) -
Write
only-digits: (Listof 1-String) -> (Listof 1-String).(check-expect (only-digits (list "1" "w" "3")) (list "1" "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") -
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)