Note: For this kind of post I don’t mind if people don’t reply. It helps me to write this stuff out for sharing, and if anyone’s interested and has comments, of course it's fine if they reply.
https://people.eecs.berkeley.edu/~bh/ssch1/showing.html
> We're about to show you a few examples of (we hope) interesting programs in Scheme. Play with them! Type them into your computer and try invoking them with different data. Again, don't worry too much if something doesn't work—it probably just means that you left out a parenthesis, or some such thing.
>
> While you're going through these examples, see how much you can figure out for yourself about how they work. In particular, try guessing what the names of procedures, such as first and keep, mean. Some of them will probably be obvious, some of them harder. The point isn't to see how smart you are, but to get you thinking about the kinds of things you want to be able to do in a computer program. Later on we'll go through these examples in excruciating detail and tell you the official meanings of all the pieces.
>
> Besides learning the vocabulary of Scheme, another point of this activity is to give you a feeling for the ways in which we put these names together in a program. Every programming language has its own flavor. For example, if you've programmed before in other languages, you may be surprised not to find anything that says print in these examples.
>
> On the other hand, some of these examples are programs that we won't expect you to understand fully until most of the way through this book. So don't worry if something doesn't make sense; just try to get some of the flavor of Scheme programming.
> Example: Pig Latin
> (define (pigl wd)
> (if (member? (first wd) 'aeiou)
> (word wd 'ay)
> (pigl (word (butfirst wd) (first wd)))))
> (By the way, if you've used other programming languages before, don't fall into the trap of thinking that each line of the pigl definition is a "statement" and that they are executed one after the other. That's not how it works in Scheme. The entire thing is a single expression, and what counts is the grouping with parentheses. Starting a new line is no different from a space between words as far as Scheme is concerned. We could have defined pigl on one humongous line and it would mean the same thing. Also, Scheme doesn't care about how we've indented the lines so that subexpressions line up under each other. We do that only to make the program more readable for human beings.)
>
> The procedure follows one of two possible paths, depending on whether the first letter of the given word is a vowel. If so, pigl just adds the letters ay at the end:
# My rewording of what they say in the text about the procedure
Don’t think of the procedure as happening in an order.
The entire thing is a single expression, and what counts is the parentheses groupings. New lines and indentations don’t mean anything.
The procedures follows one of two paths, depending on whether the first letter of the word it’s given is a vowel. If the first letter of the word is a vowel, the procedure adds “ay” to the end of the word.
# What else I think is happening in the procedure
If the first letter of the word is not a vowel, the procedure moves the first letter to the end of the word and then calls the same procedure on that result.
This procedure is recursive. That means it calls itself. This is my starting idea of what “recursive” means. This definition of “recursive” fits with:
https://en.wikipedia.org/wiki/Recursion
> Informal definition
> Recursion is the process a procedure goes through when one of the steps of the procedure involves invoking the procedure itself. A procedure that goes through recursion is said to be 'recursive'.
“word” pushes words or letters together to make a word.
“if” is followed by three things, each in its own parentheses. The first is something that evaluates to true or false. The second is what you do if the first is true. The third is what you do if the first is false. Or instead of false, is it not-true? What does the procedure do if the first is neither true nor false? I can try testing that.
# Prediction
Back to the book:
https://people.eecs.berkeley.edu/~bh/ssch1/showing.html
> You've seen every before, in the acronym example, but we haven't told you what it does. Try to guess what Scheme will respond when you type this:
>
> (every pigl '(the ballad of john and yoko))
My prediction:
ethay alladbay ofay ohnjay anday okoyay
What it actually said:
#<procedure:...imply Scheme.rkt:748:2>
(ethay alladbay ofay ohnjay anday okoyay)
I didn’t know what the first line meant. I looked again at what I had done and saw that I had copied
> (every pigl '(the ballad of john and yoko))
from my notes instead of
(every pigl '(the ballad of john and yoko))
I could have avoided that error by looking at what I pasted before hitting “return”. That seems like a good thing to do in general.
I tried again without the “>” and the result was just
(ethay alladbay ofay ohnjay anday okoyay)
Another error on my part: I did not predict the parentheses. Maybe if I had done my experimenting with the procedure first before doing my prediction, I’d have correctly predicted the parentheses. Or maybe not. I’ll try to pay more attention to what has parentheses around it. I think in this case that the parentheses make the things in the answer into a group, which makes sense because the argument for the procedure was also a group, in parentheses.
# Experimenting
Next I’ll do some experimenting with the procedure. If I post it, that’ll be a separate post. Questions for the experimenting phase:
1) What happens if the first argument of “if” doesn’t evaluate to either true or false?
2) Does “first” do what I think it does, which is to return the first letter of a word?
3) Does “member?” do what I think it does, which is to return “#t” if its first argument is one of the things in the second argument, and “#f” if it isn’t?
4) Does “word” do what I think it does, which is to combine things into a single word? Can its arguments be either single letters or words? My guess is yes. Or maybe a single letter is just a special case of a word, so it’s a word anyway.
5) Does “butfirst” do what I think it does, which is to return everything except the first letter of the given word?
6) I could experiment some with “if” to see if it does what I wrote above.
7) I could try things like leaving off an open or a closed parenthesis, or leaving off sets of parentheses, to see what happens.
8) I could look up the procedures in this program and see if the definitions I find match what I predict the procedures do.
9) Do all procedures with names that end in a “?” return a “#t” or “#f” or maybe some kind of “undefined” value? This is something I can look up.
# Learning process
I’m doing this to practice learning something as much as to learn about programming. My questions for myself are:
1) Am I going into enough detail on this exercise?
2) Should I have looked up more things?
3) Did I write down enough of the things that went through my head?
3) Did I spot some errors that I made and was I comfortable writing them down? Can I fix my errors that I found and that anyone else points out? Will I be less likely to make those same errors in the future?
4) This post is long. Maybe I’m trying to do too much at once.