This question already has answers here:
slime prints my (format ...) calls only when called function ends
(2 answers)
Closed 7 years ago.
I have the following code:
(defun Areacircle ()
(princ "Enter Radius: ")
(defparameter radius (read))
(defparameter area (* 3.1416 radius radius))
(princ "Area: ")
(write area))
(AreaCircle)
The problem is that, slime runs it with no problem but when I compile it with (compile-file "area.lisp" :output-file "area") it evaluates the (read) part first; it ask to pass the value to radius before printing anything.
Nothing in your code is forcing the (buffered) output to be sent to the terminal. Adding a call to (force-output) or (finish-output) will help.
Additionally, you re using defparameter where you should be using let. You're possibly also better off using pi instead of your approximation and using format with a suitable format string to print the result.
Related
I have been reading "The Little Schemer" in my spare time and trying to experiment with the syntax and I've come across a syntax issue I can't wrap my head around. I have a script that looks like this:
;;;; Chapter 2 examples
;;; Note: run this file with 'mit-scheme --quiet < c2.scm'
;; You can define variables using the "define" function
(define my-favorite-number 13)
;; You can define strings using double-quotes
(define my-favorite-color "green")
;; you define functions with lambdas. Functions auto return their last line
(define my-favorites
(lambda (color number)
(string-append "My favorite color is " color " and my favorite number is " number)))
; display will print
(display
(my-favorites my-favorite-color (number->string my-favorite-number)))
; and newline prints a newline
(newline)
This works perfectly when run.
Underneath that, I have a little more:
(define greet
(lambda (name)
(string-append "Hello, " name)))
(display (greet "World"))
When running this file, I can not seem to get "Hello, World"to print. The first display works beautifully, but the second one does not do anything, and I can't explain why. At first I thought this might have something to do with how scheme runs in an interactive environment normally, so I stripped out the first block of code, but still nothing happens.
If I load the file in an interactive session, the "greet" function works perfectly, so I think this must be something odd about the display function.
Can anyone explain why my first display works but my second one does not?
Here is a github link to the exact file
I am computer science student in university, and my professor give me assignment about Scheme..
then, i set up the scheme 9.2 version, and try to this but, i'm gonna be crazy because of this![enter image description here][1]
52 error> (display "hi")
hi
;Unspecified return value
what is that? ;Unspecified return value
i wanna just display "hi" !
i have questions, how to remove that and how call like that? error code?, check text? or... what? there is a word?
thanks to read....
Your are using the read-eval-print loop. This has the effect that every expression you feed it has a response. If you don't like that you can run the program instead of using the REPL.
How to run a program is not covered by the standard so you need to check the documentation for your chosen implementation.
Here is an example using Ikarus:
#!r6rs
(import (rnrs))
(+ 4 5)
(display "Hello, world!")
(newline)
I've saved it as test.scm and I start it like this:
$ ikarus --r6rs-script test.scm
Hello, world!
$
Notice the result of (+ 4 5) is not displayed. It is dead code since the value is calculated, but then thrown away since it is not used and then continues to evaluate the next expression. Nothing is printed unless you explicit ask it to. In Ikarus REPL you'll see this:
> (+ 4 5)
9
> (display "Hello, world!")
Hello, world!> (newline)
>
It doesn't print the undefiend value #<void>> in the Ikarus REPL. To see it you need to display it:
(display (if #f #t))
; ==> #<void> (not printed by the REPL), but `display` will print `#<void>`
I am following the SICP lectures from MIT, and this is what I tried to find the square root approximation of a number by Heron of Alexandria's method. This is my first time trying out lisp, sorry for making noobie mistakes.
(define guess 1)
(define (avg a b)
(/ (+ a b) 2))
(define (try guess x)
(if (goodEnough guess x)
guess
(improve guess x)))
(define (improve guess x)
(define guess (avg guess (/ x guess)))
(try guess x)
)
(define (goodEnough guess x)
(= guess (avg guess (/ x guess))))
(print (try 1 25))
I am using Chicken scheme compiler to print this. This is the output:
Error: (/) bad argument type: #<unspecified>
Call history:
1.a.SquareRootApproximation.scm:29: try
1.a.SquareRootApproximation.scm:17: goodEnough
1.a.SquareRootApproximation.scm:27: avg
1.a.SquareRootApproximation.scm:19: improve <--
Updated: I have changed my approach towards this problem using lisp with more abstraction, yet I can't figure out what this new error wants to imply. Any fixes? Thanks!
The value #<unspecified> is basically "void" in other languages. It is used as a return value whenever some procedure has nothing useful to return (for example, print will return this). It is also in some situations used as a temporary placeholder value, for example when handling an inner define.
Normally this temporary placeholder should not be visible to the user of the language, but it appears you've hit a strange edge case in the language (congratulations! This happens rarely). The error happens because (define guess (avg guess (/ x guess))) in the improve procedure is simultaneously defining a variable and using that variable. The behaviour of doing this is not well-specified, and some Scheme implementations will do what CHICKEN is doing (Guile, Gauche, Gambit) whereas others will give a somewhat more meaningful error message (MIT, Scheme48, Racket). The reason this is ill-specified has to do with the fact that inner define expands to letrec, because it allows mutually recursive procedures to be defined, but that creates a bit of an issue: what should happen for (define a b) (define b a), for example?
Your intention seems to be using the old guess variable that's passed as input to the procedure, so instead of using define you could use let to bind a new value for guess (how this should behave is well-specified), or just use a different name for it, like new-guess.
This question already has answers here:
What is the difference between quote and list?
(2 answers)
Closed 5 years ago.
(display (+ 1 2)) ; output 3
(display '(+ 1 2)) ; output (+ 1 2)
(display z) ; Error: execute: unbound symbol: "z" []
(display 'z) ; output z
What does quoting change the following expression into? A string, or a list, or both? The first example seems changed into a list, while the second seems into a single-character string.
Are strings and lists related types in Scheme?
Thanks.
'<datum> is but a synonym for (quote datum). It quotes exactly the datum that follows it. In your examples, first a syntactic list is quoted, and next it is only a symbol.
String is a primitive data type, while list is an aggregate, a cons of two values (of the languages I'm aware of so far, only Haskell defines strings as character lists, not sure if Erlang dares to do the same). Despite they can obviously be subject to the same generic algorithms, their methods, while being isomorphic, have different names in Scheme's standard library.
I'm refamiliarizing myself with Scheme and I've hit a problem that is probably reflecting a fundamental misunderstanding on my part.
Say I do the following in Scheme (using Guile in this case but it's the same in Chicken):
> (define x 5)
> x
5
> (string->symbol "x")
x
> (+ 5 (string->symbol "x"))
<unnamed port>:45:0: In procedure #<procedure 1b84960 at <current input>:45:0 ()>:
<unnamed port>:45:0: In procedure +: Wrong type: x
> (symbol? (string->symbol "x"))
#t
> (+ 5 x) ; here x is dereferenced to its value 5
10
> (+ 5 'x) ; here x is not dereferenced
<unnamed port>:47:0: In procedure #<procedure 1c7ba60 at <current input>:47:0 ()>:
<unnamed port>:47:0: In procedure +: Wrong type: x
I understand that string->symbol is returning a symbol, x, which is effectively quoted. However, I cannot figure out how to use the symbol returned by string->symbol in any later context. How can I have Scheme evaluate that symbol?
To give a background on why I want to do this, it's that I'm writing a C program with embedded Guile. I would like to be able to be able to access symbols defined in Guile by name from C, using for example scm_from_*_symbol or scm_string_to_symbol. The reasons these functions aren't working the way I thought they would is related to my core question above. Perhaps there's a better way to do what I want to do with Guile, but that's a different question. Right now I'm interested in the fundamental question above.
What you want is to evaluate the symbol (not to "dereference" it). I think this is what you meant:
(define x 5)
(+ 5 (eval 'x (interaction-environment)))
=> 10
Take a look at the documentation for further details.
You should read the chapter fly-evaluation of the Guile documentation.
You want eval and probably interaction-environment
I recommend reading the famous SICP and Queinnec's Lisp In Small Pieces
Symbols are not special in this sense, that is they are not easier to evaluate than ordinary strings.
Symbol is much like a string, it just doesn't have quotes around it. Well, the fundamental difference is, of course, not absence of quotes, but the fact that symbols are interned. This means that strings "x" and "x" are two different strings (although they are equal), while symbols 'x and 'x are actually the same object.