(archive 'newLISPer)

May 19, 2006

Putting Your Functions Away

Filed under: newLISP — newlisper @ 04:03

Have you ever been experimenting at the command line and realized the function you just wrote is useful in more than this experiment? I have. Many times. Especially in the beginning when I’m first learning a language. You’ll find out about some new function and in your excitement (you do get excited about programming in newLISP, don’t you?), you bang out a quick user function that this newfound function makes possible. When you’re all finished, there it is: (whatchamadoodle), the function of your dreams! Now what do you do? I’m sure all of us have our methods for making these at-the-moment functions more permanent.

Here’s something I’ve been using lately: (fn->file) (read “function to file”). It’s a simple function that takes a symbol for the name of the function you wish to save. Obey my wish, computer! :-)

(define (fn->file)
    ;! function-symbol
    ;! file-symbol (defaults to function-symbol)
    ;& source of function-symbol written to file
    ;= number of bytes written
        (string "/code/lisp/" (name (or (args 1) (args 0))) ".lisp")
        (source (args 0))))

The symbols I’m using after the comment symbol ; mean: required, optional, side effect, and result respectively, and they document the arguments, side effects, and results of thissy-here function. They have no effect, other than to satisfy my symbol lust, developed during years of studying architectural drawing :-)

append-file, string, name, or, args and source are all built-in functions, ready to go at start-up. Let’s see that leaves . . . huh? No user functions? Great! No more back story left. On with the show!

append-file takes a path and the string you want appended to the file at that location. string converts all of its arguments into strings and returns a single string made up of those arguments. name strips off the context portion of a symbol, returning the name part as a string. or evaluates each argument in turn till one returns a non-nil value. Here it is used to default the optional “file-symbol” argument to the required first argument “function-symbol”. args returns the list of arguments given to a function (possibly empty). And finally, source, which returns a string representation of the symbol of the function you supply.

Now that I look at it, it’s really a ridiculously simple function, isn’t it? But, in that moment you can just type:

> (fn->file 'whatchamadoodle)
> ;; is that a big enough function, or what?! :-)
> _

. . . and keep going with your experiment like nothing ever happened. The fun part comes later, when you wade through files and files of functions, trying to remember what possessed you to think a function that converts balloon magnet sizes into funny names that rhyme with time was a good candidate for a keeper :-)

m i c h a e l

;! required
;? optional
;& side effect
;= result


1 Comment »

  1. >Very nice usage of ‘source’ and ‘append-file’ :)’source’ (and also ‘save’) can be used to serialize anything in newLISP to a string or to a file. Many people use this to save and load data in a quick way, i.e. for configuration settings and simlar things or even medium size data sets. See how quickly it does it (timing numbers in millisecs on my Mac Mini ppc)> (time (set ‘data (sequence 1 100000)))58> (time (save “data.lsp” ‘data))1531> (time (load “data.lsp”))193>

    Comment by don Lucio — May 19, 2006 @ 21:12 | Reply

RSS feed for comments on this post. TrackBack URI

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out / Change )

Twitter picture

You are commenting using your Twitter account. Log Out / Change )

Facebook photo

You are commenting using your Facebook account. Log Out / Change )

Google+ photo

You are commenting using your Google+ account. Log Out / Change )

Connecting to %s

Create a free website or blog at WordPress.com.

%d bloggers like this: