#lang plait ;; Check whether a robot program has any turns. (print-only-errors #t) (define-type Instruction (drive-forward [dist : Number]) (turn-left) (turn-right)) ;(define (how-far [ins : Instruction]) : Number ; (type-case Instruction ins ; [(drive-forward d) ... d] ; [(turn-left) ...] ; [(turn-right) ...])) #; (define-type (Listof X) empty (cons [f : X] [r : (Listof X)])) ;(define (total-distance [instrs : (Listof Instruction)]) : Number ; (type-case (Listof Instruction) instrs ; [empty ...] ; [(cons f r) ...(how-far f) ... (total-distance r)...]) (define (is-turn? [ins : Instruction]) : Boolean (type-case Instruction ins [(drive-forward d) #f] [(turn-left) #t] [(turn-right) #t])) (test (is-turn? (drive-forward 3)) #f) (test (is-turn? (turn-left)) #t) (test (is-turn? (turn-right)) #t) (define (has-turns? [instrs : (Listof Instruction)]) : Boolean (type-case (Listof Instruction) instrs [empty #f] [(cons f r) (or (is-turn? f) (has-turns? r))]) ) (test (has-turns? empty) #f) (test (has-turns? (cons (turn-left) empty)) #t) (test (has-turns? (cons (drive-forward 5) empty)) #f)