Mad Libs: Difference between revisions

Add Factor example
(Add Factor example)
Line 877:
found a banana. Jack decided to take it home.ok
69> </lang>
 
=={{header|Factor}}==
<lang factor>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</lang>
{{out}}
<pre>
Enter a mad lib. A blank line signals end of input.
> <name> went for a walk in the park. <he or she>
> found a <noun>. <name> decided to take it home.
>
Enter a(n) name: William
Enter a(n) he or she: he
Enter a(n) noun: crystal
 
William went for a walk in the park. he
found a crystal. William decided to take it home.
</pre>
 
=={{header|Fortran}}==
1,808

edits