Skip to content

Instantly share code, notes, and snippets.

@shaunagm
Created February 17, 2021 19:18
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/93681d91cddfb6108d5f301bbc92ea58 to your computer and use it in GitHub Desktop.
Save shaunagm/93681d91cddfb6108d5f301bbc92ea58 to your computer and use it in GitHub Desktop.
Madlibs Iteration 10
#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 a variable and a prompt, checks if we've already recorded a response and prompts if necessary. Returns
; stored or 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
(string-join (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" "discarded")) "")
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment