[Simply Scheme] Pig Latin Example

26 views
Skip to first unread message

Anne B

unread,
Feb 6, 2020, 4:13:33 PM2/6/20
to fallibl...@googlegroups.com
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.

Elliot Temple

unread,
Feb 6, 2020, 4:41:18 PM2/6/20
to fallibl...@googlegroups.com
You have to give “if” true or false as the first input, or else it’s an error, a crash, an invalid program, something like that.

In some languages, every value counts as either true or false. They can all be automatically converted. In Ruby, basically everything is true except for a few things. 5 is true. 0 is true. the string “cat” is true. the empty string “” is true too. false is false and nil (null value) is false. people often use a “blank?” function which returns false for empty things like “” or [] (empty list). blank? still counts 0 as true. the question mark in the name is just a naming convention for functions that return true or false, it has no actual code meaning, and it’s only a convention for some programming languages.

Some languages are different, e.g. they won’t auto-convert things to true or false, so you need to use a function that returns true or false or a boolean variable, not a string, integer, list, etc. And in some languages 0 is false. sometimes “” and other empty/blank type stuff is false too, e.g. “” is false in javascript.

offhand, i forget how scheme handles all this.

Elliot Temple
www.fallibleideas.com

Anne B

unread,
Feb 7, 2020, 1:06:02 PM2/7/20
to fallibl...@googlegroups.com
Chapter 6 of Simply Scheme is called True and False.

https://people.eecs.berkeley.edu/~bh/ssch6/true.html

It says:

> Everything That Isn't False Is True
>
> #T isn't the only true value. In fact, every value is considered true except for #f.
>
> > (if (+ 3 4) 'yes 'no)
> YES

I skimmed some of this chapter. I’ll leave the rest for when I get to it, going through the book in order.

Also, something I said above isn’t right:

>> “if” is followed by three things, each in its own parentheses.

I found an example where one of the three things after an “if” is not in parentheses. So my guess is that the parentheses are not needed if an argument consists of one word (a letter is a word) or one numeral or symbol or something.

From Chapter 6 again:

> Here's a procedure that returns the absolute value of a number:
>
> (define (
> abs num)
> (if (< num 0)
> (- num)
> num))

Anne B

unread,
Feb 7, 2020, 4:13:23 PM2/7/20
to fallibl...@googlegroups.com
Before working on the questions from my previous post, I had another idea. I pasted the rest of the examples from the text into DrRacket, to make sure it gave the same answers as what they had printed in the text. It did.

I found that I can define a procedure in either the top window or the bottom window of DrRacket. If I then use the procedure, it’ll work.

Actually, that might not be right. I only tried it with this example from the text:

> (define (rotate wd)
(word (butfirst wd) (first wd)))

> (rotate ‘spaghetti)

It’s possible that “rotate" is already defined in the Scheme stuff that I loaded in.

To test this if it is, I next tried quitting and restarting DrRacket, running the Simply Scheme stuff in the top window, not defining “rotate”, then trying to use “rotate”.

I got an error message:

rotate: undefined;
cannot reference an identifier before its definition

As a double check, I searched the Simply Scheme code for “rotate” and didn’t find it.

Then I put the definition of “rotate” in the top window and saved my file. That way it’ll be there each time I reload that file.

I tried entering

(rotate ‘spaghetti)

in the bottom window, and I got an error message. Oops, I forgot to “Run” after I changed the top window. After I did “Run”, “rotate” worked as expected.

Question: What happens if I define something in the top window, and then redefine it another way, also in the top window? Would the second definition be used and the first one ignored?

I tested this by adding a different definition of “rotate” under the first one in the top window, then saving and running.

My test, nonsense definition of “rotate”:

(define (rotate wd)
(word (first wd) (butfirst wd) (first wd)))

I got the error message

module: identifier already defined in: rotate

Interesting. It wouldn’t let me define rotate in a different way from how it had already been defined. That’s useful. You’d want to know if you defined the same procedure in two different ways.


Anne B

unread,
Feb 7, 2020, 4:35:35 PM2/7/20
to fallibl...@googlegroups.com
Oops, formatting problems on my previous post.

I’ll fix and postmortem later or tomorrow.

Anne B

unread,
Feb 8, 2020, 3:21:09 PM2/8/20
to fallibl...@googlegroups.com
Postmortem. Two reasons for my formatting errors:

1) I was composing my email in Pages and then pasting it into Mail. In my draft, I had quotes in a different color. I intended to add quoting levels when I pasted it into Mail. That was a bad idea because sometimes I’ll forget to do that. I’ll go back to composing in Mail, and then copying from there if I want to save a copy of what I write outside of Mail.

