let rec move_robot (pos: int) (dir: string) (num_moves: int) : int =
let new_forward_position = pos + num_moves in
if (new_forward_position > 99) then failwith "cannot move beyond 99 steps"
else new_forward_position
let new_backward_position = pos - num_moves in
if (new_backward_position pos < 0) then failwith "cannot move less than 0 steps"
else new_backward_position
begin match dir with
| "forward" -> new_forward position
| "backward" -> new_backward_position
end
I keep on getting “unexpected token in” for the let new_backward_position line. What is my error?
Here is a code that compiles:
I modified several things:
if foo then bar else quxis an expression in OCaml, which takes either the valuebarorqux. Thusbarandquxneeds to have the same type.new_backward_positioninstead ofnew_backward_position posnew_forward positionAlso, with your code’s logic,
let _ = move_robot 0 "forward" 5fails. Shouldn’t it return 5 instead? I suggest you define a sum type forposand do a pattern matching on it first.