I'm using scheme to output some s-expressions to a port and I would really like
to add comments to the stream I'm writing.
As the ; character will comment everything after I'm unsure how to actually
do this, a comment is not an s-expression so quoting would not work
(display ;some comment) ; obviously not working
(display ';some comment) ; does not make sense
(display '(;some comment)) ; could make sense, does not work
You can escape ; using string quotes.
(display ";some comment") works.
Related
I have been trying to use ANSI escape codes to clear the screen in simply scheme, but when I do so it either does not work and returns:
read-syntax: missing ']' to close preceding '[', found instead ')'
when trying (display '\033[2J)
Or when turning it into a string it prints out (in purple for some reason, even though it is the clear screen code)
[2J
when doing (display "\033[2J")
I have been stuck here for a while as (clear) does not work in Simply Scheme, and doing '\033\[2J only causes a different error. All help with this would be appreciated.
If you are interacting with Racket using DrRacket then ANSI escape codes won't work. If you are running Racket in a terminal which supports them, they will. There may be a way of erasing a buffer in DrRacket programmatically, but it's not this.
Why does (quote '"foo") go through the Scheme interpreter? It should be syntactically redundant or wrong based on how expressions are constructed in Scheme. quote is used whenever one wants to use a symbol without Scheme thinking it's a variable and strings aren't valid symbols so why is the abbreviation for the quote operator valid when prefixed to strings? Oddly enough (quote '"foo") returns (quote "foo"). Redundancy?
Another strange experiment (symbol? '"foo") is evaluated to #f so that proves that quoted strings still aren't symbols (if the quote works that way in a statement). So, is the ' ignored on strings or does it serve some purpose elsewhere? I am using Chicken Scheme.
Somewhat trivial but kinda mind boggling at the same time.
As stated in the specification:
(quote <datum>) evaluates to the datum value represented by (see section 4.3). This notation is used to include constants.
The above doesn't exclude the quotation of strings, in fact one of the examples in that section is this one:
'"abc" => "abc"
It follows that this is also valid:
''"abc" => ''"abc"
I read some answers here and googled, but had no luck.
I have this:
''a
in scheme (Chez scheme to be exact), and I want to turn it into a string (it's a case in my to-string lambda).
Now, asking if it's a symbol (using the symbol predicate) yields a positive answer, so I know when to operate, but I can't do anything after that since there is no way to get the inner-quote itself.
So basically I can't find a way to turn ''a into "a".
Hopefully this is simple, any help will be appreciated!
This expression:
''a
Is equivalent to this symbol definition (why the double quote, by the way? a single quote suffices):
(quote (quote a))
To turn it into a string, simply do this:
(symbol->string (cadr ''a))
=> "a"
I'm trying to learn scheme by myself. Could anyone tell me why '(quote quote) will output 'quote, and '(quote 'quote) will output ''quote?
Thank you very much!
This expression:
'(quote quote)
... after expanding '<something> to (quote <something>) is equivalent to (quote (quote quote)), notice that the symbol quote is being quoted two times, and this expression is evaluated and printed as ''quote.
On the other hand, this expression:
'(quote 'quote)
... is equivalent to (quote (quote (quote quote))), notice that the symbol quote is being quoted three times, and this expression is evaluated and printed as '''quote.
Take a look at (free, online) How To Design Programs, intermezzo 2. It explains quote in terms of list and cons. If anything in that explanation doesn't make sense, just back up a bit in the textbook.
PLT Scheme's documentation says:
The rationale for providing print
is that display and write both
have relatively standard output
conventions, and this standardization
restricts the ways that an environment
can change the behavior of these
procedures. No output conventions
should be assumed for print, so that
environments are free to modify the
actual output generated by print in
any way.
Could somebody please explain what that means for a noob and how is print and display different?
The thing is that programs can expect certain output formats from write and display. In PLT, it is possible to change how they behave, but a little involved to do so. This is intentional, since doing such a change can have dramatic and unexpected result.
OTOH, changing how print behaves is deliberately easy -- just see the current-print documentation. The idea is that print is used for debugging, for presenting code to you in an interactive REPL -- not as a tool that you will rely on for output that needs to be formatted in a specific way. (BTW, see also the "~v" directive for format, printf, etc.)
You are free to override print function. If you want to override standardized functions, for example the write, you must obey to the output standard, otherwise code that use it will possibly break.
About display and write:
The Scheme Programming Language, 3rd edition, pg. 178
(display obj)
(display obj output-port)
returns unspecified
display is similar to write
but prints strings and characters
found within obj directly. Strings
are printed without quotation marks or
slashes, and characters are printed
without #\ notation. For example,
both (display "(a b c)") and
(display '("a b" c)) would print (a b c). Because of this, display should not be used to print objects
that are indended to be read with
read. display is useful primarily for printing messages, with
obj most often being a string.
If you don't want to replace print you might try out SRFI-28 instead:
http://srfi.schemers.org/srfi-28/srfi-28.html
Basically print is meant for debugging ie, showing the data/value without any beautification or formatting, and display is meant for showing the data in your desired format or more beautified format
ex:
print(dataframe)
enter image description here
display(dataframe)
enter image description here