2) In some cases, like the first quotation below the “====", I want to show something I typed in the bottom window of DrRacket. The “>” is the prompt given by DrRacket. I want to be able to show it so it’s clear what I typed (after a prompt) and what the program returned (no prompt). The problem is that Mail interprets “>” as showing a quote level rather than a character I want to type. I tried some tests by emailing to myself. I tried a space in front of the “>” and that didn’t work. I tried an apostrophe in front of the “>” and that did seem to work. (I got the idea of using the apostrophe from how Scheme uses apostrophes, which I had already started writing about in another post.) So in my first quotation below, the things preceded by “‘>” are things that I typed in, and the things not preceded by “‘>” are the responses from the program. I’ll go with this for now; maybe I’ll find something better later.

One reason I made both these mistakes is I posted that email too fast. I didn’t proofread thoughtfully enough.

Maybe I was rushing because I was telling myself that I “should” write more posts. I’m trying to stop thinking I should do things. Then when I noticed my formatting errors, I did the pressuring-myself thing again:

> I’ll fix and postmortem later or tomorrow.

The “later or tomorrow” part was adding time pressure that wasn’t needed.



Below is the part of the post I wrote, with better formatting.

====

Before working on the questions from my previous post, I had another idea. I pasted the rest of the examples from the text into DrRacket, to make sure it gave the same answers as what they had printed in the text. It did.

I found that I can define a procedure in either the top window or the bottom window of DrRacket. If I then use the procedure, it’ll work.

Actually, that might not be right. I only tried it with this example from the text:

> ‘> (define (rotate wd)
> (word (butfirst wd) (first wd)))
>
> ‘> (rotate ‘spaghetti)

Maybe it’s possible that “rotate" is already defined in the Scheme stuff that I loaded in.

Anne B

unread,
Feb 13, 2020, 12:16:59 PM2/13/20
to fallibl...@googlegroups.com
On Feb 6, 2020, at 4:13 PM, Anne B <anne...@gmail.com> wrote:

> 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)))))


> # 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:

> 2) Does “first” do what I think it does, which is to return the first letter of a word?


——

Actually, before I work on the given question, I want to know what’s with the apostrophe in front of some words.

An answer is here in Chapter 5: Words and Sentences:

https://people.eecs.berkeley.edu/~bh/ssch5/words.html

The apostrophe before a word means that it’s talking about the word itself, not whatever the word might evaluate to.

> The difference between a thing and its name is one of the important ideas that programmers need to understand.

Okay. So what about something that can’t be evaluated? Would the apostrophe not be necessary then? Or maybe everything can be evaluated? I did a test:

> '> (first 'grass)
> g
> '> (first grass))
> . . grass: undefined;
> cannot reference an identifier before its definition

It looks like the apostrophe is necessary if you want something to be treated as a word. Without the apostrophe, it’ll try to evaluate the word and return an error message when it can’t. So it’s not that everything can be evaluated, it’s that the program will try to evaluate everything unless it’s told not to.

——

Then I wanted to see what happens if you leave out the parentheses:

> '> first 'grass
> #<procedure:first>
> grass

I would not have guessed what the message "#<procedure:first>” meant. So I wondered how I’d find out if I couldn’t figure out on my own what the error was.

First I searched for “Scheme #<procedure”. I didn’t find anything useful.

Searching for “DrRacket error messages” was better.

The following video seemed like it might be helpful, but it didn’t answer this particular question. It was also made in 2011, so DrRacket may have changed since then.

> https://www.youtube.com/watch?v=Z182Wd8SiTQ

However, I thought it was a clear video, and I saw there were others in the series, so I saved a link to the guy’s channel in case I want to look at some of his other DrRacket videos in the future.

Then I found

https://docs.racket-lang.org/reference/exns.html

which doesn’t answer the current question. But it reminded me that I could look on the DrRacket website. I should have done that first and should do it first next time I have this kind of question.

Then I searched "DrRacket #<procedure”. I didn’t find anything to answer the current question. But again I found something I thought was useful enough to read through. It’s an introduction to DrRacket written for a course in 2019:

https://www.cs.grinnell.edu/~rebelsky/Courses/CSC151/2019S/readings/drracket

What I’ve been calling the top window, they call the “ Definitions Pane" or "Definitions Window”. And what I’ve been calling the bottom window they call the "Interactions Pane" or "Interactions Window”. These terms make sense. I don’t know if they are standard.

Back to the error message. I’m going to leave the question open for now. If and when I get to an error message that isn’t enough to help me figure out my error, I’ll try again to search.

——

