CS 227 Lecture -*- Outline -*- Remember that the point is not the primitives, but string programming. ** Strings (6.2) Recall that strings are written in double quotes. Recall that to get a " in a string, use a backslash (\) before it. ------------------ STRING VALUES A string is a finite length sequence of characters. "" or "a string" STRING OBJECTS A string object is a sequence of characters, written in double quotes. "a string" INDEXES: 01234567 ------------------ *** basic operations Play with the following: string-length string-append show how string-append can take any number of args *** Substrings -- part of a string try: (substring "A fine day" 0 3) (substring "A fine day" 1 3) (substring "A fine day" 27 3) (substring "A fine day" 2 0) (substring "A fine day" 2 2) (substring "A fine day" 2 3) (substring "A fine day" 2 4) Summary: What does substring do? What constraints does it have? 0 <= start <= end <= length *** Comparisons try the following (string=? "aBc" "aBc") (string=? "aBc" " ") (string=? "aBc" "DDD) (string=? "aBc" "AbC") (string-ci=? "aBc" "AbC") (string-ci=? "aBc" "aBc") (string-ci=? "aBc" "DDD") Summary? ---------------- BASIC PROCEDURES FOR STRINGS string-length: (-> (string) integer) string-append: (-> (string ...) string) substring: (-> (string natural natural) string) ; REQUIRES: 0 <= arg2 <= arg3 <= length string=?: (-> (string string) boolean) string-ci=? (-> (string string) boolean) ---------------- also string "12345678" (string-insert "abc " "I learn" 0) ==> "abc I learn" TYPE: (-> (string string natural) string) ---------------- We do not have structure like lists for building strings. Instead, we do it "all at once" using string-append. ---------------- ;; Program 6.1 (define string-insert ;TYPE: (-> (string string natural) string) (lambda (insrt strng n) (string-append (substring strng 0 n) insrt (substring strng n (string-length strng))))) ---------------- *** iteration This is the usual way to do string stuff. Note it's iterative. One could write string-car, string-cdr, etc., but this is faster **** exercise 6.1 The following is exercise 6.1... check if it's in the HW. (substring? "s a s" "It's a str") ==> #t (substring? "" "abc") ==> #t (substring? "xy" "abc") ==> #f (substring? "xy" "abcxyxy") ==> #t (substring? "xy" "abcxey") ==> #f Develop this by - type? - working out algorithm idea (sliding the shorter string along) on the board - how to code the comparison? - how to do it over and over? - when to stop? What information is needed to prevent illegal use with substring? - any information not changing? put in let or letrec (define substring? ;TYPE: (-> (string string) boolean) (lambda (s1 s2) (let ((ls2 (string-length s2))) (letrec ((test-place ; TYPE: (-> (integer integer) boolean) (lambda (start end) ; REQUIRES: 0 <= start <= end <= 1 + ls2 ; ENSURES: result is true if s1 occurs as a substring of s2 ; in positions start to (- ls2 (string-length s1)) (cond ((< ls2 end) #f) ((string=? s1 (substring s2 start end)) #t) (else (test-place (add1 start) (add1 end))))))) (test-place 0 (string-length s1)))))) You can optimize this to take out the constant booleans... **** alternative --------------- (string-index "bush" "Trees and bushes") ==> 10 (string-index "Tr" "Trees and bushes") ==> 0 (string-index "xyzzy" "Trees and bushes") ==> -1 (string-index "xyzzy" "xyzzy") ==> 0 (string-index "xyzzy" "x") ==> -1 (string-index "z" "xyzzy") ==> 2 --------------- Develop this by - type? - working out algorithm idea (sliding the shorter string along) on the board - how to code the comparison? - how to do it over and over? - when to stop? What information is needed to prevent illegal use with substring? - any information not changing? put in let or letrec Can they code it now? (define string-index ;TYPE: (-> (string string) integer) (lambda (s1 s2) (let ((ls2 (string-length s2))) (letrec ((test-place ; TYPE: (-> (integer integer) number) (lambda (start end) ; REQUIRES: 0 <= start <= end <= 1 + ls2 ; ENSURES: result is true if s1 occurs as a substring of s2 ; in positions start to (- ls2 (string-length s1)) (cond ((< ls2 end) -1) ((string=? s1 (substring s2 start end)) start) (else (test-place (add1 start) (add1 end))))))) (test-place 0 (string-length s1))))))