reading file : bad syntax (multiple expressions after identifier) - scheme

(define bootstrap-c-code
(define (from-file file-name)
(let* ((ip (open-input-file file-name))
(res (read-text-file-from-input-port ip)))
(close-input-port ip)
res))
(from-file "llvm.c"))
Error : define: bad syntax (multiple expressions after identifier)
But I can't see anything wrong with it. Can someone explain / fix it please.

It's not clear what you intended with the above code. If you were trying to load a text file and leave the loaded value in a variable called bootstrap-c-code, then try this:
(define bootstrap-c-code
(let ((from-file
(lambda (file-name)
(let* ((ip (open-input-file file-name))
(res (read-text-file-from-input-port ip)))
(close-input-port ip)
res))))
(from-file "llvm.c")))
Of course, the from-file definition will only be visible inside the let, if you need to use it outside, define it outside of the whole expression. If you only need the functionality of from-file inside the let, you can obtain the same result in a much simpler way:
(define bootstrap-c-code
(let* ((ip (open-input-file "llvm.c"))
(res (read-text-file-from-input-port ip)))
(close-input-port ip)
res))
On the other hand, if what you intended was to create a procedure called bootstrap-c-code, then the correct syntax would be:
(define (bootstrap-c-code)
(define (from-file file-name)
(let* ((ip (open-input-file file-name))
(res (read-text-file-from-input-port ip)))
(close-input-port ip)
res))
(from-file "llvm.c"))

