The Boston Diaries

The ongoing saga of a programmer who doesn't live in Boston, nor does he even like Boston, but yet named his weblog/journal “The Boston Diaries.”

Go figure.

Monday, March 13, 2006

RACTER, generative text and Lisp. Oh my! Part II

I found programming in LispSchemeguile to be quite frustrating—partly due to it being an unfamiliar environment to program in and partly due to not fully understanding the implications of my design decisions for the generative text platform I'm developing.

Ideally, I wanted something like:

(define firstname
  #(
  	"Alice"
  	"Bob"
  	"Carol"
  	"Dave"
  )
)

(define lastname
  #(
  	"Smith"
  	"Jones"
  	"Conner"
  )
)

(define heros	;; from scifi.scm from the other day
  #(
        "Tim Conway"
        "Big Bird"
        "The Fonz"
        "Wonder Woman"
        "Kareem Abdul Jabar"
        "Tom Selleck"
        "Lucille Ball"
	#((ref firstname) " " (ref lastname))
  )
)

Where heros will be one of “Tim Conway” or a generated named like “Carol Jones.” And it was handling this “second level” of references where I had difficulty (lots of unbound and/or undefined references—God I can't stand dynamic languages). I spend hours trying to figure out how to iterate through an array. I mean, in a C-like language it would be something like:

for (i = 0 ; i < array.length ; i++)
{
  if (typeof(array[i]) == string) 
    result.append(array[i]);
  else if (typeof(array[i]) == array)
    result.append(expand_array(array[i]));
}

But LispSchemeguile is rather lacking in the loop department; instead you're supposed to use tail recursion or one of the mapping functions and LispSchemeguile's support of arrays isn't quite as dynamic as say, Perl, and well …

I found the experience rather trying.

But in the end the code turned out to be rather simple. Well, once I “fixed” the data structures being used:

(define firstname
  #(
  	"Alice"
  	"Bob"
  	"Carol"
  	"Dave"
  )
)

(define lastname
  #(
  	"Smith"
  	"Jones"
  	"Conner"
  )
)

(define heros	;; from scifi.scm from the other day
  #(
        "Tim Conway"
        "Big Bird"
        "The Fonz"
        "Wonder Woman"
        "Kareem Abdul Jabar"
        "Tom Selleck"
        "Lucille Ball"
	((ref firstname) " " (ref lastname)) ;; look! a list!  Not an array!
  )
)

(define rndstate 0)
(define refn     array-ref) ;; return an element from an array

;; need this to get random numbers out of (random)
(set! rndstate (seed->random-state (current-time)))

(define (nref a) (car (array-dimensions a)))
(define (ref a)
  (let
    ((result (refn a (random (nref a) rndstate))))
    (cond
      ((string? result) result)
      ((list?   result) (eval
                                (cons 'string-append result)
                                (interaction-environment)
                        ))
    )
  )
)

Now that this is working, adding additional functions and variables should be straightforward. And today's example is a guile version of my Quick-n-Dirty B-Movie Plot Generator.

Obligatory Picture

[It's the most wonderful time of the year!]

Obligatory Contact Info

Obligatory Feeds

Obligatory Links

Obligatory Miscellaneous

You have my permission to link freely to any entry here. Go ahead, I won't bite. I promise.

The dates are the permanent links to that day's entries (or entry, if there is only one entry). The titles are the permanent links to that entry only. The format for the links are simple: Start with the base link for this site: http://boston.conman.org/, then add the date you are interested in, say 2000/08/01, so that would make the final URL:

http://boston.conman.org/2000/08/01

You can also specify the entire month by leaving off the day portion. You can even select an arbitrary portion of time.

You may also note subtle shading of the links and that's intentional: the “closer” the link is (relative to the page) the “brighter” it appears. It's an experiment in using color shading to denote the distance a link is from here. If you don't notice it, don't worry; it's not all that important.

It is assumed that every brand name, slogan, corporate name, symbol, design element, et cetera mentioned in these pages is a protected and/or trademarked entity, the sole property of its owner(s), and acknowledgement of this status is implied.

Copyright © 1999-2022 by Sean Conner. All Rights Reserved.