Google Groups no longer supports new Usenet posts or subscriptions. Historical content remains viewable.
Dismiss

primitive question about functions

8 views
Skip to first unread message

jblazi

unread,
Mar 16, 2004, 8:33:35 AM3/16/04
to
(1)
Why does something like this not work:
(setq f (lambda (x) (+ x 1)))
(f 3)

though the following line works:
((lambda (x) (+ x 1)) 1)

(2)
And it is possible to define a function tmp that is only visible inside
another function f?

TIA,

--
jb


----== Posted via Newsfeed.Com - Unlimited-Uncensored-Secure Usenet News==----
http://www.newsfeed.com The #1 Newsgroup Service in the World! >100,000 Newsgroups
---= 19 East/West-Coast Specialized Servers - Total Privacy via Encryption =---

Hannah Schroeter

unread,
Mar 16, 2004, 9:17:35 AM3/16/04
to
Hello!

jblazi <jbl...@hotmail.com> wrote:
>(1)
>Why does something like this not work:
> (setq f (lambda (x) (+ x 1)))
> (f 3)

>though the following line works:
>((lambda (x) (+ x 1)) 1)

(funcall f 3)

Btw, you should use distinct symbol names for global variables, like
*f*.

>(2)
>And it is possible to define a function tmp that is only visible inside
>another function f?

See flet and labels in the Hyperspec.

>[...]

Kind regards,

Hannah.

Raymond Wiker

unread,
Mar 16, 2004, 9:50:23 AM3/16/04
to
jblazi <jbl...@hotmail.com> writes:

> (1)
> Why does something like this not work:
> (setq f (lambda (x) (+ x 1)))
> (f 3)
>
> though the following line works:
> ((lambda (x) (+ x 1)) 1)

You're setting the "value binding" for the symbol f. If a
symbol is used in the first position of a list, the "function
binding" is used.

> (2)
> And it is possible to define a function tmp that is only visible inside
> another function f?

Check the hyperspec for "labels" and "flet". Briefly,

(defun f
(flet ((tmp (args for tmp) ...) ...)
(body of f)))

--
Raymond Wiker Mail: Raymon...@fast.no
Senior Software Engineer Web: http://www.fast.no/
Fast Search & Transfer ASA Phone: +47 23 01 11 60
P.O. Box 1677 Vika Fax: +47 35 54 87 99
NO-0120 Oslo, NORWAY Mob: +47 48 01 11 60

Try FAST Search: http://alltheweb.com/

a

unread,
Mar 16, 2004, 11:10:42 AM3/16/04
to

"jblazi" <jbl...@hotmail.com> wrote in message
news:pan.2004.03.16....@hotmail.com...

> (1)
> Why does something like this not work:
> (setq f (lambda (x) (+ x 1)))
> (f 3)
>
> though the following line works:
> ((lambda (x) (+ x 1)) 1)
>
> (2)
> And it is possible to define a function tmp that is only visible inside
> another function f?
>
> TIA,
>
> --
> jb

CL-USER 194 > (setq f (lambda (x) (+ x 1)))
#'(LAMBDA (X) (+ X 1))

