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

Published on 12 May 2024 09:40 PM

How to resolve the algorithm Menu step by step in the Racket 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 Racket programming language

Source code in the racket programming language

#lang racket

(define (menu choices)
  (cond [(null? choices) ""]
        [else (for ([c choices] [i (in-naturals 1)]) (printf "~a. ~a\n" i c))
              (printf "Enter a number: ")
              (define n (string->number (read-line)))
              (or (and (exact-integer? n)
                       (<= 1 n (length choices))
                       (list-ref choices (sub1 n)))
                  (menu choices))]))

(menu '("fee fie" "huff and puff" "mirror mirror" "tick tock"))


  

You may also check:How to resolve the algorithm Average loop length step by step in the Scheme programming language
You may also check:How to resolve the algorithm Exponentiation operator step by step in the TI-57 programming language
You may also check:How to resolve the algorithm CRC-32 step by step in the C++ programming language
You may also check:How to resolve the algorithm Averages/Simple moving average step by step in the 360 Assembly programming language
You may also check:How to resolve the algorithm Hello world/Text step by step in the LFE programming language