How to resolve the algorithm Mad Libs step by step in the Factor programming language
Published on 12 May 2024 09:40 PM
How to resolve the algorithm Mad Libs step by step in the Factor programming language
Table of Contents
Problem Statement
Mad Libs is a phrasal template word game where one player prompts another for a list of words to substitute for blanks in a story, usually with funny results.
Write a program to create a Mad Libs like story. The program should read an arbitrary multiline story from input. The story will be terminated with a blank line. Then, find each replacement to be made within the story, ask the user for a word to replace it with, and make all the replacements. Stop when there are none left and print the final story.
The input should be an arbitrary story in the form:
Given this example, it should then ask for a name, a he or she and a noun (
Let's start with the solution:
Step by Step solution about How to resolve the algorithm Mad Libs step by step in the Factor programming language
Source code in the factor programming language
USING: formatting io kernel make regexp sequences sets splitting ;
IN: rosetta-code.mad-libs
: get-mad-lib ( -- str )
"Enter a mad lib. A blank line signals end of input." print
[
[ "> " write flush readln dup , empty? f t ? ] loop
] { } make harvest "\n" join ;
: find-replacements ( str -- seq )
R/ <[\w\s]+>/ all-matching-subseqs members ;
: query ( str -- str )
rest but-last "Enter a(n) %s: " printf flush readln ;
: replacements ( str seq -- str )
dup [ query ] map [ replace ] 2each ;
: mad-libs ( -- )
get-mad-lib dup find-replacements replacements nl print ;
MAIN: mad-libs
You may also check:How to resolve the algorithm Mastermind step by step in the Wren programming language
You may also check:How to resolve the algorithm Ludic numbers step by step in the Python programming language
You may also check:How to resolve the algorithm Delete a file step by step in the Perl programming language
You may also check:How to resolve the algorithm Ethiopian multiplication step by step in the Eiffel programming language
You may also check:How to resolve the algorithm World Cup group stage step by step in the Lua programming language