Skip to content

Instantly share code, notes, and snippets.

@jbclements
Created May 18, 2020 17:27
Show Gist options
  • Save jbclements/0f5e11ec1e2503b23b4975f9eaaf33a1 to your computer and use it in GitHub Desktop.
Save jbclements/0f5e11ec1e2503b23b4975f9eaaf33a1 to your computer and use it in GitHub Desktop.
#lang racket
(define last-str #f)
(define (b-after-a? tree)
(match tree
[(? string? s)
(cond [(and (equal? s "b")
(equal? last-str "a"))
#t]
[else
(begin
(set! last-str s)
#f)])]
[else
(or (b-after-a? (car tree))
(b-after-a? (cdr tree)))]))
(require rackunit)
(check-equal? (b-after-a?
(cons (cons "a" "f")
(cons (cons "g" "z")
(cons "y" "b"))))
#f)
(check-equal? (b-after-a?
(cons (cons "a" "f")
(cons (cons "g" "a")
(cons "b" "b"))))
#t)
(define (b-after-a?/sps tree last-str)
(match tree
[(? string? s)
(cond [(and (equal? s "b")
(equal? last-str "a"))
(list #t "b")]
[else
(list #f s)])]
[else
(match (b-after-a?/sps (car tree) last-str)
[(list success last-str-2)
(if success
(list success last-str-2)
(b-after-a?/sps (cdr tree) last-str-2))])]))
(require rackunit)
(check-equal? (b-after-a?/sps
(cons (cons "a" "f")
(cons (cons "g" "z")
(cons "y" "b")))
#f)
(list #f "b"))
(check-equal? (b-after-a?/sps
(cons (cons "a" "f")
(cons (cons "g" "a")
(cons "b" "b")))
#f)
(list #t "b"))
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment