Skip to content

Instantly share code, notes, and snippets.

@shaunagm
Last active February 17, 2021 22:03
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save shaunagm/5a0099678b8cb38b49b8f5f3326bdb24 to your computer and use it in GitHub Desktop.
Save shaunagm/5a0099678b8cb38b49b8f5f3326bdb24 to your computer and use it in GitHub Desktop.
Madlibs Iteration 11
#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