Here's a little something I was playing with, the notion of context
switching. What does a certain symbol (er, parameter) mean in a given
context? Can parameters be grouped together into meaningful context
objects that change a bunch of parameters at once?

The motivation is for my text adventure thing, which has any number of
parameters that change simultaneously, such as current room, current
number of steps, current time of day, current status of unhatched eggs
etc. But also just because it's a kind of neat idea. I envision having a
set of contexts that refer to each other's parameters, that you just
throw together and they all plug everything into the right spot and form
a cohesive fully functioning program.

Yeah after reading some of the code of LambdaMOO again and seeing I
quote "({...@first, @$list_utils:map_verb(args[1] ? listdelete(args[1], 1)
| {}, v)}, @listdelete(args, 1))" I figured it might be easier to make a
scheme/racket MU than to get this junk to work.
#lang racket/base

(define-struct context (parameters values))

(define (build-context stuff)
  (make-context (map car stuff) (map cdr stuff)))

(define (with-context context proc)
  (let loop ((parameters (context-parameters context)) (values (context-values 
context)))
    (if (null? parameters) (proc)
        (parameterize
            (((car parameters) (car values)))
          (loop (cdr parameters) (cdr values))))))


(define one (make-parameter 1))

(define print (make-parameter display))

(define my-+ (make-parameter +))

(define (do-a-thing)
  ((print) ((my-+) (one) (one))))

(define special-circumstances
  (build-context
   `((,one . ,42)
     (,print . ,(λ (item)
                  (display (string-append "~*~" item "~*~"))))
     (,my-+ . ,(λ (a b)
                (format "~a *** ~a" a b))))))

(display "Normally 1+1=")
(do-a-thing)
(newline)

(display "With a different context 1+1=")
(with-context special-circumstances do-a-thing)
(newline)
_________________________________________________
  For list-related administrative tasks:
  http://lists.racket-lang.org/listinfo/users

Reply via email to