Home | Notes | Languages | Programs | Homework |
Re: return values and errors |
Stephen Edwards (edwards@CS.VT.EDU)
Tue, 23 Oct 2001 12:25:46 -0400
Message-ID: <3BD59A0A.8F38C335@cs.vt.edu> Date: Tue, 23 Oct 2001 12:25:46 -0400 From: Stephen Edwards <edwards@CS.VT.EDU> Subject: Re: return values and errors
§ídësmâçk wrote:
>
> I want to say if the stack is null, then there has been a stack underflow.
> so I say... (null? stack (write-line "Stack underflow"))
I'm assuming that the above is one clause in a cond. Remember that the
"condition" is an S-expression, so you probably want to write this as:
(cond
; some stuff here
((null? stack) (write-line "Stack underflow"))
;^-----------^ ^----------------------------^
; condition action
; more conditions here
)
Now, in this situation, the "value" produced by cond is the "value"
produced by the clause in the cond that was selected. In this case,
if "stack" is an empty list, the right-hand side of the clause
(labeled "action") above is what will be evaluated. Thus, the
"value" returned by cond will be the value produced by evaluating
"action".
Two points:
1. a write/write-line/display call produces an "unspecified return
value", which means that in the code written above, cond will
produce an unspecified return value. This is probably not what
you want. Effectively, output operations like these are always
called for their side-effects, not for their return results.
2. cond allows the "action" to be a *series* of S-expressions; it
does not limit you to just one S-expression. If "action" is a
series of S-expressions, then the value returned by cond is the
value produced by the *last* S-expression in the selected "action".
This means you can do the following:
(cond
; some stuff here
((null? stack)
(display "Stack ")
(display "underflow.")
(newline)
714
)
; more conditions here
)
Here, 4 S-expressions are listed for the "action" when the stack is
empty. The first three cause output to be produced, but the return
value produced by cond is 714 (the last S-expression).
-- Steve
-- Stephen Edwards 604 McBryde Hall Dept. of Computer Science e-mail : edwards@cs.vt.edu U.S. mail: Virginia Tech (VPI&SU) office phone: (540)-231-5723 Blacksburg, VA 24061-0106 -------------------------------------------------------------------------------
Home | Notes | Languages | Programs | Homework |