Here is my solution to a modified version of DNAprefix from exercise 17.6.6. I feel like it *should* be able to be written more clearly/simply but I can't think of the logic to make it work properly:

Modify |DNAprefix| so that it returns the first item beyond the pattern in the search-string if the pattern is a proper prefix of the search-string. If the lists do not match or if the pattern is no shorter than the search-string, the modified function should still return |false|. Similarly, if the lists are equally long and match, the result is still |true|.

(check-expect (symbol=? (DNAprefix (list 'a 't) (list 'a 't 'c)) 'c) true)
(check-expect (not (DNAprefix (list 'a 't) (list 'a))) true)
(check-expect (DNAprefix (list 'a 't) (list 'a 't)) true)

;; DNAprefix : list-of-symbols list-of-symbols -> boolean
;; to check if pattern is a prefix of the search-string
(define (DNAprefix pattern search-string)
  (cond
    [(and (empty? search-string) (not (empty? pattern))) false]
    [(and (empty? search-string) (empty? pattern)) true]
    [(empty? pattern) (first search-string)]
[(boolean? (DNAprefix (rest pattern) (rest search-string))) (and (symbol=? (first pattern) (first search-string)) (DNAprefix (rest pattern) (rest search-string)))]
    [else (DNAprefix (rest pattern) (rest search-string))]))
_________________________________________________
  For list-related administrative tasks:
  http://lists.racket-lang.org/listinfo/users

Reply via email to