;;; ;;; Part III. ;;; ; 1.) (define (string-hex->char s) (integer->char (string-hex->number s))) ; 2.) ; helper function (define (two-hex-chars->char c1 c2) (integer->char (+ (* 16 (char-hex->number c1)) (char-hex->number c2)))) ; helper function ; takes in a list of char, returns a new decoded list of char (define (decode-hex-charlist l) (cond ((null? l) '()) ((char=? #\+ (car l)) (cons #\space (decode-hex-charlist (cdr l)))) ((char=? #\% (car l)) (cons (two-hex-chars->char (cadr l) (caddr l)) (decode-hex-charlist (cdddr l)))) (else (cons (car l) (decode-hex-charlist (cdr l)))))) (define (decode-hex-string s) (list->string (decode-hex-charlist (string->list s)))) ; 3.) ;;; We will show two different versions of split. This first way ;;; converts to a list of characters, the second version will use ;;; string manipulations. ; converts a list of lists of characters to a list of strings (define (list-of-list-of-char->list-of-string l) (if (null? l) '() (cons (list->string (car l)) (list-of-list-of-char->list-of-string (cdr l))))) ; returns the position of the first occurence of a character within a list ; -1 if doesn't exist (define (first-position ch l) (cond ((null? l) -1) ((char=? ch (car l)) 0) (else (let ((x (first-position ch (cdr l)))) (if (= -1 x) -1 (+ 1 x)))))) ; grabs the first n elements of a list and returns them as a new list (define (grab-n n l) (if (= 0 n) '() (cons (car l) (grab-n (- n 1) (cdr l))))) ; expects a list of char, rather than a string (define (split-helper ch l) (let ((pos (first-position ch l))) (cond ((= -1 pos) (list l)) (else (cons (grab-n pos l) (split-helper ch (list-tail l (+ pos 1)))))))) (define (split ch str) (list-of-list-of-char->list-of-string (split-helper ch (string->list str)))) ;;; Second version of split: doesn't convert to a list of characters, ;;; but rather uses substring. We noticed that this is the way most of ;;; you implemented split. (define (index-tail-rec ind x lst) (cond ((null? lst) -1) ((equal? x (car lst)) ind) (else (index-tail-rec (+ ind 1) x (cdr lst))) ) ) (define (index x lst) (index-tail-rec 0 x lst) ) (define (string-index ch str) (index ch (string->list str)) ) (define (split ch str) (let ((ind (string-index ch str)) (len (string-length str))) (if (> 0 ind) (list str) (cons (substring str 0 ind) (split ch (substring str (+ ind 1) len))) ) ) )