[racket] TLS "atom?" definition for Scheme does not work in DrRacket

125 views
Skip to first unread message

Rufus

unread,
Mar 3, 2015, 3:18:02 PM3/3/15
to racket users list
On page 10 of TLS there is a note defining the "atom?" function for the
reader to use w/Lisp or Scheme. The definition for Scheme (which is the
most likely one to use for DrR) does not work. To fix one must remove
the parens that open at the "and". Apparently the DrR
compiler/interpreter sees them as an attempt to invoke a function and
that's not correct here. I don't know if other Lisp versions would find
the code in the book correct.

In the text below XXX shows where I removed a set of parens to make the
function work.

(define atom? (lambda (arg1)
XXX and (not (pair? arg1))
(not (null? arg1))
XXX
)
)


Rufus
____________________
Racket Users list:
http://lists.racket-lang.org/users

Alexander D. Knauth

unread,
Mar 3, 2015, 3:22:39 PM3/3/15
to Rufus, racket users list
This works for me:
(define atom? (lambda (arg1)
(and (not (pair? arg1))
(not (null? arg1)))))

Rufus

unread,
Mar 3, 2015, 3:51:26 PM3/3/15
to racket users list
Alexander

Well, in fact, I thought it worked for me, too. And then it stopped
working; DrR threw an "application: not a procedure;" error saying it
was given a null list instead of an application. Removing the parens got
it working again.

Now that just doesn't make any sense at all but that's what I seem to
have observed. Looking at some StackOverflow responses on similar errors
I found that parens in certain positions are interpreted as invoking a
function regardless of what may in fact be inside the parens. IOW, extra
parens will trigger that error. I believe that initially it was working.
Perhaps I had run it in the executable pane instead of loading and
running the defs file? If I have more time somewhere along, I'll try to
recreate things.

When I first defined atom? I was using a very small definitions file
with the " #lang racket" spec; IIRC atom? worked. After trying BSL and
then going back to using the #lang spec and adding some comments and the
list of variable defs - it didn't work.

At the moment I don't have any good ideas, except to chg the code to get
it to work and proceed.

Rufus

Alexander D. Knauth

unread,
Mar 3, 2015, 4:04:04 PM3/3/15
to Rufus, racket users list
Well that’s weird. And after it stopped working with the parens, then it worked without them?
It doesn’t do that for me:
(define atom? (lambda (arg1)
and (not (pair? arg1))
(not (null? arg1))))
;. and: bad syntax in: and

Is there a specific way to reproduce it?

Matthias Felleisen

unread,
Mar 3, 2015, 4:22:25 PM3/3/15
to Rufus, racket users list

Use BSL with List Abbreviation.


On Mar 3, 2015, at 3:50 PM, Rufus <rlag...@mail.com> wrote:

Rufus

unread,
Mar 3, 2015, 6:36:32 PM3/3/15
to racket users list
Matthias

My goodness. There seems to be some strangeness here.

1) When using "#lang racket" and running the defs file,
- Atom? is defined regardless of whether the "AND clause
get parens ie. "Atom?" entered on the interactive pane
(then hit Enter) tells us it's a proc regardless the
way it's defined.
- (Atom? x) (where x is defined) throws an error if its
definition used parens around the AND clause but works
as expected w/out them.

2) When using BSL-lists:
- Atom? can be defined on the interactive pane using parens
around the AND clause the way it appears in TLS; it
works as expected.
- Atom? cannot be defined on the i-pane w/out the parens
around the AND clause because it throws a syntax error.

3) As an aside: Under BSL-lists, I cannot get any code in the
defs file to run. At least none of the definitions exist
after I load the file and click "Run". So now it comes back
to me exactly what led me to use the "#lang racket" spec:
The code in the defs file was not getting executed when I
used the BSL-lists, loaded the defs file and clicked Run.

GOT IT! The toggle for the atom? weirdness is the (define and '...)
included in the variable defs I made up from the TLS examples. Ie. a
reserved word problem. Doesn't know the exact mechanism but nottt a
problem. <g>

Still unclear on why BSL-lists apparently won't run a defs file. Another
day...

Rufus



On 03/03/2015 03:20 PM, Matthias Felleisen wrote:
>
> Use BSL with List Abbreviation.
>
>

Alexander D. Knauth

unread,
Mar 3, 2015, 7:46:48 PM3/3/15
to Rufus, racket users list

On Mar 3, 2015, at 6:34 PM, Rufus <rlag...@mail.com> wrote:

> Matthias
>
> My goodness. There seems to be some strangeness here.
>
> 1) When using "#lang racket" and running the defs file,
> - Atom? is defined regardless of whether the "AND clause
> get parens ie. "Atom?" entered on the interactive pane
> (then hit Enter) tells us it's a proc regardless the
> way it's defined.
> - (Atom? x) (where x is defined) throws an error if its
> definition used parens around the AND clause but works
> as expected w/out them.

