How to disable Racket's any s-expression value output - racket

Example, I write a Racket script:
; filename: hello.rkt
#lang racket/base
(displayln "hello")
(+ 1 1)
and execute it:
$ racket hello.rkt
output is:
hello
2
I don't want it to print the number '2', how to disable result of the any S-expression value output?

You could use void to discard values, and begin to group several expressions together. E.g.
(void (begin (+ 1 1) (+ 2 2) (+ 3 3)))
This prints nothing.

Related

Different outputs about saved continuations in DrRacket's definitions window and interactions window

I'm new to Racket and I'm trying to use call/cc. When I put the following code in the definitions window
of DrRacket, run it, and type (saved-k 0) in the interactions window, I get two of 3 printed.
#lang racket ; the definitions window
(define saved-k #f)
(define (f)
(+ 1
(call/cc (λ (k) (set! saved-k k) 0))))
(+ 1 (+ 1 (f)))
; the interactions window
3
> (saved-k 0)
3
3
My understanding is that the saved continuation should be (+ 1 (+ 1 (+ 1 ?)))
where ? is the hole. Why are there two results printed?
When I try all of those things in the interactions window, everything looks normal.
; the interactions window
> (define saved-k #f)
> (define (f)
(+ 1
(call/cc (λ (k) (set! saved-k k) 0))))
> (+ 1 (+ 1 (f)))
3
> (saved-k 0)
3
Racket prints values at the top level. For example, running
#lang racket
1
2
will print 1 and 2.
How is this done? Well, when the program is run, it expands the code to something like:
(module test racket
(#%module-begin
(#%app call-with-values (lambda () '1) print-values)
(#%app call-with-values (lambda () '2) print-values)))
where print-values is responsible for the printing.
What happened in your program is that when the continuation is captured, it includes the print-values frame. That's why when you invoke the continuation, you see the double printing. One from print-values. Another from the evaluation result.
But when you try them all in the interactive window, there's no print-values, so there's no double printing.

Apparent 'eval' of quote symbol in CLISP

Some output from the CLISP REPL:
[1]> (list 'list 1 2 3)
(LIST 1 2 3)
[2]> (list 'list '(1 2 3))
(LIST (1 2 3))
[3]> (list 'quote 1 2 3)
(QUOTE 1 2 3)
[4]> (list 'quote '(1 2 3))
'(1 2 3)
The first three, I understand exactly what's going on: The list function is passed a symbol ('list or 'quote) and so the result is a list that begins with the list or quote symbol. It's the fourth that confuses me. Why doesn't it return (QUOTE (1 2 3))?
I realise that if you enter (QUOTE '(1 2 3)) into the REPL, you get '(1 2 3) back, so the expression are equivalent in that sense. But (LIST 1 2 3) is equivalent to (1 2 3), and yet the first expression doesn't return that.
It seems inconsitent that (list 'quote 1 2 3) returns a list with the first item being a quote symbol, but (list 'quote (1 2 3)) returns a quoted list. Especially since expressions like (list 'list ...) seem to always return a list beginning with the symbol - so far, at least, quote is the only 'special case' like this.
It's not the easiest question to articulate, so I'm hoping I've managed to get my confusion across. Can anyone explain why quote gets treated in this seemingly-unique way?
'something is the same as (quote something) for the lisp reader. Even when nested it will be the case. The next expressions I will double quote so that after evaluation one of the quotes are still in there.
When printing the implementations can choose what to output where there are several possible representations, so some implementations would print the evaluation of ''something as
(quote something) while others may use the abbreviation 'something.
'(quote 1 2 3) cannot be abbreviated since a quoted form only has one argument. Thus here both lisp systems would print (quote 1 2 3).
Here is a way to look at your last expression:
(let ((data (list 'quote '(1 2 3))))
(format nil
"whole thing: ~a first element: ~a second-element: ~a"
data
(car data)
(cadr data)))
This will either evaluate to "whole thing: '(1 2 3) first element: QUOTE second-element: (1 2 3)" or "whole thing: (QUOTE (1 2 3)) first element: QUOTE second-element: (1 2 3)".
Since the printer never sees if the input is abbreviated and the data has the same structure in memory the output is never affected by how you input the data. Thus (quote (quote (1 2 3))) will print the same as ''(1 2 3).
You have the same behaviour with cons cells but the standard dictates how the rules are. (cons 1 (cons 2 (cons 3 '()))) would be (1 . (2 . (3 . ()))) but is actually just printed (1 2 3) However if you (cons 1 2) you get (1 . 2) showing that print treats the output differently based on the cdr. However the reader can read any of these and they will all print the same eg. '(1 . (2 . (3 . ()))) ==> (1 2 3) and (+ . (2 . ( 3 . ()))) ; ==> 5
Numbers can have as many visual forms as there are bases below the number in question.
(let ((*print-base* 16))
(print 255)) ; prints FF (255 in hexadecimal)
list does not have any abbreviation or specialness in Lisp. It's not even a primitive function but it's very helpful as it removes the inconvenience of having to cons by hand everytime. It can be defined like this:
(defun my-list (&rest lst)
lst)
(my-list 1 2 3 4) ; ==> (1 2 3 4)
Note that a REPL (the READ-EVAL-PRINT-LOOP) does three things:
reading using the function READ
evaluating using the function EVAL
and printing the result using something like the function PRINT
To understand what is going on you have to look at all three functions.
Let's look at the third form:
(list 'quote 1 2 3)
This is read as a list of five elements:
LIST
(QUOTE QUOTE)
1
2
3
EVAL then evaluates the arguments, and calls the function list with the four results and returns a new result, a list of four elements:
QUOTE
1
2
3
PRINT then takes this list and writes it as: (QUOTE 1 2 3). There is no abbreviated way to print it.
Let's look at the fourth form:
(list 'quote '(1 2 3))
This is read as a list of three elements:
LIST
(QUOTE QUOTE)
(QUOTE (1 2 3))
eval calls list with two arguments:
QUOTE
(1 2 3)
eval then returns a list of length two:
QUOTE
(1 2 3)
print now can print this list in two different ways:
(QUOTE (1 2 3)) or the abbreviated form '(1 2 3). Here a quote character is in front of a single expression.
Your implementation used the first version.

List Item Evaluation

I'm learning lisp and have a question about a simple list:
(setq stuff '(one two three (+ 2 2)))
stuff ; prints "one two three (+ 2 2)"
(setq stuff (list `one `two `three (+ 2 2)))
stuff ; prints "one two three 4"
The first setq creates a list "one two three (+ 2 2)". The second list creates "one two three 4". Why does the first list not evaluate the (+ 2 2), but the second one does? I read in the Emacs Lisp intro documentation that when the list is built that it evaluates from the inside out. Why doesn't the first list evaluate the addition before adding it to the list?
This is elisp in emacs 24.
' is not equivalent to list, it's shorthand for quote. You're really doing this:
(setq stuff (quote (one two three (+ 2 2))))
The argument to quote is the expression (one two three (+ 2 2)).
From http://www.gnu.org/software/emacs/manual/html_node/elisp/Quoting.html: "The special form quote returns its single argument, as written, without evaluating it".
Looks like you're coming to grips with the evaluation semantics of Lisp, so keep playing around!
You can think of quote as suppressing evaluation of its argument. This allows you to write expressions that you can manipulate or pass around. It is also used to write data structures that should not be evaluated as function calls.
Data structures:
'(1 2 3) ; => '(1 2 3)
(1 2 3) ; => Lisp error: (invalid-function 1)
;; The Lisp reader sees the number 1 in the function position and tries to call it, signalling an error.
Syntax transformations:
(setq x '(string-to-int "123"))
(setf (car x) 'string-to-list)
x ; => '(string-to-list "123")
Delayed evaluation:
(setq x '(message "Hello World")) ; => '(message "Hello World")
(eval x) ; => "Hello World"
There is a closely related special operator called syntax quote, written using the backtick. It allows you to evaluate individual forms in a quoted expression using the comma ( , ) operator. It is like quote with an escape hatch.
`(1 2 (+ 3 4)) ; => '(1 2 (+ 3 4))
`(1 2 ,(+ 3 4)) ; => '(1 2 7) ;; Note the comma!
Syntax quote also permits list splicing using the ,# syntax:
`(1 2 ,#(+ 3 4)) ; => '(1 2 + 3 4)
As you can see, it splices the subsequent expression into the containing one. You probably won't see it all that often until you start writing macros.
list on the other hand is a simple function. It evaluates its arguments, then returns a new data structure containing these items.
(list 1 2 (+ 3 4)) ; => '(1 2 7)

I don't know how lisp macro construct its expansion? what's the exact step?

I tried to write a macro and execute it as follow. but it failed to execute.
(defmacro times_two (var) (* 2 var))
(times_two '(+ 1 2))
In my imagination, I think the expansion would be (* 2 (+ 1 2)). and after execution, the result would be 6. But failed.
I don't know why. I read the Emacs lisp manual, but I still can't understand them. I want to know what on earth the exact steps is while constructing expansion. What did the interpreter do?
When I evaluate these forms in Emacs, I get this error message when evaluating the second one:
Debugger entered--Lisp error: (wrong-type-argument number-or-marker-p (quote (+ 1 2)))
*(2 (quote (+ 1 2)))
(lambda (var) (* 2 var))((quote (+ 1 2)))
(times_two (quote (+ 1 2)))
eval((times_two (quote (+ 1 2))))
eval-last-sexp-1(nil)
eval-last-sexp(nil)
call-interactively(eval-last-sexp nil nil)
This is showing you how it expanded the macro, which should tell you what went wrong. (The final expansion is at the top.)
The quoted expression '(+ 1 2) gets passed to the times_two macro, but a quoted list is not a valid argument to the * function.
What you actually want here is:
(defmacro times_two (var) `(* 2 ,var))
(times_two (+ 1 2))
Keep in mind that, generally, the result of a macro will be new Lisp code, not a final value. Your goal in writing a macro is to construct the form that will give you the result you want. Thus, most of the time your macro will end up using the quasiquote (`) syntax.
I suspect that you're confusing compile-time and run-time. Macros run at compile time, producing code to be executed at run-time. Generally speaking it's hard to keep these straight and it makes writing macros difficult.
In any event, when I put this into ielm, I get:
ELISP> (defmacro times_two (var)
(* 2 var))
times_two
ELISP> (times_two '(+ 1 2))
*** Eval error *** Wrong type argument: number-or-marker-p, (quote (+ 1 2))
ELISP>
At least part of the problem then is the '(+ 1 2), but then when I remove the quote the following is no better:
ELISP> (times_two (+ 1 2))
*** Eval error *** Wrong type argument: number-or-marker-p, (+ 1 2)
ELISP>
It seems that elisp is looking for a number or a marker in the place where we are putting '(+ 1 2) and (+ 1 2). Let's try using a number:
ELISP> (times_two 3)
6
That works.
Interestingly, the macro expansion of this gives:
ELISP> (macroexpand '(times_two 3))
6
Which is probably not really what we want.
When we write macros we want to return expressions to be evaluated at run time. So rather than returning a number we can try this:
ELISP> (defmacro times_two (var)
`(* 2 ,var))
The backtick (quasiquote) is a way of creating a list, but also allowing interpolation with the use of a comma. Defining times_two in this way gives:
ELISP> (times_two (+ 1 2))
6
And the expansion of:
ELISP> (macroexpand '(times_two (+ 1 2)))
(* 2
(+ 1 2))
Which is exactly how you imagined it.

mcons in dr racket

I'm having trouble reading output from dr racket. By default it displays lists using mcons. For example, sicp exercise 2.32 produces:
> (subsets (list 1 2 3))
(mcons
(mcons
'()
(mcons
(mcons 3 '())
(mcons
(mcons 2 '())
(mcons
(mcons 2 (mcons 3 '()))
(mcons
(mcons 1 '())
(mcons
(mcons 1 (mcons 3 '()))
(mcons
(mcons 1 (mcons 2 '()))
(mcons (mcons 1 (mcons 2 (mcons 3 '()))) '()))))))))
'())
I'm having trouble reading this. Is there a way to make the output look like:
(() (3) (2) (2 3) (1) (1 3) (1 2) (1 2 3))
Thanks!
Do you know what language are you using in your #lang line? The rest of the instructions below are assuming that you're using a #lang line.
If you are in #lang r5rs and you display or write the values, you should see the output you expect.
> (define p (list 1 2))
> (display p)
(1 2)
> (set-car! p 'one)
> (display p)
(one 2)
If you just type the values bare in Interactions, DrRacket will print them, and that uses the representation you're seeing. In DrRacket, you can customize the way that values print. Here's the process, step-by-step:
Go to the Language menu, and select Choose Language. You should see the language dialog pop up.
If the button on the lower left says Show Details, click it, and the dialog window should expand to include customizations.
Look for the Output Style option. There should be four choices: Constructor, Quasiquote, write, and print. Select write style, and then press Ok to confirm the customization.
Once you do this, then:
> (display (list 1 2))
(1 2)
> (write (list 1 2))
(1 2)
> (list 1 2)
{1 2}
It will still print slightly differently than you expect, using curly braces, because it's trying to notate that the list structure is mutable.
If this bothers you, we can fix that. Add the following line near the top of your program (but after the #lang line).
(#%require r5rs/init)
This line pulls in a Racket-specific module called r5rs/init that tries to improve r5rs compliance; in particular, the braces should finally print as round ones for mutable pairs.
> (display (list 1 2))
(1 2)
> (write (list 1 2))
(1 2)
> (list 1 2)
(1 2)