How to resolve the algorithm Menu step by step in the Brat programming language

Published on 12 May 2024 09:40 PM

How to resolve the algorithm Menu step by step in the Brat programming language

Table of Contents

Problem Statement

Given a prompt and a list containing a number of strings of which one is to be selected, create a function that:

The function should reject input that is not an integer or is out of range by redisplaying the whole menu before asking again for a number. The function should return an empty string if called with an empty list. For test purposes use the following four phrases in a list: This task is fashioned after the action of the Bash select statement.

Let's start with the solution:

Step by Step solution about How to resolve the algorithm Menu step by step in the Brat programming language

Source code in the brat programming language

menu = { prompt, choices |
  true? choices.empty?
  { "" }
  {
    choices.each_with_index { c, i |
      p "#{i}. #{c}"
    }

    selection = ask prompt

      true? selection.numeric?
      { selection = selection.to_i
        true? selection < 0 || { selection >= choices.length }
          { p "Selection is out of range"; menu prompt, choices }
          { choices[selection] }
      }
    { p "Selection must be a number"; menu prompt, choices }
  }
}

p menu "Selection: " ["fee fie" "huff and puff" "mirror mirror" "tick tock"]

  

You may also check:How to resolve the algorithm Integer comparison step by step in the Raven programming language
You may also check:How to resolve the algorithm Almost prime step by step in the Mathematica / Wolfram Language programming language
You may also check:How to resolve the algorithm Loops/Continue step by step in the Metafont programming language
You may also check:How to resolve the algorithm Hostname step by step in the Maple programming language
You may also check:How to resolve the algorithm Least common multiple step by step in the FunL programming language