I had previously read that Scheme doesn’t distinguish between capital letter and lower case letter inputs. So I tested a word with an initial capital letter to see if if would return a lower-case letter:

> '> (first 'Grass)
> G


Hmm. That’s not what I expected. I’ll go back and find what I read before.

It was in my notes.

https://people.eecs.berkeley.edu/~bh/ssch1/showing.html

> For the most part, Scheme doesn't care about whether you type in UPPER CASE or lower case. For the examples in this book, we'll assume that you always type in lower case and that the computer prints in upper case. Your Scheme might print in lower case; it doesn't matter.

My theory is that Scheme doesn’t care if we type in upper case or lower case for the names of procedures. But things with apostrophes in front of them are treated as written, upper case or lower case.

——

Then I thought of another question: Is it better to call that mark before something in Scheme an apostrophe or a single quote mark?

I searched “apostrophe versus single quote”.

https://english.stackexchange.com/questions/36046/apostrophe-vs-single-quote#36048

> An easy way to differentiate:
>
> An apostrophe is only used within or at the very end of a word - it is part of the word.
>
> In English, it serves three purposes:
>
> • The marking of the omission of one or more letters (as in the contraction of do not to don't).
> • The marking of possessive case (as in the cat's whiskers).
> • The marking as plural of written items that are not words established in English orthography (as in P's and Q's, the late 1950's). (This is considered incorrect by some; see Use in forming certain plurals. The use of the apostrophe to form plurals of proper words, as in apple's, banana's, etc., is universally considered incorrect.)
> Single quotes are only used around words - they come in pairs, and are not part of any word.
>
> Single or double quotation marks denote either speech or a quotation.

It doesn’t come in pairs in Scheme, but it’s there to indicate that the thing after it should be treated as is, so its meaning is that of a quotation mark and not of an apostrophe. “single quote mark” is cumbersome. I could say “quote mark”.

——

Meta note: I’m trying to write low-error-rate. I think this post is too long for that. Maybe I should have broken it up and written each question as a separate post. But I wanted to show the flow of the questions that I considered.


Alisa Zinov'yevna Rosenbaum

unread,
Feb 13, 2020, 2:46:15 PM2/13/20
to fallibl...@googlegroups.com
On Thu, Feb 13, 2020 at 12:16:57PM -0500, Anne B wrote:

> Then I thought of another question: Is it better to call that mark before something in Scheme an apostrophe or a single quote mark?

The ASCII entry in Eric S. Raymond's "Jargon File" is a good reference for questions like this: http://www.catb.org/jargon/html/A/ASCII.html

It says that "single quote" is common for the punctuation mark you're talking about. Sounds good to me.

Elliot Temple

unread,
Feb 13, 2020, 3:33:54 PM2/13/20
to FIGG
You were mistaken to assume "#<procedure:first>” is an error message. It’s not.

Elliot Temple
www.curi.us

Anne B

unread,
Feb 14, 2020, 10:39:35 AM2/14/20
to fallibl...@googlegroups.com
> You were mistaken to assume "#<procedure:first>” is an error message. It’s not.

Ohh.

Maybe it’s saying that “first” is a procedure and then it’s also just printing “grass".

I’ll test just the "grass" part.

> '> 'grass
> grass

What if there's no single quote mark before the word “grass”?

> '> grass
> . . grass: undefined;
> cannot reference an identifier before its definition


Now just the “first” part.

> '> first
> #<procedure:first>

So “first” has already been defined and "grass" hasn’t.

I still don’t know what "#<procedure:first>” means, though. And I have a bigger question: generally, what does Scheme do with inputs that don’t start with a left parenthesis?

I want to figure out how to search the whole Simply Scheme book. So far I’ve been looking at the Table of Contents to pick chapters to search.

Hmm, there’s an Index. I can try the “parentheses” entries there. Nope, nothing there.

I did lots of searching, on the internet, in Simply Scheme, on the DrRacket website. Finally, I found something relevant by searching Simply Scheme chapters for “<":

https://people.eecs.berkeley.edu/~bh/ssch4/defining.html

> '> +
> #<PRIMITIVE PROCEDURE +>
>
> '> define
> ERROR - INVALID CONTEXT FOR KEYWORD DEFINE

I’ll try what they did above:

> '> +
> #<procedure:...imply Scheme.rkt:748:2>
> '> define
> . define: bad syntax in: define

I also tried:

> '> 3
> 3


So my best guess is that when you input something that doesn’t start with a left parenthesis, Simply Scheme either gives it back to you (as in the case of a number or something with a single quote in front of it) or tells you what kind of thing it is (if it’s a kind of thing that it knows about) or tells you it can’t do either of those things. This may still be incomplete or otherwise wrong.

——

Meta Note: I worked on this email for a while yesterday. I wasn’t finding what I wanted to find and I got frustrated. I wanted to finish the email by saying I give up on the problem. I felt pressure to reply the same day. Then I thought: *Wait! I don’t need to finish this today. I can put it aside and work on it another day. There’s no hurry!* Then this morning I had another idea for searching and it resulted in something helpful.

Anne B

unread,
Feb 14, 2020, 2:52:43 PM2/14/20
to fallibl...@googlegroups.com
On Feb 6, 2020, at 4:13 PM, Anne B <anne...@gmail.com> wrote:

> 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)))))