According to R5RS, internal definitions can occur only at the beginning of the of a bunch of forms like let, let*, lambda etc. In the case of your code, that is not the case since you have an internal definition inside a non-procedural define. You could fix it by making `bootstrap-c-code' bind to a procedure.

Related

Evaluating code which refers to function parameters

Update: The original version of this question did not fully describe the constraints of my situation. It now includes an example with some hand-waving for the sake of simplicity as well as a testable minimum example.
I am trying to pass a snippet of code into a procedure for evaluation at a later point in time. The system with all of the constraints looks like this:
; External Library code - cannot be changed
(define (serialize-parameters . args)
(format some-file "~A~%" args))
(define (function-called-later-possibly-after-a-system-reboot callback)
(apply callback some-default-parameters (deserialize-parameters some-file)))
; Internal library code - can be changed, but the value of `code-snippet` cannot be hardcoded
(define (callback some-default-parameters code-snippet)
(eval code-snippet (current-module))
; Application code - can be changed, can be hardcoded
(define (main)
(serialize-parameters #:code-snippet '(format #t "~A~%" some-default-parameters)))
The main point is to allow the application to execute an arbitrary code snippet in a callback function. The callback function receives other parameters which are not known to the application and may change between calls.
An important constraint is that the parameters that the application wants to send to the callback are written to a file and then read back at a later point in time, possibly after a system reboot.
This example is easier to test, but does not capture all of the constraints:
(define (test foo bar)
(eval bar (current-module)))
(test "value of foo" '(format #t "~A~%" foo))
Running this program results in Unbound variable: foo. What I want is for the test function to be defined in such a way that the result of the call will be "value of foo\n" being printed to the terminal. Is this possible in Guile?
Thank you.
This will no work. eval takes an environment and you pass it (current-module). That are top level bindings in the module, like test but not any lexical bindings like foo or bar. They just don't exist in the environment returned by (current-module).
You can do this:
(define foo "value of foo")
(eval '(format #t "~A~%" foo) (current-module))
; prints "value of foo"
; ==> #t
Also, the elephant in the room is that you can do this with lambdas:
(define (test value proc)
(proc value))
(test "value to process" (lambda (foo) (format #t "~A~%" foo)))
; prints "value to process"
; ==> #t
Alternatively, but I'm guessing you can't have format in callback because "code-snippet" can have many different values:
(define (main)
(serialize-parameters #:code-snippet "~A~%"))
(define (callback some-default-parameters code-snippet)
(format #t code-snippet some-default-parameters))
EDIT
I think you can do it semi hard-coded:
(define (main)
(serialize-parameters #:code-snippet 'print-format))
(define (callback argument message)
(case message
((print-format) (format #t "~A~%" argument))
((other-message) (handle-message ...))
(else ...)))
You can even make it a dynamic dispatcher. Eg. you do something like this:
(define messages '())
(define (register-callback message proc)
(set! messages (cons (cons message proc) messages)))
(define (callback argument message)
(let ((found (assq message messages)))
(when found
((cdr found) argument))))
(register-callback 'print-format (lambda (arg) (format #t "~A~%" arg)))
(callback "test" 'print-format) ; prints "test"
Now only the message gets stored in a file, which easily can be any data literal.

Common Lisp No Dispatch Character Defined

I am currently reading the chapter on read-time macros from Paul Graham's "On Lisp" book.
The problem I am encountering is the following. When I run one of his examples:
(set-dispatch-macro-character #\# #\?
#’(lambda (stream char1 char2)
‘#’(lambda (&rest ,(gensym))
,(read stream t nil t))))
I get the following error:
No dispatch function defined for #\’
Why is it happening? Could it be because I am running it at the REPL? What can one do to fix it?
The PDF from which you are copying the code uses punctuation marks outside the range of basic ASCII characters you are supposed to use here:
CL-USER> (char-name #\’)
"RIGHT_SINGLE_QUOTATION_MARK"
The usual quote symbol should instead use the apostrophe character:
CL-USER> (char-name #\')
"APOSTROPHE"
The same goes for backquote:
CL-USER> (char-name #\‘)
"LEFT_SINGLE_QUOTATION_MARK"
You should be writing instead:
(set-dispatch-macro-character #\# #\?
#'(lambda (stream char1 char2)
`#'(lambda (&rest ,(gensym))
,(read stream t nil t))))
The #' is not necessary before lambda, since Common Lisp also defines a macro named lambda which expands into (function (lambda ...)).
You can test your new read macro as follows:
CL-USER> #?10
#<FUNCTION (LAMBDA (&REST #:G617)) {1001C541FB}>
CL-USER> (funcall *)
10
When using SBCL, I obtain warnings about unused variables. This happens because the code declares variables in anonymous functions but never uses them. This is not a serious problem, but generally speaking, it is better to declare which variables are ignored:
(set-dispatch-macro-character
#\# #\?
(lambda (stream &rest chars)
(declare (ignore chars))
(let ((rest (gensym)))
`(lambda (&rest ,rest)
(declare (ignore ,rest))
,(read stream t nil t)))))

SICP Exercise 4.54

I have a question about Exercise 4.54 from Section 4.3.3 of Structure and Interpretation of Computer Programs (http://mitpress.mit.edu/sicp/full-text/book/book-Z-H-28.html#%_sec_4.3.3). This exercise concerns the Amb evaluator.
The exercise is the following:
If we had not realized that require could be implemented as an ordinary procedure that uses amb, to be defined by the user as part of a nondeterministic program, we would have had to implement it as a special form. This would require syntax procedures
(define (require? exp) (tagged-list? exp 'require))
(define (require-predicate exp) (cadr exp))
and a new clause in the dispatch in analyze
((require? exp) (analyze-require exp))
as well the procedure analyze-require that handles require expressions. Complete the following definition of analyze-require.
(define (analyze-require exp)
(let ((pproc (analyze (require-predicate exp))))
(lambda (env succeed fail)
(pproc env
(lambda (pred-value fail2)
(if <??>
<??>
(succeed 'ok fail2)))
fail))))
I completed it as follows:
(define (analyze-require exp)
(let ((pproc (analyze (require-predicate exp))))
(lambda (env succeed fail)
(pproc env
(lambda (pred-value fail2)
(if (false? pred-value)
(fail2) ;; or (fail)
(succeed 'ok fail2)))
fail))))
My doubt is the following:
I know that, during execution, when the predicate value pred-value is false, require should fail; that is, it should call a failure continuation procedure. But I'm a bit confused as to whether it should call (fail) or (fail2). Which one is correct?
(fail2) is the correct one. The procedure conforms to a continuation-passing style, and the correct continuation procedure in this case is fail2.
I think (fail) is correct.
Here we have got pred-value, which means pproc evaluates well for now. But if it is false value, we treat it as bad as pproc evaluates wrong. so the continuation here is fail not fail2.
A test can be found here:
https://wizardbook.wordpress.com/2011/01/22/exercise-4-54/
(fail2)is correct, well,most of the time it doesn't matter whichever you choose,but for the following conditons:
1.the predicate part contains (amb) such as
(require (amb false true false true true))
2.the predicate part contains (set! <...> <...>)
you can check it out,this is because most of the process's fail-continuation is the same as its caller's except for the above two kinds of processes. Though it sounds some kind of ridiculous.

How to ignore side-effect in Racket from using `set!`?

In Exercise 35.4.2 from HtDP, I've implemented the GUI and have a button called "Remove" which invokes a callback function. Here it is:
(define (cb-remove x)
((lambda (name result)
(cond
[(number? result) (remove-name name address-book)]
[else (draw-message msg "Not found")]))
(string->symbol (text-contents label-name))
(lookup (string->symbol (text-contents label-name)) address-book)))
When I run this, I get the following message: button-callback: result of type <Boolean> expected, your function produced #<set!-result>. The problem is that I have to call set! in order to change the address book. However, the result of set! is (void), which cannot cannot be combined with a Boolean type. How can I avert this problem? Thanks for any insight.
Simple:
(begin (set! foo bar) #t)

PLT Scheme: Converting one of the macros in 'Casting SPELs in LISP'

(defspel game-action (command subj obj place &rest rest)
`(defspel ,command (subject object)
`(cond ((and (eq *location* ',',place)
(eq ',subject ',',subj)
(eq ',object ',',obj)
(have ',',subj))
,#',rest)
(t '(i cant ,',command like that.)))))
Thats the code from http://www.lisperati.com/actions.html for the 'macro defining macro'. I can't seem to convert it appropriately to scheme. Can someone explain to me the process of creating this same sort of thing in Scheme?
This kind of macro is actually much simpler in Scheme, since you can do it all with define-syntax-rule (in standard Scheme code you will need define-syntax + syntax-rules). You basically do the same thing, minus the whole quote/unquote mess.
(defspel (game-action command subj obj place rest ...)
(defspel (command subject object)
(cond [(and (eq? *location* 'place)
(eq? 'subject 'subj)
(eq? 'object 'obj)
(have 'subj))
rest ...]
[else '(i cant command like that.)])))
And since this is actually most of the code, I ported the whole thing to PLT -- see the post on the mailing list.

Resources