;; filterAbyB: list-of-num list-of-any --> list-of-num-and-any ;; replaces any element in listB according to the gate function, ;; which depends on the corresponding element in lonA. ;; The gateFn must have the following abstract contract: ;; gateFn: num --> function: symbol --> symbol (define (filterAbyB lonA listB gateFn) (cond [(empty? lonA) empty] [(empty? listB) empty] [(cons? lonA) (cons ((gateFn (first lonA)) (first listB)) (filterAbyB (rest lonA) (rest listB) gateFn))])) ;; stepFn: num --> function: sym --> sym ;; returns a no-op function if a > 0 ;; returns a function that returns '_ otherwise. (define (stepFn a) (cond [(>= 0 a) (lambda (s) '_)] [else (lambda (s) s)])) "filterAbyB test case, stepFn:" (equal? (list 'A 'B '_ 'D '_ '_ 'G) (filterAbyB (list 1 4 -2 3 -1 -3 2) (list 'A 'B 'C 'D 'E 'F 'G) stepFn)) ;;signFn: num --> function: sym --> sym ;; returns a function that prepends a "+" to the symbol if a >0 ;; otherwise returns a function that prepends a "-" to the symbol. (define (signFn a) (cond [(>= 0 a) (lambda (s) (string->symbol (string-append "-" (symbol->string s))))] [else (lambda (s) (string->symbol (string-append "+" (symbol->string s))))])) "filterAbyB test case, signFn:" (equal? (list '+A '+B '-C '+D '-E '-F '+G) (filterAbyB (list 1 4 -2 3 -1 -3 2) (list 'A 'B 'C 'D 'E 'F 'G) signFn))