> 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?

Simply Scheme has an appendix that lists Scheme Primitives. It agrees with my guess:

https://people.eecs.berkeley.edu/~bh/ssch27/appendix-funlist.html

> Is the first argument an element of the second?

Then it links to here:

https://people.eecs.berkeley.edu/~bh/ssch6/true.html#memq

> A function that returns either #t or #f is called a predicate. You've already seen the equal? predicate. It takes two arguments, which can be of any type, and returns #t if the two arguments are the same value, or #f if they're different. It's a convention in Scheme that the names of predicates end with a question mark, but that's just a convention.

So “member?” is a predicate, and a predicate is a kind of function.

Is a function a kind of procedure? I don’t get the relationship between functions and procedures.

I looked around a bit and found this:

https://people.eecs.berkeley.edu/~bh/ssch4/defining.html

> Functions and Procedures
>
> Throughout most of this book, our procedures will describe processes that compute functions. A function is a connection between some values you already know and a new value you want to find out.

> We said earlier that a procedure is "a description of the process by which a computer can work out some result that we want." What do we mean by process? Consider these two definitions:
>
> f(x)=3x+12
> g(x)=3(x+4)
> The two definitions call for different arithmetic operations. For example, to compute f(8) we'd multiply 8 by 3, then add 12 to the result. To compute g(8), we'd add 4 to 8, then multiply the result by 3. But we get the same answer, 36, either way. These two equations describe different processes, but they compute the same function. The function is just the association between the starting value(s) and the resulting value, no matter how that result is computed.

> In real life, functions are not always represented by procedures. We could represent a function by a table showing all its possible values, like this:
>
> Alabama Montgomery
> Alaska Juneau
> Arizona Phoenix
> Arkansas Little Rock
> California Sacramento
> … …
> This table represents the State Capital function; we haven't shown all the lines of the complete table, but we could. There are only a finite number of U.S. states. Numeric functions can also be represented by graphs, as you probably learned in high school algebra. In this book our focus is on the representation of functions by procedures. The only reason for showing you this table example is to clarify what we mean when we say that a function is represented by a procedure, rather than that a function is the procedure.
> We'll say "the procedure f" when we want to discuss the operations we're telling Scheme to carry out. We'll say "the function represented by f" when our attention is focused on the value returned, rather than on the mechanism. (But we'll often abbreviate that lengthy second phrase with "the function f" unless the context is especially confusing.)[2]

What I get from this is that a procedure is some instructions for the computer to follow. And a function is a relationship between starting values and ending values. We can represent a function with a procedure.






Anne B

unread,
Feb 15, 2020, 1:54:35 PM2/15/20
to fallibl...@googlegroups.com
On Feb 6, 2020, at 4:13 PM, Anne B <anne...@gmail.com> wrote:

> 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)))))


> 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?


Yes, a single letter just counts as a word:

https://people.eecs.berkeley.edu/~bh/ssch5/words.html

> But there's no separate data type called "letter"; a letter is the same as a one-letter word.


Some experiments I did:

> '> (word a r t)
> . . a: undefined;
> cannot reference an identifier before its definition
> '> (word 'a 'r 't)
> art
> '> (butfirst 'art)
> rt
> '> (word 'a (butfirst 'art))
> art
> '> (word '10 '34)
> 1034

So the arguments for “word” need to be text that’s treated as text rather than as something to be evaluated. To input something like that, we put a single quote mark before it.

The output from (butfirst ‘art) must be such a thing too. It must be treated by Scheme the same way as “‘rt” (with a single quote mark) is rather than as “rt” (without a single quote mark) is, even though when I put in “(butfirst ‘art)” I got back “rt” without the single quote mark in front of it.


Anne B

unread,
Feb 16, 2020, 4:39:26 PM2/16/20
to fallibl...@googlegroups.com
On Feb 6, 2020, at 4:13 PM, Anne B <anne...@gmail.com> wrote:

> 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)))))