When you checked that it “works as expected,” did you check that it returns false for non-empty lists?

> 2) When using BSL-lists:
> - Atom? can be defined on the interactive pane using parens
> around the AND clause the way it appears in TLS; it
> works as expected.
> - Atom? cannot be defined on the i-pane w/out the parens
> around the AND clause because it throws a syntax error.
>
> 3) As an aside: Under BSL-lists, I cannot get any code in the
> defs file to run. At least none of the definitions exist
> after I load the file and click "Run". So now it comes back
> to me exactly what led me to use the "#lang racket" spec:
> The code in the defs file was not getting executed when I
> used the BSL-lists, loaded the defs file and clicked Run.
>
> GOT IT! The toggle for the atom? weirdness is the (define and '...)
> included in the variable defs I made up from the TLS examples. Ie. a
> reserved word problem.

One of the things I really like about DrRacket is that it can draw arrows from an identifier to its definition.
In the file with the (define and ‘…), when you hover over the and in the atom? definition,
does it show an arrow pointing to the (define and ‘…) ?

> Doesn't know the exact mechanism but nottt a
> problem. <g>
>
> Still unclear on why BSL-lists apparently won't run a defs file. Another
> day…

BSL+list-abbrevs doesn’t let you re-define and; Is that the reason?

Rufus

unread,
Mar 3, 2015, 10:30:45 PM3/3/15
to racket users list
Alexander

> arrows

That's how I found the problem, although I had to chase it down off the
bottom of the pane. Somebody did a neat good thing there.

> BSL+list

It apparently doesn't run the defs _at all_. The interactive pane will
not recognize _any_ of the variables or functions that appear to be
defined in the definition pane and should form the environment (or scope
or whatever the correct word) after I click on the "Run" button. And I
don't see any error msg's anywhere. I'm probably missing something
simple and dumb here like the reserved word problem; but so far no light
bulbs...

And btw thanks all for your help and suggestions.


Rufus

Alexis King

unread,
Mar 3, 2015, 10:34:30 PM3/3/15
to Rufus, racket users list
> That's how I found the problem, although I had to chase it down off the
> bottom of the pane.

If the arrow goes off-screen, you can right-click the identifier and select “Jump to Binding Occurrence” to automatically scroll to where the identifier is bound. You can also select “Tack/Untack Arrows” to make the arrows persist even after you move the cursor off the identifier.

Definitely a helpful feature overall.

Daniel Feltey

unread,
Mar 3, 2015, 10:39:15 PM3/3/15
to Rufus, users
> It apparently doesn't run the defs _at all_. The interactive pane will
> not recognize _any_ of the variables or functions that appear to be
> defined in the definition pane and should form the environment (or scope
> or whatever the correct word) after I click on the "Run" button. And I
> don't see any error msg's anywhere. I'm probably missing something
> simple and dumb here like the reserved word problem; but so far no light
> bulbs...

This is certainly not the expected behavior of any of the teaching languages. You say that none of the definitions seem to be recognized in the interactions window after you click the run button? Does this mean that when you try to reference a defined value you get an error? Or what exactly is happening when you try to use one of your definitions in the interactions window?

Dan

Rufus

unread,
Mar 3, 2015, 10:41:10 PM3/3/15
to racket users list
Aha! Eureka. <g>

Thanks

I'm just starting this odyssey. The main goal is to wrap my head around
OOP and recursive concepts and hopefully I can avoid any more language
issues for a while and get through TLS. Although it doesn't hurt _that_
much to get reacquainted w/how sneaky syntax and programming interfaces
can be and how dumb blind I can get. <g>


Rufus

Rufus

unread,
Mar 3, 2015, 10:49:14 PM3/3/15
to users
Daniel


Thanks for your interest.

Any reference to any definition (supposed) from the I-pane throws an
"undefined" error; there are no apparent errors when click Run
immediately after I load the definition file. I'm not going to worry
about it too much at this point. It's probably a simple noobie problem
and my main thrust is to get through TLS as effectively (maybe not as
fast) as possible and I think the "racket" spec will let me do that.

If I get sand-bagged by TLS I'll probably try setting up a new
definitions file with only one or two lines and seeing what I can promote.


Rufus

Alexander D. Knauth

unread,
Mar 3, 2015, 10:57:18 PM3/3/15
to Rufus, racket users list

When I run this program:
(define atom? (lambda (arg1)
(and (not (pair? arg1))
(not (null? arg1)))))
(define and 'dummy)
It highlights the `and` in the `(define and ‘dummy)`, and gives me this error message:
define: expected a variable name, or a function name and its variables (in parentheses), but found a keyword

Do you not get an error message like that? If you don’t it looks like a bug.
Or could there possibly be some setting that turns off error messages or something?

Daniel Feltey

unread,
Mar 3, 2015, 11:46:36 PM3/3/15
to Rufus, users
Just one more question, for clarification. Are you using the `lambda` form in BSL? I ask because several examples in this thread have been using `lambda`, but I don't think that lambda expressions are actually valid in either of the beginning student languages, though I would expect you to receive an error about lambda being undefined in that situation.

Daniel Feltey

unread,
Mar 4, 2015, 12:10:46 AM3/4/15
to Rufus, users
> Just one more question, for clarification. Are you using the `lambda` form in BSL? I ask because several examples in this thread have been using `lambda`, but I
> don't think that lambda expressions are actually valid in either of the beginning student languages, though I would expect you to receive an error about lambda
> being undefined in that situation.

Turns out I'm wrong, I'd never tried to use lambda in BSL before, but it should work.

Matthias Felleisen

unread,
Mar 4, 2015, 8:34:09 AM3/4/15
to Rufus, users

All of this should work out of the box, w/o any problem.

1. Please report on the following experiment. In the Definitions pane, type

(define rufus 0)

2. Click Run

3. In the Interactions pane, type

> rufus

4. Please report the response of DrRacket.

Rufus

unread,
Mar 4, 2015, 1:21:39 PM3/4/15
to users
To All

Using BSL-Lists:

Matt't basic test works.

(define rufus 0)

Run, then type and execute "rufus" in the I-pane which displays 0



If I then add Alexander's example define so the def file appears as follows:
---------
(define rufus 0)

(define atom? (lambda (arg1)
(and (not (pair? arg1))
(not (null? arg1)))))
-------------

and hit Run I get an error in the I-pane:

"pair?; this function is not defined".


OK, so the BSL-Lists doesn't have that function. Then when I again type
"rufus"/Enter in the I-pane I get an error "rufus; this variable is not
defined"

So it appears the Run procedure deletes the existing environment before
running the defs file and if it finds any error in the defs file it does
not run _anything_ from that file. So "Run" will leave an empty
environment (right word?) if there is any error in the defs file.

On that note. It seems like the problems I've had relate directly to my
leaving noobie errors in the defs file. My confusion about how the defs
file code is Run - thinking that after a Run that throws and error
either the old environment (from the last successful Run) would remain
intact AND/OR that those lines which evaluate correctly in the new file
would be run despite some errors on other lines in that particular file
- slowed my understanding.

I believe that I do have a defs file w/errors which, on Run, does not
cause an error to display in the I-panealthough it does delete the
environment. I will try to recreate it this afternoon or evening.

As a last thought it still appears that the "#lang racket" yields better
effect than the BSL-Lists when trying follow through the TLS because
some functions shown in the TLS notes aimed at making examples and
exercises work in Scheme (eg. pair?) are not defined in the BSL-Lists.
Matt, I'm pretty sure you have reasons for recommending the BSL-Lists so
maybe I'm missing something else?

Thank you all.

Rufus

Matthias Felleisen

unread,
Mar 4, 2015, 4:13:52 PM3/4/15
to Rufus, users

On Mar 4, 2015, at 1:18 PM, Rufus <rlag...@mail.com> wrote:

> To All
>
> Using BSL-Lists:
>
> Matt't basic test works.
>
> (define rufus 0)
>
> Run, then type and execute "rufus" in the I-pane which displays 0
>
>
>
> If I then add Alexander's example define so the def file appears as follows:
> ---------
> (define rufus 0)
>
> (define atom? (lambda (arg1)
> (and (not (pair? arg1))
> (not (null? arg1)))))
> -------------
>
> and hit Run I get an error in the I-pane:
>
> "pair?; this function is not defined".


It is called cons? not pair? because BSL and friends restrict it to list construction, like TLL.


>
> OK, so the BSL-Lists doesn't have that function. Then when I again type
> "rufus"/Enter in the I-pane I get an error "rufus; this variable is not
> defined"



When you have an error in a program, you get no interactions. You'd never know what these interactions really mean in the presence of an error in your definitions.

0 == 1



> So it appears the Run procedure deletes the existing environment before
> running the defs file and if it finds any error in the defs file it does
> not run _anything_ from that file. So "Run" will leave an empty
> environment (right word?) if there is any error in the defs file.

Yes.

Alexander D. Knauth

unread,
Mar 4, 2015, 5:19:31 PM3/4/15
to Rufus, racket users list

On Mar 4, 2015, at 1:18 PM, Rufus <rlag...@mail.com> wrote:

> If I then add Alexander's example define so the def file appears as follows:
> ---------
> (define rufus 0)
>
> (define atom? (lambda (arg1)
> (and (not (pair? arg1))
> (not (null? arg1)))))
> -------------
>
> and hit Run I get an error in the I-pane:
>
> "pair?; this function is not defined”.

When you include this:
(define and ‘dummy)
Do you get:
define: expected a variable name, or a function name and its variables (in parentheses), but found a keyword
instead of the `pair?` error?

Rufus

unread,
Mar 5, 2015, 12:23:11 AM3/5/15
to racket users list
Alex

Yes, that is what happens. (with BSL) But if I remove the erroneous
(define and...) then it again flags pair? as "function not defined". I
think maybe it only prints one error msg, the last one found.

Sorry for the delay. I am rebuilding the laundry room here at my
sister's house during the day and now I'm trying to read up on the
evolution of program languages - I find that getting a handle on how/why
people did things helps me a lot to actually understand the concepts and
methods we have arrived at today. And I need all the help I can get. <g>
It's 30 yrs since I touched a line of code and I was never into the
theory stuff anyway.

Regards

Rufus

Alexander D. Knauth

unread,
Mar 5, 2015, 7:33:12 AM3/5/15
to Rufus, racket users list

On Mar 5, 2015, at 12:21 AM, Rufus <rlag...@mail.com> wrote:

> Alex
>
> Yes, that is what happens. (with BSL)

Ok, does it show that (or a similar message about not being able to re-define add1)
for your original program with BSL?

> But if I remove the erroneous
> (define and...) then it again flags pair? as "function not defined". I
> think maybe it only prints one error msg, the last one found.

For this:
(pair? #f)
(define and 'dummy)
It reports the and error, but for this:
(define and 'dummy)
(pair? #f)
It also reports the and error.
The and error is found and reported first because it checks that definitions
are valid before it checks that functions like pair? exist.
If that weren’t the case mutual recursion couldn't work the way it does
without needing foreword declarations. (I think)

Rufus

unread,
Mar 5, 2015, 5:18:45 PM3/5/15
to racket users list
Alex

> [racket processes variable definitions first, then functions]

Got it.


> Ok, does it show that (or a similar message about not being able to re-define add1)
> for your original program with BSL?
> ...

If I run the add1 definition, regardless I place it above or below the
definition of atom?, Racket flags it w/a "...defined previously" error;
when it does this it ignores the pair? problem in the atom? defintion.
If I remove the unneeded add1 def, it then gives a "pair?... not
defined" error for the atom? definition.

So Racket does variable defs first, then functions and checks for
"previously defined" before trying to create any new functions. I guess
this means that we cannot override existing function definitions in
Racket? OK by me. <g>

I think in Javascript and maybe others one could just redefine right
over the top of "inherited" stuff. But then that's probably
apples/oranges and I began Racket b4 really learning much JS. JS seems
to do pretty much whatever it wants, although D. Crockford has provided
a lot of "good-practice" forms that look to make it civilized. JS is
what actually got me looking at code again: It appears I need it to mess
w/some really annoying bank websites that require 6, up to 20 or so,
clicks to download images of your paper transactions. Each transaction,
that is. But I need to learn modern concepts that weren't common
practice when I left IT in the mid '80s. Crockford recommended TLS most
highly and after going cross-eyed pencilling intermediate answers I
decided I might as well just code the stuff. The rest is history...

I have to say, Racket looks _lovely_ compared to most of the development
options I've found in the last few weeks. And while I don't think I'd
actually try to _kiss_ the authors, I gotta say the documentation is
fantastic. Better than anything I've seen anywhere.
'
Rufus

Alexander D. Knauth

unread,
Mar 5, 2015, 5:35:35 PM3/5/15
to Rufus, racket users list

On Mar 5, 2015, at 5:16 PM, Rufus <rlag...@mail.com> wrote:

> So Racket does variable defs first, then functions and checks for
> "previously defined" before trying to create any new functions. I guess
> this means that we cannot override existing function definitions in
> Racket? OK by me. <g>

Well, you sometimes can in racket, but not in BSL.

Matthias Felleisen

unread,
Mar 12, 2015, 4:23:07 PM3/12/15
to Rufus, racket users list

On Mar 5, 2015, at 5:16 PM, Rufus <rlag...@mail.com> wrote:

> Alex
>
>> [racket processes variable definitions first, then functions]
>
> Got it.


That's not true. When you evaluate a definitions area,
you evaluate a module body and that process proceeds
top to bottom for Racket definitions and expressions.

In *SL, check-* tests are lifted to the very end of
the module so that students can specify test properly.

Keep in mind that 'value' means number, string, lambda,
etc. And also keep in mind that

(define (f x) ... x ...)

is short for

(define f (lambda (x) ... x ...))

The evaluator stops when it encounters an error. The
interactions are will not know the defined (or imported)
names then.

Can Racket programs redefine variables then? Yes. Some.
That's a careful balancing act.
Reply all
Reply to author
Forward
0 new messages