Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • Home
  • SEARCH
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 9177173
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 17, 20262026-06-17T17:16:04+00:00 2026-06-17T17:16:04+00:00

let rec move_robot (pos: int) (dir: string) (num_moves: int) : int = let new_forward_position

  • 0
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?

  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-06-17T17:16:05+00:00Added an answer on June 17, 2026 at 5:16 pm

    Here is a code that compiles:

    let rec move_robot pos dir num_moves =
        let new_forward_position = pos + num_moves in
        if new_forward_position > 99 then failwith "cannot move beyond 99 steps";
    
        let new_backward_position = pos - num_moves in
        if new_backward_position < 0 then failwith "cannot move less than 0 steps";
    
        begin match dir with
        | "forward" -> new_forward_position
        | "backward" -> new_backward_position
        end
    

    I modified several things:

    • Important: if foo then bar else qux is an expression in OCaml, which takes either the value bar or qux. Thus bar and qux needs to have the same type.
    • new_backward_position instead of new_backward_position pos
    • you don’t need type annotations : OCaml has type inference
    • no need for parentheses around the if clause
    • typo in new_forward position

    Also, with your code’s logic, let _ = move_robot 0 "forward" 5 fails. Shouldn’t it return 5 instead? I suggest you define a sum type for pos and do a pattern matching on it first.

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

The following code: exception NoElements of string let nth(k, list) = let rec loop
I have a simple f# quick sort function defined as: let rec qsort(xs:List<int>) =
let rec filtersList2fromList1 (List1:string list) (List2:string list) : string list = let finalList =
type node = { lan: string; lat: string; };; let rec read_nodes_from_list list =
Consider the following code: [<EntryPoint>] let main (args: string []) = let rec main
I often use this recursive 'visitor' in F# let rec visitor dir filter= seq
I like to define sequences recursively as follows: let rec startFrom x = seq
There is a Matrix transpose function: let rec transpose = function | (_::_)::_ as
Here is the code on sorting any given list: let rec sort lst =
A simple append function like this (in F#): let rec app s t =

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.