#lang shplait type Instruction | left() | right() | forward(distance :: Int) // Template for a program that take an `Instruction`: #// fun path_distance(i :: Instruction) :: Int: match i | left(): .... | right(): .... | forward(d): .... fun path_distance(i :: Instruction) :: Int: match i | left(): 0 | right(): 0 | forward(d): d check: path_distance(left()) ~is 0 check: path_distance(right()) ~is 0 check: path_distance(forward(5)) ~is 5 // A list of Instructions // (Listof Instruction) // [The "#//" below comments out the `define-type` form, // since `ListOf` is built-in] #// type Listof_Instruction | empty() | cons(i :: Instruction, rest :: Listof_Instruction) // Template for a program that take a `(Listof Instruction)`: #// fun program_distance(li :: Listof(Instruction)) :: Int: match li | []: .... | cons(i, is): (path_distance i) .... (program_distance is) fun program_distance(li :: Listof(Instruction)) :: Int: match li | []: 0 | cons(i, is): path_distance(i) + program_distance(is) check: program_distance([]) ~is 0 check: program_distance(cons(forward(5), (cons(left(),[])))) ~is 5 check: program_distance([forward(5), left()]) ~is 5 fun does_turn(li :: Listof(Instruction)) :: Boolean: match li | []: #false | cons(i, is): is_turn(i) || does_turn(is) fun is_turn(i :: Instruction) :: Boolean: match i | left(): #true | right(): #true | forward(d): #false check: does_turn([]) ~is #false check: does_turn([forward(5), left()]) ~is #true check: does_turn([forward(3), forward(5)]) ~is #false check: is_turn(left()) ~is #true check: is_turn(right()) ~is #true check: is_turn(forward(4)) ~is #false