Last active
February 17, 2021 22:03
-
-
Save shaunagm/5a0099678b8cb38b49b8f5f3326bdb24 to your computer and use it in GitHub Desktop.
Madlibs Iteration 11
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
#lang at-exp racket | |
(require (for-syntax racket/match)) | |
(define prompt-responses | |
; Hash's list must be of cons of key value pairs such as (cons adjective null) | |
(make-hash (list ))) | |
(define (ask prompt) | |
(display prompt) | |
(read-line)) | |
(define (lookup-or-ask var prompt) | |
; Given variable & prompt, checks if we've recorded a response and prompts as needed. Returns stored/new response. | |
(if (null? (hash-ref prompt-responses var null)) | |
(let ([response (ask prompt)]) | |
(hash-set! prompt-responses var response)) | |
null) | |
(hash-ref prompt-responses var null)) | |
(define-syntax (blank stx) | |
(match (syntax->datum stx) | |
[(list _ var prompt) | |
(datum->syntax stx `(lookup-or-ask ,var ,prompt))])) | |
;; Story templates | |
(define-syntax (story-template stx) | |
(match (syntax->datum stx) | |
[(list _ story-name parts) | |
(datum->syntax stx `(define (,(string->symbol story-name)) (string-join ,parts "")))])) | |
(story-template "tree-story" (list | |
"There once was a big " (blank "adjective" "Adjective: ") | |
" tree in the old forest of " (blank "location" "Location: ") | |
". Yes that's right, in " (blank "location" "discard") ".")) | |
(story-template "river-story" (list | |
"The " (blank "adjective" "Adjective: ") " " (blank "adjective2" "Adjective: ") | |
" river ran to the " (blank "adjective" "discard") " " (blank "adjective2" "discard") | |
" sea.")) |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment