From: surendra
Subject: LISP - question (PLT Scheme)
Date: 
Message-ID: <cm7a31$ahu$1@news.asu.edu>
Hi,

I saw the recording of "lightweight languages Workshop 2002" and liked 
the talk about PLT Scheme. Is there any way of doing something similar 
in Ansi LISP.

To be more specific, I have written a program which reads in the program 
of students in my class and checks if it is correct. To do this I first 
load in their file and then execute the function. Although, I have asked 
them to return the values as a result, lot of students because of their 
C++, JAva background don't follow it, and print the result on the console.

So, what I want is that the output instead of being sent to the 
screen(terminal) be directed to a printer or some other stream. What 
will be the best way of doing that?

One more question which I had was about load-file, it takes in a print 
parameter with value t or nil, but is there any way I can tell it which 
stream to print the file on.

Thanks in advance,

Surendra Singhi

From: Frank Buss
Subject: Re: LISP - question (PLT Scheme)
Date: 
Message-ID: <cm7f17$l43$1@newsreader2.netcologne.de>
surendra <·········@netscape.net> wrote:

> So, what I want is that the output instead of being sent to the 
> screen(terminal) be directed to a printer or some other stream. What 
> will be the best way of doing that?

(with-open-file (s "somefile.txt" :direction :output :if-exists :supersede)
  (let ((*standard-output* s))
    (your-function)))

> One more question which I had was about load-file, it takes in a print
> parameter with value t or nil, but is there any way I can tell it
> which stream to print the file on.

should work the same way.

-- 
Frank Bu�, ··@frank-buss.de
http://www.frank-buss.de, http://www.it4-systems.de
From: Pascal Bourguignon
Subject: Re: LISP - question (PLT Scheme)
Date: 
Message-ID: <874qk8y8sj.fsf@thalassa.informatimago.com>
surendra <·········@netscape.net> writes:
> So, what I want is that the output instead of being sent to the
> screen(terminal) be directed to a printer or some other stream. What
> will be the best way of doing that?

That would depend on how they wrote their output expressions.


Assuming they wrote: (FORMAT T "..."), the "T" here means that the
output is sent to the stream referenced by the dynamic variable:
*STANDARD-OUTPUT*. (In the REPL, this stream is usually the same as
*TERMINAL-IO*, or in anycase, it's the stream that directs output to
the user interface).  So, an easy way to redirect output sent there is
to bind *STANDARD-OUTPUT* to a stream leading to where you want:

(with-open-file (*standard-ouput* "output.txt" 
                    :direction :output
                    :if-does-not-exist :create
                    :if-exists :append) ;; or :supersede
    (some-program))

Of course, if you want to convert outputs to a result, you could
redirect them to a string:

(with-output-to-string (*standard-output*) (some-program)) 
==> "output..."


There are other output stream that may be worthwhile to bind. Check
the reference page about *STANDARD-OUTPUT*.

 
> One more question which I had was about load-file, it takes in a print
> parameter with value t or nil, but is there any way I can tell it
> which stream to print the file on.

You haven't read the reference page about LOAD. _Read_ it now:
http://www.lispworks.com/reference/HyperSpec/Body/f_load.htm

-- 
__Pascal Bourguignon__                     http://www.informatimago.com/

Voting Democrat or Republican is like choosing a cabin in the Titanic.
From: surendra
Subject: Re: LISP - question (PLT Scheme)
Date: 
Message-ID: <cmc47e$fed$1@news.asu.edu>
Pascal Bourguignon wrote:
> surendra <·········@netscape.net> writes:
> 
>>So, what I want is that the output instead of being sent to the
>>screen(terminal) be directed to a printer or some other stream. What
>>will be the best way of doing that?
> 
> 
> That would depend on how they wrote their output expressions.
> 
> 
> Assuming they wrote: (FORMAT T "..."), the "T" here means that the
> output is sent to the stream referenced by the dynamic variable:
> *STANDARD-OUTPUT*. (In the REPL, this stream is usually the same as
> *TERMINAL-IO*, or in anycase, it's the stream that directs output to
> the user interface).  So, an easy way to redirect output sent there is
> to bind *STANDARD-OUTPUT* to a stream leading to where you want:
> 
> (with-open-file (*standard-ouput* "output.txt" 
>                     :direction :output
>                     :if-does-not-exist :create
>                     :if-exists :append) ;; or :supersede
>     (some-program))
> 
> Of course, if you want to convert outputs to a result, you could
> redirect them to a string:
> 
> (with-output-to-string (*standard-output*) (some-program)) 
> ==> "output..."
> 
> 

I haven't tried these techniques, because I am done with grading the 
assignment.
But I have some doubts about them. I am using ANSI Common lisp (clisp).

As far as I know Ansi lisp is lexically scoped. So will these functions 
work?
Because as far as my understanding goes, when we set *standard-output* 
to a different value in the above functions, then a new lexically scoped 
variable with the name *standard-output* should be created, but in 
functions, called in the "(some-program)", the global binding of 
*standard-output* would be used.

If thats not the case, and the above program works correctly, then 
please explain the anomaly or, point out the flaw in my (mis)understanding.
I have seen some people writing ugly codes like:

    (let ((car (first lst)) (cdr (rest list)))
      (print car))......


What I might have done to achieve the effect of redirecting the output is:

(let ((temp-stream *standard-output*))
    (setf *standard-output* file-stream)
    .......
     (setf *standard-output* temp-stream))


-Surendra Singhi
From: Rahul Jain
Subject: Re: LISP - question (PLT Scheme)
Date: 
Message-ID: <87fz3q41fu.fsf@nyct.net>
surendra <·········@netscape.net> writes:

> As far as I know Ansi lisp is lexically scoped. So will these functions
> work?

Variables are lexically scoped unless otherwise declared.

> Because as far as my understanding goes, when we set *standard-output*
> to a different value in the above functions, then a new lexically scoped
> variable with the name *standard-output* should be created, but in
> functions, called in the "(some-program)", the global binding of
> *standard-output* would be used.

*standard-output* is defined in the spec to be dynamically scoped.

-- 
Rahul Jain
·····@nyct.net
Professional Software Developer, Amateur Quantum Mechanicist
From: Peter Seibel
Subject: Re: LISP - question (PLT Scheme)
Date: 
Message-ID: <m3zn1yxqea.fsf@javamonkey.com>
surendra <·········@netscape.net> writes:

> I haven't tried these techniques, because I am done with grading the
> assignment. But I have some doubts about them. I am using ANSI
> Common lisp (clisp).
>
> As far as I know Ansi lisp is lexically scoped. So will these
> functions work?

Yes. Because *standard-output* is not lexically scoped. Variables in
Common Lisp are by default lexically scoped but can be declared
"special" which makes them dynamically scoped. And DEFVAR and
DEFPARAMETER both declare the variables they define as special. The
naming convention of *names* *like* *this* is used to indicate that a
variable is special.

-Peter

-- 
Peter Seibel                                      ·····@javamonkey.com

         Lisp is the red pill. -- John Fraser, comp.lang.lisp
From: surendra
Subject: Re: LISP - question (PLT Scheme)
Date: 
Message-ID: <cmcaou$h0u$1@news.asu.edu>
Peter Seibel wrote:
> surendra <·········@netscape.net> writes:
> 
> 
>>I haven't tried these techniques, because I am done with grading the
>>assignment. But I have some doubts about them. I am using ANSI
>>Common lisp (clisp).
>>
>>As far as I know Ansi lisp is lexically scoped. So will these
>>functions work?
> 
> 
> Yes. Because *standard-output* is not lexically scoped. Variables in
> Common Lisp are by default lexically scoped but can be declared
> "special" which makes them dynamically scoped. And DEFVAR and
> DEFPARAMETER both declare the variables they define as special. The
> naming convention of *names* *like* *this* is used to indicate that a
> variable is special.
> 
> -Peter
> 

What is the convention for declaring global variables? Is it the same as 
  the special variables?

-Surendra
From: Pascal Bourguignon
Subject: Re: LISP - question (PLT Scheme)
Date: 
Message-ID: <87wtx2w6sg.fsf@naiad.informatimago.com>
surendra <·········@netscape.net> writes:
> What is the convention for declaring global variables? Is it the same
> as the special variables?

Yes. In Common-Lisp, there's no lexical global variables. Only special
global variables.  (It seems that ISLISP has lexical global variables
(defglobal vs. defdynamic), but that's about the only thing it has
Common-Lisp has not).


-- 
__Pascal Bourguignon__
From: Coby Beck
Subject: Re: LISP - question (PLT Scheme)
Date: 
Message-ID: <Xtrid.92180$9b.32389@edtnps84>
"surendra" <·········@netscape.net> wrote in message 
·················@news.asu.edu...
> What I might have done to achieve the effect of redirecting the output is:
>
> (let ((temp-stream *standard-output*))
>    (setf *standard-output* file-stream)
>    .......
>     (setf *standard-output* temp-stream))

This above is just a broken and unsafe way of immitating what you will get 
with

(let ((*standard-output* file-stream))
   .....)

As *standard-output* is a special variable, the let binding is not lexical, 
it is dynamic and all later users of that variable will get your value 
unless they rebind it somewhere else.  The previous binding will be restored 
when you exit your let either normally or exceptionally.

-- 
Coby Beck
(remove #\Space "coby 101 @ big pond . com")
From: Frode Vatvedt Fjeld
Subject: Re: LISP - question (PLT Scheme)
Date: 
Message-ID: <2hlldky6qc.fsf@vserver.cs.uit.no>
surendra <·········@netscape.net> writes:

> So, what I want is that the output instead of being sent to the
> screen(terminal) be directed to a printer or some other stream. What
> will be the best way of doing that?

  (read-from-string
    (with-output-to-string (*standard-output*)
      (load "solution.lisp")))

  Personally, I would have failed students unable to comply with the
  simplest of requirements.

> One more question which I had was about load-file, it takes in a
> print parameter with value t or nil, but is there any way I can tell
> it which stream to print the file on.

Load's print output is specified to be output on standard
output. Remember that this is a _variable_, not a stream as such
(i.e. it's not like the stdout file-descriptor which is more tightly
bound to a particular file/stream).

In other words, just re-bind *standard-output* while executing load.

-- 
Frode Vatvedt Fjeld