jcam77
jcam77

Reputation: 173

Single quotes vs double quotes in scheme

I hope this hasn't been asked already. I saw a bunch of single vs double quotes for other languages (html, javascript, python) but can't find scheme

In scheme at the interpreter, if I type:

(something 'x) I understand that the x will be treated as an x, not evaluated to something as if it is a variable

On the other hand, if I use

(something x)

x is evaluated as if its a variable

I know that ' is a short hand for quote (ie (quote x)) but what I don't get is how that differs from a double quote.

If I type

"hello" at the prompt, I get back "hello"

Is the only difference that the double quote keeps the quotes around the data? I've heard the double quote is like a char array, but it doesn't get evaluated and neither does the single quote, so that is the difference and when/why would I use one over the other?

Thanks all.

Upvotes: 1

Views: 2284

Answers (2)

Vladimir Nikishkin
Vladimir Nikishkin

Reputation: 159

The important thing is that 'not evaluated' doesn't mean 'not parsed'.

Compare:

>'(+ a b)
(mcons '+ (mcons 'a (mcons 'b '())))

>(string->symbol "(+ 1 2)")
'|(+ 1 2)|

Upvotes: 0

Fred Foo
Fred Foo

Reputation: 363567

In Scheme, single quotes and double quotes are entirely different constructs. Double quotes produce a string:

> (string? "foo")
#t

Whereas the prefix operator single quote prevents an expression from being evaluated. E.g. (+ 1 2) evaluates to 3, but when you single-quote it, you get a list consisting of +, 1 and 2:

> (define three '(+ 1 2))
> three
(+ 1 2)
> (car three)
+
> (cadr three)
1
> (caddr three)
2

It's actually syntactic sugar for an operator called quote, which you can verify by quoting twice:

> ''foo
(quote foo)

Upvotes: 8

Related Questions