CL-USER 195 > (symbol-value 'f)
#'(LAMBDA (X) (+ X 1))

CL-USER 196 > (symbol-function 'f)

Error: Undefined function F in form (SYMBOL-FUNCTION F).
...

CL-USER 198 > (funcall (symbol-value 'f) 3)
4

CL-USER 199 > (setf (symbol-function 'f) (symbol-value 'f))
#'(LAMBDA (X) (+ X 1))

CL-USER 200 > (f 3)
4

Symbols have both a value slot and a function slot. (Symbols have more than
that, look in the CLHS.) That's at the heart of the Lisp-1 vs. Lisp-2
threads you see here from time to time.


a

unread,
Mar 16, 2004, 11:12:42 AM3/16/04
to

"jblazi" <jbl...@hotmail.com> wrote in message
news:pan.2004.03.16....@hotmail.com...
...

> (2)
> And it is possible to define a function tmp that is only visible inside
> another function f?
>
> TIA,
>
> --
> jb

I forgot to answer the second question.

(defun f (x)
(labels ((tmp (y)
(+ x y)))
(tmp (* 3 x))))

David Sletten

unread,
Mar 16, 2004, 4:47:03 PM3/16/04
to
jblazi wrote:

> (1)
> Why does something like this not work:
> (setq f (lambda (x) (+ x 1)))
> (f 3)
>
> though the following line works:
> ((lambda (x) (+ x 1)) 1)

As has already been pointed out, symbols in Lisp have several cells
associated with them. These are accessible via the SYMBOL- functions,
e.g., SYMBOL-VALUE, SYMBOL-PLIST, etc... You may have some exposure to
Scheme wherein a symbol has a single value rather than a value and
(potentially) a function as in Common Lisp. In that case what you are
trying to do would work as expected (I think...).

Typically we define a function using DEFUN:
(defun f1 (x) (+ x 1))

This is similar to the following:
(setf (symbol-function 'f1) #'(lambda (x) (+ x 1)))

In your example you assigned a value to F. When Lisp looked for a
function associated with F, it found none. FUNCALL can be used to invoke
a function such as the one you stored in F:
(funcall f 3) vs. (f1 3)

jblazi

unread,
Mar 16, 2004, 5:15:35 PM3/16/04
to

Thank you all for these answers. (I have not had any "exposure to Scheme",
I was simply trying to define a "local function".)

Joe Marshall

unread,
Mar 16, 2004, 6:31:54 PM3/16/04
to
Raymond Wiker <Raymon...@fast.no> writes:

> Check the hyperspec for "labels" and "flet". Briefly,
>
> (defun f
> (flet ((tmp (args for tmp) ...) ...)
> (body of f)))


(defun f (args of f)


(flet ((tmp (args for tmp) ...) ...)
(body of f)))


--
~jrm

Raymond Wiker

unread,
Mar 17, 2004, 2:30:40 AM3/17/04
to
Joe Marshall <prunes...@comcast.net> writes:

Aieee.... embarrassing. Even more embarrassing is that I spent
close to 30 seconds staring at your post, first wondering what the
difference was and then why _you_ omitted the arglist from the defun.

Joe Marshall

unread,
Mar 17, 2004, 9:18:56 AM3/17/04
to
Raymond Wiker <Raymon...@fast.no> writes:

> Joe Marshall <prunes...@comcast.net> writes:
>
>> Raymond Wiker <Raymon...@fast.no> writes:
>>
>>> Check the hyperspec for "labels" and "flet". Briefly,
>>>
>>> (defun f
>>> (flet ((tmp (args for tmp) ...) ...)
>>> (body of f)))
>>
>>
>> (defun f (args of f)
>> (flet ((tmp (args for tmp) ...) ...)
>> (body of f)))
>
> Aieee.... embarrassing. Even more embarrassing is that I spent
> close to 30 seconds staring at your post, first wondering what the
> difference was and then why _you_ omitted the arglist from the defun.

Nothing like having another set of eyeballs to expose the most
embarrassing typos!

Kaz Kylheku

unread,
Mar 17, 2004, 1:51:22 PM3/17/04
to
jblazi <jbl...@hotmail.com> wrote in message news:<pan.2004.03.16....@hotmail.com>...
> (1)
> Why does something like this not work:
> (setq f (lambda (x) (+ x 1)))
> (f 3)
>
> though the following line works:
> ((lambda (x) (+ x 1)) 1)

So this is where you are at, after three years of c.l.l participation?

jblazi

unread,
Mar 17, 2004, 4:19:40 PM3/17/04
to
On Wed, 17 Mar 2004 10:51:22 -0800, Kaz Kylheku wrote:

> So this is where you are at, after three years of c.l.l participation?

Yes, because I always stop.

I decided at least five times to abandon the idea of using Lisp in
teaching completely. But again and again I start over again. There is some
strange fascination of Lisp that no other computer language has. This time
it happened that I was told in a German newsgroup to use Haskell in
teaching (I use Python at the moment). So I downloaded GHC, it even worked
this time as they have a windows installer now. But then, while reading
the a book about it (the one by Hudak, which is excellent by the way) I
started telling to myself: This is completely crazy and if I really want
to stress functional aspects of programming, which seems to be a good
thing to me, I should prefer Lisp or Scheme.

So I downloaded MIT Scheme but it turned out that it cannot be used on
Windows, at least it cannot be started in Emacs any more. (I asked in the
Scheme NG and additionally in the mailing list but received no sensible
answer. (Yes, I know DrScheme and maybe I shall use it...)

So I finally downloaded CLisp again... It would be absolutely phantastic
for teaching if only I could do a *little* graphics with it (on XP),
sigh... At least what can be done with MIT
Scheme: opening a graphical window and drawing lines, rectangles, etc.

You see: This is deeply tragic passion-love.

Pascal Bourguignon

unread,
Mar 17, 2004, 9:05:34 PM3/17/04
to
jblazi <jbl...@hotmail.com> writes:

> On Wed, 17 Mar 2004 10:51:22 -0800, Kaz Kylheku wrote:
>
> > So this is where you are at, after three years of c.l.l participation?
>
> Yes, because I always stop.
>
> I decided at least five times to abandon the idea of using Lisp in
> teaching completely. But again and again I start over again. There is some
> strange fascination of Lisp that no other computer language has. This time
> it happened that I was told in a German newsgroup to use Haskell in
> teaching (I use Python at the moment). So I downloaded GHC, it even worked
> this time as they have a windows installer now. But then, while reading
> the a book about it (the one by Hudak, which is excellent by the way) I
> started telling to myself: This is completely crazy and if I really want
> to stress functional aspects of programming, which seems to be a good
> thing to me, I should prefer Lisp or Scheme.
>
> So I downloaded MIT Scheme but it turned out that it cannot be used on
> Windows, at least it cannot be started in Emacs any more. (I asked in the
> Scheme NG and additionally in the mailing list but received no sensible
> answer. (Yes, I know DrScheme and maybe I shall use it...)
>
> So I finally downloaded CLisp again... It would be absolutely phantastic
> for teaching if only I could do a *little* graphics with it (on XP),
> sigh... At least what can be done with MIT
> Scheme: opening a graphical window and drawing lines, rectangles, etc.
>
> You see: This is deeply tragic passion-love.

You should try DrScheme.

http://drscheme.org/
http://download.plt-scheme.org/drscheme/


--
__Pascal_Bourguignon__ http://www.informatimago.com/
There is no worse tyranny than to force a man to pay for what he doesn't
want merely because you think it would be good for him.--Robert Heinlein
http://www.theadvocates.org/

jblazi

unread,
Mar 17, 2004, 10:46:03 PM3/17/04
to
On Thu, 18 Mar 2004 03:05:34 +0100, Pascal Bourguignon wrote:

> You should try DrScheme.

Thx, I have. But when the reference implementation does not work? What is
the reason for offering MIT Scheme for Windows if you cannot work with it
in a sensible manner?

I am going to use CLisp this time. I am planning to write a few very
simple functions that will draw the objects I want to. The function calls
will be immediately translated into Postsrcipt and saved on Disk. Then it
can be viewed and printed with Gsview.

But this is just a plan.

Pascal Bourguignon

unread,
Mar 18, 2004, 12:43:27 AM3/18/04
to
jblazi <jbl...@hotmail.com> writes:

> On Thu, 18 Mar 2004 03:05:34 +0100, Pascal Bourguignon wrote:
>
> > You should try DrScheme.
>
> Thx, I have. But when the reference implementation does not work? What is
> the reason for offering MIT Scheme for Windows if you cannot work with it
> in a sensible manner?

Scheme, is defined no more than Common-Lisp by a reference implementation!

The reference for Scheme is the R5RS,
in which the last "R" means "Reference" !!

And anyway, perl, for example, does not run on CICS. (Or does it?)
That does not remove any of its qualities as a reference
implementation...

> I am going to use CLisp this time. I am planning to write a few very
> simple functions that will draw the objects I want to. The function calls
> will be immediately translated into Postsrcipt and saved on Disk. Then it
> can be viewed and printed with Gsview.
>
> But this is just a plan.

You could do better!
Why go thru the disk?

(let* ((win-w 512)
(win-h 342)
(win-back 0.33)
(but-x 10)
(but-y 140)
(but-w 76)
(but-h 20)
(but-back 0.66)
(but-title "Viva NeXT!")
(but-font "Helvetica")
(ps (ext:run-program "gs"
:arguments (list (format nil "-g~Dx~D" win-w win-h))
:input :stream)))
(format ps "newpath 0 0 moveto ~D 0 rlineto 0 ~D rlineto ~D 0 rlineto ~
closepath ~F setgray fill ~%"
win-w win-h (- win-w) win-back)
(format ps "newpath ~D ~D moveto ~D 0 rlineto 0 ~D rlineto ~D 0 rlineto ~
closepath ~F setgray fill ~%~
newpath ~D ~D moveto ~D 0 rlineto 0 ~D rlineto
1.00 setlinewidth ~
0.00 setgray stroke ~
newpath ~D ~D moveto 0 ~D rlineto ~D 0 rlineto
1.00 setgray stroke ~%"
but-x (- win-h but-y) but-w but-h (- but-w) but-back
but-x (- win-h but-y) but-w but-h
but-x (- win-h but-y) but-h but-w)
(format ps "0.00 setgray /~A findfont 12 scalefont setfont ~
~D ~D moveto (~A) show ~%"
but-font (+ but-x 6) (+ (- win-h but-y) 6) but-title)
(format ps "showpage~%")
(read-line) (format ps "~%quit~%"))


Of course, all this calls for intensive macrology...

Barry Margolin

unread,
Mar 18, 2004, 1:14:19 AM3/18/04
to
In article <877jxik...@thalassa.informatimago.com>,

Pascal Bourguignon <sp...@thalassa.informatimago.com> wrote:
> Scheme, is defined no more than Common-Lisp by a reference implementation!
>
> The reference for Scheme is the R5RS,
> in which the last "R" means "Reference" !!

R5RS is an abbreviation for Revised^5 Report on Scheme. Where do you
see "Reference"?

--
Barry Margolin, bar...@alum.mit.edu
Arlington, MA
*** PLEASE post questions in newsgroups, not directly to me ***

jblazi

unread,
Mar 18, 2004, 12:39:52 PM3/18/04
to

Thank you very much! Should this program run "out of the box"? I get the
error message "gsview: Error parsing command line" (in a window of gsview).

TIA,

Pascal Bourguignon

unread,
Mar 18, 2004, 4:44:40 PM3/18/04
to
Barry Margolin <bar...@alum.mit.edu> writes:

> In article <877jxik...@thalassa.informatimago.com>,
> Pascal Bourguignon <sp...@thalassa.informatimago.com> wrote:
> > Scheme, is defined no more than Common-Lisp by a reference implementation!
> >
> > The reference for Scheme is the R5RS,
> > in which the last "R" means "Reference" !!
>
> R5RS is an abbreviation for Revised^5 Report on Scheme. Where do you
> see "Reference"?

In my failing memory. Sorry.

Pascal Bourguignon

unread,
Mar 18, 2004, 4:47:13 PM3/18/04
to

jblazi <jbl...@hotmail.com> writes:

> On Thu, 18 Mar 2004 06:43:27 +0100, Pascal Bourguignon wrote:
>
> > You could do better!
> > Why go thru the disk?
> >

> > (ps (ext:run-program "gs"
> > :arguments (list (format nil "-g~Dx~D" win-w win-h))
> > :input :stream)))
> >

> > Of course, all this calls for intensive macrology...
>
> Thank you very much! Should this program run "out of the box"? I get the
> error message "gsview: Error parsing command line" (in a window of gsview).

Well, obviously I don't have a MS-Windows system available with
gsview. It made it with ghostscript on unix. The opions and their
syntax may be different. You could try first without any
argument. The purpose of -g is to specify the size of the ghostscript
window.

Gareth McCaughan

unread,
Mar 18, 2004, 5:13:16 PM3/18/04
to
Pascal Bourguignon wrote:

> Well, obviously I don't have a MS-Windows system available with
> gsview. It made it with ghostscript on unix. The opions and their
> syntax may be different. You could try first without any
> argument. The purpose of -g is to specify the size of the ghostscript
> window.

Is it really necessary
to add so much extra whitespace?
Justified monospaced text is hard
to read, and you've added
even more space than is needed
for justification.

--
Gareth McCaughan
.sig under construc

Pascal Bourguignon

unread,
Mar 18, 2004, 6:09:25 PM3/18/04
to
Gareth McCaughan <gareth.m...@pobox.com> writes:

1- ask it to the programmer of emacs full justify function.
2- I like it full justified.
3- your paragraph full justified looks like:

Is it really necessary to add so much extra whitespace? Justified
monospaced text is hard to read, and you've added even more space than
is needed for justification.

and is quite readable.

Gareth McCaughan

unread,
Mar 18, 2004, 8:24:02 PM3/18/04
to
Pascal Bourguignon wrote:

> > > Well, obviously I don't have a MS-Windows system available with
> > > gsview. It made it with ghostscript on unix. The opions and their
> > > syntax may be different. You could try first without any
> > > argument. The purpose of -g is to specify the size of the ghostscript
> > > window.
> >
> > Is it really necessary
> > to add so much extra whitespace?
> > Justified monospaced text is hard
> > to read, and you've added
> > even more space than is needed
> > for justification.
>
> 1- ask it to the programmer of emacs full justify function.
> 2- I like it full justified.
> 3- your paragraph full justified looks like:
>
> Is it really necessary to add so much extra whitespace? Justified
> monospaced text is hard to read, and you've added even more space than
> is needed for justification.
>
> and is quite readable.

1. It's no affair of mine what tools you use to get your articles
to look the way they do.

2. I find your articles consistently hard to read simply because
they have uneven spacing because of full justification. Of course
you must do as you like.

3. I was exaggerating for effect. I apologize if that wasn't
obvious.

Edi Weitz

unread,
Mar 19, 2004, 4:02:47 AM3/19/04
to
On 19 Mar 2004 01:24:02 +0000, Gareth McCaughan <gareth.m...@pobox.com> wrote:

> I find your articles consistently hard to read simply because they
> have uneven spacing because of full justification.

<AOL>
Me too.
</AOL>

Pascal Bourguignon

unread,
Mar 19, 2004, 10:23:50 AM3/19/04
to
Edi Weitz <e...@agharta.de> writes:

Ok. Since I don't see much full justified text from others, I won't
bother calling a vote on this and I'll try to shift my finger ten
millimeters and hit F5 instead if F6 to justify text for newsgroup.
But I'll continue to full justify my texts for my eyes, nah!

Jens Axel Søgaard

unread,
Mar 19, 2004, 10:31:40 AM3/19/04
to
Pascal Bourguignon wrote:

> 3- your paragraph full justified looks like:

> and is quite readable.

The problem is with long paragraphs. No with short ones.
When the lines have uneven lengths I find it easier to find
the next line.

I might be biased though - I don't like even margins in boks
either.

--
Jens Axel Søgaard

jblazi

unread,
Mar 19, 2004, 10:43:30 AM3/19/04
to
On Thu, 18 Mar 2004 22:47:13 +0100, Pascal Bourguignon wrote:
> Well, obviously I don't have a MS-Windows system available with
> gsview. It made it with ghostscript on unix. The opions and their
> syntax may be different. You could try first without any
> argument. The purpose of -g is to specify the size of the ghostscript
> window.

I do not know if this works on Windows at all.
If I write ":arguments ()" instead of your arguments (and I vaguely
remember that I do not have to quote the empty list), then gsview opens
without a file.

If test.ps is a valid ps file and I say (at the command line)
gsview < test.ps,
gsview will open but the drawing in test.ps is not shown. This is what
ext:run-program dows, if I understand this correctly.
But if I say
gsview < test.ps,
the drawing is shown.

Anyway, I am getting convinced that clisp is the right tool for me as I
only needed *some* capability of displaying drawings and Postscript is
definitely powerful enough for our needs :)
Having to creat a named file on disk is not much of an inconvenience and
if want to do this, I can even make that transparent to my pupils.

Your suggestion were very helpful however, as now I know how to run
external programs.

One further question: I saw, there is support for sockets built into
CLisp. Can I then connect to a news server like text-west.newsfeeds.com
using nntp?

Pascal Bourguignon

unread,
Mar 19, 2004, 10:55:47 AM3/19/04
to
jblazi <jbl...@hotmail.com> writes:

> On Thu, 18 Mar 2004 22:47:13 +0100, Pascal Bourguignon wrote:
> > Well, obviously I don't have a MS-Windows system available with
> > gsview. It made it with ghostscript on unix. The opions and their
> > syntax may be different. You could try first without any
> > argument. The purpose of -g is to specify the size of the ghostscript
> > window.
>
> I do not know if this works on Windows at all.
> If I write ":arguments ()" instead of your arguments (and I vaguely
> remember that I do not have to quote the empty list), then gsview opens
> without a file.

Yes, that's the point. Don't you have pipes on MS-Windows?



> If test.ps is a valid ps file and I say (at the command line)
> gsview < test.ps,
> gsview will open but the drawing in test.ps is not shown. This is what
> ext:run-program dows, if I understand this correctly.
> But if I say
> gsview < test.ps,
> the drawing is shown.
>
> Anyway, I am getting convinced that clisp is the right tool for me as I
> only needed *some* capability of displaying drawings and Postscript is
> definitely powerful enough for our needs :)
> Having to creat a named file on disk is not much of an inconvenience and
> if want to do this, I can even make that transparent to my pupils.

The advantage of using a pipe is to get the poscript code displayed as
soon as it's written by the lisp code. You can easily test and debug
your postscript generating lisp code.

> Your suggestion were very helpful however, as now I know how to run
> external programs.
>
> One further question: I saw, there is support for sockets built into
> CLisp. Can I then connect to a news server like text-west.newsfeeds.com
> using nntp?

You can write a NNTP client in clisp yes.

http://www.cliki.net/admin/search?words=nntp

Sunnan

unread,
Mar 21, 2004, 1:42:41 PM3/21/04
to
Pascal Bourguignon <sp...@thalassa.informatimago.com> writes:
> Ok. Since I don't see much full justified text from others, I won't
> bother calling a vote on this and I'll try to shift my finger ten
> millimeters and hit F5 instead if F6 to justify text for newsgroup.
> But I'll continue to full justify my texts for my eyes, nah!

You can still drop three spaces from each line above:

-re-justified quote starts


Ok. Since I don't see much full justified text from others, I won't
bother calling a vote on this and I'll try to shift my finger ten
millimeters and hit F5 instead if F6 to justify text for newsgroup.
But I'll continue to full justify my texts for my eyes, nah!

-quote ends

(PS, I don't mind either way.)

Sunnan

unread,
Mar 22, 2004, 12:11:28 PM3/22/04
to
Hmm, I found this quote last night:

"Idiosyncratic indentations, double-spacing, capitalization, etc., while
stamps of individuality, leave one an easy target for parody."
-- from the Symbolics Guidelines for Sending Mail

0 new messages