HtDP / Chpt. 5: How do I use "symbol=?"? (Scheme) - scheme

I'm currently working through HtDP on my own.
In Chapter 5 "Symbolic Information" is covered. The example in the text is:
(define (reply s)
(cond
[(symbol=? s 'GoodMorning) 'Hi]
[(symbol=? s 'HowAreYou?) 'Fine]
[(symbol=? s 'GoodAfternoon) 'INeedANap]
[(symbol=? s 'GoodEvening) 'BoyAmITired]))
That's all clear. However, the second exercise asks:
Exercise 5.1.2. Develop the function check-guess. It consumes two numbers, guess and target. Depending on how guess relates to target, the function produces one of the following three answers: 'TooSmall, 'Perfect, or 'TooLarge.
Frankly, I don't really see when or why "symbol=?" comes in here. My solution only uses "cond". [EDIT: Code removed due to a suggestion since it is a solution to a textbook exercise.]
According to the text, "symbol=?" consumes two symbols and returns either true or false, depending on whether they are identical or not.
I fear that I am now deep into the realm of the Dunning-Kruger effect, but I really don't see a way to implement this piece of code with the use of "symbol=?". "cond" is covered in Chapter 4, which is why I am now confused.
Any help is highly appreciated.
If you want to have a look at the chapter in HtDP, then please go here:
http://www.htdp.org/2003-09-26/Book/curriculum-Z-H-8.html#node_sec_5.1
Unfortunately, the solutions are only accessible with a password.

use < and > to compare the numbers. You can't to use symbol=? for this.

Related

Implementation dependent code in Scheme

In Common Lisp, when I want to use different pieces of code depending on Common Lisp implementations, I can use *features* and the provided notation of #+ and #- to check the availability of a given feature and proceed accordingly. So for example (taken from Peter Seibel's PCL):
(defun foo ()
#+allegro (do-one-thing)
#+sbcl (do-another-thing)
#+clisp (something-else)
#+cmu (yet-another-version)
#-(or allegro sbcl clisp cmu) (error "Not implemented"))
Is anyone aware of a similar mechanism for Scheme? There are sometimes subtle differences between different implementations of Scheme, which, when you're trying to be portable, would be nice to abstract away. One such case that comes to my mind is Racket not providing mutable pairs by default. Instead of writing e.g. (set-cdr! lst '(1 2 3)) you would have to use set-mcdr! and only after you ran (require racket/mpair). Of course, such things could be abstracted by functions and/or macros, but I think the Common Lisp approach is neat in this aspect.
The closest thing there is, is cond-expand (aka SRFI 0), which is available on some Schemes but not others (Racket, for example, doesn't have it, and your code won't compile if you try to use it). For those Schemes that do have it, it looks like a cond form, except you test for booleans that tell you things about the compiler/interpreter. On some Schemes you can detect which Scheme you're running on, while on others you can only check for SRFIs:
(cond-expand (chicken
'bok-bok-bok!)
((and guile srfi-3432)
'this-guile-is-full-of-SRFI!)
(else
'(might be MIT Scheme, whose cond-expand only tests for SRFIs)))

ERROR: sllgen:make-rep-loop: unbound identifier in module in: sllgen:make-rep-loop

(define read-eval-print (sllgen:make-rep-loop "--> "
(lambda (pgm) (eval-program pgm))
(sllgen:make-stream-parser
the-lexical-spec
the-grammar)))
Is anyone familiar with this type of error?
Help please.
You need to use the right language. According to the documentation, it looks like you need to start your source file with #lang eopl:
#lang eopl
(define read-eval-print
(sllgen:make-rep-loop
"--> "
(lambda (pgm)
(eval-program pgm))
(sllgen:make-stream-parser
the-lexical-spec
the-grammar)))
Once you do this, of course, you'll start getting some other errors, e.g., that eval-program isn't defined. If you define a dummy one, e.g.,
(define (eval-program pgm)
(values))
You'll then get an error that
sllgen:make-stream-parser: bad scanner specification in: the-lexical-spec
You'll need to replace that lexical specification with an actual specification, and similarly (I expect, though I didn't explore this far) with the-grammar. The documentation linked above might (or might not) help you out with those. It looks like you can get the code for the book that this is based on, Essentials of Programming Languages, from the book's website.
What really looks the most promising, though, are results like
Environment-Passing Interpreters, Programming Language Essentials, 2nd edition, Appendix: The SLLGEN Parsing System (PDF)
SLLGEN tutorial (PDF)
sllgen source (sllgen.scm) (a somewhat older version)
other sources (I found the above just by searching with Google for sslgen)

Does Chicken Scheme support complex numbers? If so, why am I getting this error?

I just started learning a little Scheme, and I'm using Dorai Sitaram's Teach Yourself Scheme in Fixnum Days. In said work it is stated:
Scheme numbers can be integers (eg, 42) ... or complex (2+3i).
Emphasis mine. Note the form.
Using the principles I had been taught so far I tried writing a few different programs that dealt with the different kinds of numbers. I ended up writing this extremely simple snippet to test complex numbers:
(begin
(display 3+4i)
(newline)
)
Testing this on codepad.org (which uses MzScheme) and Ideone.com (which uses guile) worked perfectly.
Now, when I tried it with Chicken Scheme (my local development environment), it compiles fine, but when run, crashes and gives me the error:
Error: unbound variable: 3+4i
Call history:
main.scm:2: 3+4i <--
Apperently there's an unbound variable error, but with my limited Scheme I don't even know what that means (yet.)
Has anyone else experienced this? I know Chicken Scheme is supposed to be pretty standards compliant, and so it seems wierd that it wouldn't support something simple like this. I Googled through their documentation, but I couldn't find anything specific (although I think there is an external complex number library available, so perhaps that's a hint.)
If anyone has any suggestions, they'd be greatly appreciated. Thanks in advance! :)
I believe you need to install the numbers extension for dealing with complex numbers in Chicken Scheme. Do this:
> chicken-install numbers
And don't forget to load it:
(use numbers)

What do :+ and :or do on Scheme?

I'm trying to do my homework and hacking through some example code I saw this line:
[(:+ (:or VAR)) (token-VAR (string->symbol lexeme))]
This is from a lexical analyzer in a calculator;
Now I'm not really sure what either of this does, and I'm not particularly sure what this means exactly, but I'm pretty sure it has what I need to finish my homework. Searching hasn't gotten me any help so all help is great at this time. Thanks!
The sample code probably imported parser-tools using the : prefix (which is the recommended prefix in the parser-tools documentation. If that's the case, then :+ means "repetition one or more times" and :or matches any of the subpatterns (just VAR in this case).

Where to find Scheme library functions?

Is there a reference website to look up syntax for Scheme library function like http://www.cplusplus.com/reference/?
I'm looking for syntax of fold, but google gave me nothing :(
Thanks,
fold is from SRFI 1. Many functions have good documentation if you know where it "comes from".
Also, since you're using Racket (as mentioned in your previous questions), you should check out the Racket documentation. It has a very nice search facility. (Also, you might like to know about Racket's foldl, which is identical to SRFI 1's fold.)

Resources