Scheme: How to Assign Variables

This can't be healthy.
Part of what makes scheme such a bloody mess is the variable situation. If you’ve ever found yourself chewing at the curtains wondering why, dear god, why the console won’t stop with the unbound variable errors, chances are something is wrong with the way you wrote one or all of your variables.

That’s why I’m doing a short walk-through of scheme variables. Local ones, anyway.

The Formula for Scheme Variables

(
let (   (variable number)   (variable number)   (variable number) )
(expressions)
)

formula

Yup, good luck tracking all those parentheses.

The Formula for Scheme Lists

(let* ((listname ‘(num num num))) listname)

The second listname makes the Script-Fu console return the list.What if you don’t want to return the entire list? That’s where the car and cdr functions come in handy.

(let* ((listname ‘(num num num)))(car listname)); Returns the 1st value of a list
(let* ((listname ‘(num num num)))(cdr listname)); Returns the rest of the list

I was gonna go with clusterfuck but decided hell hole is more accurate.

So here’s the piece that’s gonna pull your hair out. Accessing any value of a list requires that you use a combination of a’s (for 1st value of the list) and d’s (for the rest of the list). To simplify this, I’ve just started thinking of d as “drop this item in the list,” so cdr cuts one item, cddr cuts two items, cdddr cuts three items and so on. Adding an a to that gives you the first value that hasn’t been dropped.

Now this... This is a clusterfuck.

Funny story. That only works until you try caddddr at which point the console will slap you with an eval: unbound variable: caddddr error. Because reasons. That’s okay though. For our purposes, we’re only going to use the car function. If you, however, happen to know why scheme does this, please post it in the comments.

Why Do I Need to Know the Formula for Scheme Lists?

Because functions in GIMP don’t return values. They return lists with one item. Who thought up that bullshit? No idea.

The Procedure Browser tells us that the new image function takes three parameters.

(gimp-image-new WidthOfImage HeightOfImage TypeofImage)

It returns an image number. You’ll need that again if you want to display the image.

(gimp-display-new ImageNumber)

And to add text to the image.

(gimp-text-fontname ImageNumber Layer* X Y Text” Border Antialias Size SizeType FontName)

*New Layer = -1

So to make things less complicated, that image number really needs to be assigned to a variable. Since technically gimp-image-new returns a list with one item, you’ll need to use car to assign an image number variable.

The Simplest Scheme Script of All Time

(let *
(
(MyImage (car (gimp-image-new 100 100 RGB) ) )
)
(gimp-text-fontname MyImage -1 0 0 "Hello" 0 FALSE 20 0 "Sans")
(gimp-display-new MyImage)
)

A Breakdown

There's gotta be an easier way of doing this...

So in plain human-speak, let* makes the variable MyImage equal the value of the new image.The MyImage variable then gets plopped into the text and display functions to make life easy.

This is why you’ll see car all over Scheme scripts in Gimp. It’s just making variables of whatever the functions spit out.

A Warning Before Continuing

Seriously? SERIOUSLY?!

Take a good look at this screenshot. Notice how the script looks EXACTLY the same. Script-Fu does not give a fuck. If you copy/paste from a different program, the quotation marks can register differently, and you might get an unbound variable error.

How long did it take me to figure that out? A FULL HOUR. An hour of my life I’m not getting back. I’m going back to Python to shore up my sanity.

 

 

 

 

Advertisements
Tagged , , , , ,

3 thoughts on “Scheme: How to Assign Variables

  1. codeinfig says:

    yeah… id rather use python 🙂 lisp and co are fantastic languages that should inspire everyone, but never were my cup of tea. ive tried scheme, and racket, (dr racket is pretty cool) but if i wanted a language full of parentheses like that, id finish/fix that recursive parser i tried to write a couple times, or just use someone elses recursive parser (there are plenty for the taking.)

    whether youre making a language in python, c++, js or even php, you can have all the parentheses you want. instead i just added inline python– so you can have all the parentheses python has! lisp, and curly braces, i get enough of that sort of thing in js. to each his own, though. i mean lisp has major strengths, thats why it keeps coming back.

    Liked by 1 person

  2. adsatis says:

    Ahahah, I learned to program on Scheme back in university, and good gods it was a hellhole of parenthesis indeed.

    Liked by 1 person

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

%d bloggers like this: