Home     |     .Net Programming    |     cSharp Home    |     Sql Server Home    |     Javascript / Client Side Development     |     Ajax Programming

Ruby on Rails Development     |     Perl Programming     |     C Programming Language     |     C++ Programming     |     IT Jobs

Python Programming Language     |     Laptop Suggestions?    |     TCL Scripting     |     Fortran Programming     |     Scheme Programming Language


 
 
Cervo Technologies
The Right Source to Outsource

MS Dynamics CRM 3.0

Scheme Programming Language

Grrrr ... quotes and how Scheme identifies characters in strings.


Hi all,

I just can't seem to figure out what is returned from this code and it is
driving me nutz.

(define sentence '(this is a sentence))
sentence ; ==> (this is a sentence) which is a list containing 4 items
(first sentence) ; ==> this

OK ... here is my problem.  I know that the contents of the first item in
the constant 'sentence' is the word 'first' (my quotes).  But I don't know
how to tell the type of the first item.  It isn't a string and it isn't a
list.

This won't work:
(string (first sentence)) ; ==> returns an error that it was expecting an
argument of type <character>, given this.

Also, while playing with strings in Scheme, I came across the
char-alphabetic?, char-numeric?, symbol? procedures.

I believe (wrongly??) that strings can contain any ASCII character.  And
more routinely can contain an apostrophe character but could contain other
symbols like +-/\#.* etc.  Is there any easy way to test for special
characters in a string that can be used as a procedure like a + or -?
(Other than by having a slew of cond or if statements.)

And to those who recommended I pick up a copy of the Little Schemer - it
arrives in two days ::)))

My objective is to parse a string (containing a given character) to produce
a list of string items.  From there I wanted to see
if I could turn that into a procedure.

For example:
"(define (cube-number val) (* val val val)))"

I thought I would need to parse the string and create a list of each key
item that might look like this:
(list "(" "define" "(" "cube-number" "val" ")" "(" ... etc)

So far this is an intellectual exercise to (a) play with strings and quoted
lists and (b) see if I can parse it (should be relatively easy but '(a b)
stuff has me stuck - see above), and (c) once parsed, see if I can then
execute what I get.

The (c) part may be well beyond me but (a) and (b) should be within reach, I
hope.

The on-line help and DrScheme help aren't especially 'helpful' in these
areas.  I was hoping to see some example code.

All suggestions gratefully received.

Kindest regards,

Mike
PS - to all USA students / colleagues, my thoughts are with you, we are
family (I'm an Aussie).  Wish to God I could help - just don't know how.

On Tue, 24 Apr 2007, Mike wrote:
> Hi all,

> I just can't seem to figure out what is returned from this code and it is
> driving me nutz.

> (define sentence '(this is a sentence))
> sentence ; ==> (this is a sentence) which is a list containing 4 items
> (first sentence) ; ==> this

> OK ... here is my problem.  I know that the contents of the first item in
> the constant 'sentence' is the word 'first' (my quotes).  But I don't know
> how to tell the type of the first item.  It isn't a string and it isn't a
> list.

Its a symbol:

"this is a string"
'this-is-a-symbol
'("this" "is" "a" "list" "of" 7 "strings" "and" 2 "numbers")
'(this is a list of 8 symbols and 2 numbers)
'(this is a list of 8 symbols and 2 numbers "and one string")

Mike wrote:
> Hi all,

> I just can't seem to figure out what is returned from this code and it is
> driving me nutz.

> (define sentence '(this is a sentence))
> sentence ; ==> (this is a sentence) which is a list containing 4 items
> (first sentence) ; ==> this

> OK ... here is my problem.  I know that the contents of the first item in
> the constant 'sentence' is the word 'first' (my quotes).

Did you mean "the word 'this'"?

>  But I don't know
> how to tell the type of the first item.  It isn't a string and it isn't a
> list.

It is a symbol.  (symbol->string 'this) --> "this"

-- JK

Thanks heaps ...

(first '( "x")) resolves to a string "x" while (first '(name-of? world))
resolves to a symbol which is the word name-of?.

Can you do anything with this symbol during program execution?  And please
excuse me if this is obvious - I'm trying to understand what purpose there
might be for Scheme to have symbols like this.  I can understand a + or -
etc.  But then in Scheme they are procedures aren't they?  If they were
symbols like this (first '(+ - * /)) then you end up with a symbol of the
plus sign + and this is not the procedure +

If I had a procedure called name-of? then it is an object that resolves to
something.  What does a symbol resolve to?

Thanks again,

Mike
"Kjetil S. Matheussen" <k.s.matheus@notam02.no> wrote in message
news:Pine.LNX.4.64.0704241955160.9366@ttleush...

On Tue, 24 Apr 2007 14:47:16 -0500, Mike <mcu87@bigpond.net.au> wrote:
> Can you do anything with this symbol during program execution?  And  
> please
> excuse me if this is obvious - I'm trying to understand what purpose  
> there
> might be for Scheme to have symbols like this.  I can understand a + or -
> etc.  But then in Scheme they are procedures aren't they?  If they were
> symbols like this (first '(+ - * /)) then you end up with a symbol of the
> plus sign + and this is not the procedure +

SICP has some good examples of using Symbols in its text, and a section  
that explains them  
(http://mitpress.mit.edu/sicp/full-text/book/book-Z-H-16.html#%_idx_1986).  
I often use symbols in the same way others use Constants in other  
languages. They serve as useful labels, identifiers, and they are, in  
fact, used similar to identifiers (though, my terminology could be off).  
For example:

: (eval '(+ 1 1)) => 2

Or, another example is the CASE form. Here symbols are used in place of  
constants which are generally used in other languages:

#define FUNNY 0
#define NOTFUNNY 1
#define NEUTRAL 2

switch (result) {
   case FUNNY: funny(); break;
   case NOTFUNNY: notFunny(); break;
   case NEUTRAL: neutral(); break;

}

Vs.

(case result
   ((funny) (funny))
   ((notfunny) (not-funny))
   ((neutral) (neutral)))

I hope this helps. Symbols also tend to have the potential for much higher  
performance than Strings, which makes them useful for a lot of things.

--
Aaron Hsu <aaron.@sacrificumdeo.net>
"No one could make a greater mistake than he who did nothing because he  
could do only a little." - Edmund Burke

On Tue, 24 Apr 2007 19:47:16 GMT, "Mike" <mcu87@bigpond.net.au>
wrote:

>If I had a procedure called name-of? then it is an object that resolves to
>something.  What does a symbol resolve to?

To itself. A symbol is simply an opaque token, uniquely identified by
its name. On the surface, such a thing might seem useless, but there are
plenty of applications. I don't know what you're programming background
is, so I apologize if the following makes no sense.

Consider an enumerated type in Pascal:

 type
   TMyType = (Foo, Bar, Baz);

The semantic interpretation of the tokens Foo, Bar and Baz in Pascal is
similar to that of a symbol in Scheme. (There are differences--Pascal
enumeration values are inherently ordered, for example.) You can't do
anything with a Pascal enumerated type that you couldn't do with, say,
integers, but enumerated types have certain advantages. For one thing,
they can make the code more readable. For another, they make it harder
to supply nonsense values (i.e, they offer a form of type-checking).

Steve Schafer
Fenestra Technologies Corp.
http://www.fenestra.com/

Steve Schafer wrote:
> On Tue, 24 Apr 2007 19:47:16 GMT, "Mike" <mcu87@bigpond.net.au>
> wrote:

>>If I had a procedure called name-of? then it is an object that resolves to
>>something.  What does a symbol resolve to?

> To itself.

Evaluating a bare symbol yields the symbol's value,
not the symbol itself.  Or an error, if the symbol
happens to be unbound.

-- JK

I have a suggestion on how to get started if you would like to focus on
executing your input and leave the parsing to the read procedure.

(let ((user-input (read)))
  user-input)

- - - - - - - - - -
(symbol? (read))
;=> x
#t
- - - - - - - - - -
(list? (read))
;=> (define (f x) (* x x))
#t
- - - - - - - - - -
(define read-eval-print-loop
  (lambda ()
    (let ((user-input (read)))
      (eval-input user-input))))

(read-eval-print-loop)

"Mike" wrote

> Hi all,

> I just can't seem to figure out what is returned from this code and it is
> driving me nutz.

> (define sentence '(this is a sentence))
> sentence ; ==> (this is a sentence) which is a list containing 4 items
> (first sentence) ; ==> this

> OK ... here is my problem.  I know that the contents of the first item in
> the constant 'sentence' is the word 'first' (my quotes).  But I don't know
> how to tell the type of the first item.  It isn't a string and it isn't a
> list.

"[...] Scheme does have strings, but symbols have the salutary property that
they can be compared for equality in constant time."
PLAI footnote on page 16.
Add to del.icio.us | Digg this | Stumble it | Powered by Megasolutions Inc