> “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.


> 6) I could experiment some with “if” to see if it does what I wrote above.


I remembered reading this:

https://people.eecs.berkeley.edu/~bh/ssch6/true#g66

> #T isn't the only true value. In fact, every value is considered true except for #f.

So I did the below tests:

> '> (if #f 'true 'false)
> false
> '> (if #t 'true 'false)
> true
> '> (if 'word 'true 'false)
> true
> '> (if word 'true 'false)
> true
> '> (if 12 'true 'false)
> true
> '> (if 'define 'true 'false)
> true
> '> (if define 'true 'false)
> . define: not allowed in an expression context in: define
> '> (if 'first 'true 'false)
> true
> '> (if first 'true 'false)
> true

So “define” is not true. Then it must not be a value. In the book they call it a special form. Other special forms are “if”, “let” and “lambda”.

This page

https://people.eecs.berkeley.edu/~bh/ssch27/appendix-funlist.html

lists the non-standard Scheme primitives that are used in Simply Scheme. It labels some as special forms. I tested “if”, “let” and “lambda” as above, and they don’t evaluate to true either. I tested all the other special forms and some of the procedures on that page too. The procedures were values and the special forms weren’t.


Elliot Temple

unread,
Feb 16, 2020, 4:44:41 PM2/16/20
to fallibl...@googlegroups.com
There’s a common trick for converting values to true or false.

You do !!value which in Scheme you’d write more like: (not (not value))

not inverts true and false, and also returns true or false only, not something else. so it converts for you. and you do it twice to get it non-inverted.

you could similarly use e.g. (and value value) to get true or false. you don’t need to use if.

Elliot Temple
www.elliottemple.com

Anne B

unread,
Feb 18, 2020, 10:28:01 AM2/18/20
to fallibl...@googlegroups.com
Ah yes. I tested this and it seemed to work.

> you could similarly use e.g. (and value value) to get true or false. you don’t need to use if.

This, however, did not work as I expected it to. From my log:

> '> (and 1 1)
> 1
> '> (and word word)
> #<procedure:word>
> '> (and 'word 'word)
> word
> '> (and #t #t)
> #t
> '> (and #t #f)
> #f


I looked up “and” in Simply Scheme and I found this:

https://people.eecs.berkeley.edu/~bh/ssch6/true#g96

> This allows us to have semipredicates that give slightly more information than just true or false.

> And and or are also semipredicates.

> And returns a true value only if all of its arguments are true. In that case, it returns the value of the last argument:

That explains it. Some more testing:

> '> (and #f word)
> #f
> '> (and word #f)
> #f
> '> (and #t word)
> #<procedure:word>
> '> (and word #t)
> #t

So you could use (and value #t) to convert a value to true or false but not (and value value).


Justin Mallone

unread,
Oct 18, 2020, 7:42:22 PM10/18/20
to fallibl...@googlegroups.com
I came across this guide:

https://en.wikibooks.org/wiki/Scheme_Programming/Procedures

It evaluates (lambda (x) (* x x)) and gets back #<procedure>, and then
says:

> Evaluating a lambda form gives us back a procedure object, which we
> can treat like any other value.

The idea of a "procedure object" seemed important. I wanted more
information:

https://www.cs.utexas.edu/ftp/garbage/cs345/schintro-v14/schintro_61.html#:~:text=In%20Scheme%2C%20procedures%20are%20data,with%20any%20other%20Scheme%20value.&text=A%20procedure%20pointer%20is%20just,a%20pair%20or%20a%20boolean.

> In Scheme, procedures are data objects--you can have a pointer to a
> procedure and do the same things you can do with any other Scheme
> value. Technically, we say that procedures are *first class* objects
> in the language--you can pass a procedure value as an argument to a
> procedure, return it as the value of a procedure call, store it in a
> variable or a field of another object. A procedure pointer is just a
> value that you can pass around like any other value, like a pair or a
> boolean.

So I think #<procedure> (and variants in which the procedure actually
has a name, like the example with "first" that Anne discusses above)
indicate the type of thing that is being returned, which is a procedure.
More broadly, the point about them being objects that are the same as
other values in certain ways seems really important. I think that maybe
the language would have to work very differently if that were not the
case.

The page I linked immediately above says:

> An unusual feature of Scheme is that it uses a *unified namespace*,
> which means that there's only one kind of name for both normal
> variables and procedures--in fact, procedure names are really just
> variable names, and there's only one kind of variable. A named
> procedure is really just a first-class procedure object that happens
> to be referenced from a variable.

-JM
Reply all
Reply to author
Forward
0 new messages