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) )
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
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.
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.
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) )
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
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.
3 Replies to “Scheme: How to Assign Variables”
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.
LikeLiked by 1 person
Agreed. Python all the way. It’s hands down my favorite language so far.
LikeLiked by 1 person
Ahahah, I learned to program on Scheme back in university, and good gods it was a hellhole of parenthesis indeed.
LikeLiked by 1 person