Created
February 17, 2021 19:18
-
-
Save shaunagm/93681d91cddfb6108d5f301bbc92ea58 to your computer and use it in GitHub Desktop.
Madlibs Iteration 10
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 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