From: none
Subject: Implementing a stateful function
Date: 
Message-ID: <45f3d350$0$2457$db0fefd9@news.zen.co.uk>
I've been playing around, trying to get a stateful function working.

 

(defun make-counter ()
   (let ((i -1))
     #'(lambda ()
         (incf i))))

(setf counter (make-counter))

(setf counter1 (make-counter))
(funcall counter1)
(funcall counter)


So far, so good. But I'm wondering if it is possible to eliminate having 
to write "funcall" all the time, so that I could just do something like
(counter)


Another thing I've noticed: when I was writing my password manager, I 
occasionally found myself passing functions around, and using funcall. 
It struck me that by doing this, one doesn't need macros; although maybe 
some typing is saved.

From: Dan Bensen
Subject: Re: Implementing a stateful function
Date: 
Message-ID: <et0nfp$t67$1@wildfire.prairienet.org>
none wrote:
> So far, so good. But I'm wondering if it is possible to eliminate having 
> to write "funcall" all the time, so that I could just do something like
> (counter)

Do you want one global counter or several independent ones?
If more than one, is it enough to create them at compile time,
or do they have to be generated at runtime?

-- 
Dan
www.prairienet.org/~dsb
From: mark carter
Subject: Re: Implementing a stateful function
Date: 
Message-ID: <45f3e49d$0$32025$fa0fcedb@news.zen.co.uk>
Dan Bensen wrote:
> none wrote:
>> So far, so good. But I'm wondering if it is possible to eliminate 
>> having to write "funcall" all the time, so that I could just do 
>> something like
>> (counter)
> 
> Do you want one global counter or several independent ones?
> If more than one, is it enough to create them at compile time,
> or do they have to be generated at runtime?

I was mainly just experimenting, and checking my understanding of Lisp. 
I was aiming for several independent ones. I'm a n00b, so I take it that 
by "compile time" you mean a fixed number of predetermined counters, as 
opposed to "runtime", where the counters are generated as required. If 
so, then the former.
From: Dan Bensen
Subject: Re: Implementing a stateful function
Date: 
Message-ID: <et0tmb$v5i$1@wildfire.prairienet.org>
mark carter wrote:
> Dan Bensen wrote:
>> Do you want one global counter or several independent ones?
>> If more than one, is it enough to create them at compile time,
>> or do they have to be generated at runtime?
> 
> I was mainly just experimenting, and checking my understanding of Lisp. 
> I was aiming for several independent ones. I'm a n00b, so I take it that 
> by "compile time" you mean a fixed number of predetermined counters, as 
> opposed to "runtime", where the counters are generated as required.

Correct.

> If so, then the former.

First, you can make a single global counter function
by inverting your example:

> (defun make-counter ()
>   (let ((i -1))

Put the LET form on the outside, and put a DEFUN COUNTER form
in the body of the LET.  You'll have a counter function that you
call directly.

To make multiple counters, take your LET form with the DEFUN inside it,
and define a DEF-COUNTER macro that returns that form.  All you
have to do is make the macro take the name of the counter function
as an argument.  Replace "counter" in the defun with ",func-name"
or whatever you call the argument inside the macro.  Then you can
define as many counters as you want by calling DEF-COUNTER with
different name arguments.

That should be enough to get you started, unless you want
more specifics.

-- 
Dan
www.prairienet.org/~dsb
From: mark carter
Subject: Re: Implementing a stateful function
Date: 
Message-ID: <45f3fcad$0$32029$fa0fcedb@news.zen.co.uk>
Dan Bensen wrote:
> mark carter wrote:
>> Dan Bensen wrote:
>>> Do you want one global counter or several independent ones?
>>> If more than one, is it enough to create them at compile time,
>>> or do they have to be generated at runtime?
>>
>> I was mainly just experimenting, and checking my understanding of 
>> Lisp. I was aiming for several independent ones. I'm a n00b, so I take 
>> it that by "compile time" you mean a fixed number of predetermined 
>> counters, as opposed to "runtime", where the counters are generated as 
>> required.
> 
> Correct.
> 
>> If so, then the former.
> 
> First, you can make a single global counter function
> by inverting your example:
> 
>> (defun make-counter ()
>>   (let ((i -1))
> 
> Put the LET form on the outside, and put a DEFUN COUNTER form
> in the body of the LET.  You'll have a counter function that you
> call directly.
> 
> To make multiple counters, take your LET form with the DEFUN inside it,
> and define a DEF-COUNTER macro that returns that form.  All you
> have to do is make the macro take the name of the counter function
> as an argument.  Replace "counter" in the defun with ",func-name"
> or whatever you call the argument inside the macro.  Then you can
> define as many counters as you want by calling DEF-COUNTER with
> different name arguments.
> 
> That should be enough to get you started, unless you want
> more specifics.

I think I'm with you. Thanks.
From: Vassil Nikolov
Subject: Re: Implementing a stateful function
Date: 
Message-ID: <yy8vtzwrojrx.fsf@eskimo.com>
On Sun, 11 Mar 2007 10:00:48 +0000, none <···········@(none)"> said:
| ...
| Another thing I've noticed: when I was writing my password manager, I 
| occasionally found myself passing functions around, and using funcall. 
| It struck me that by doing this, one doesn't need macros; although maybe 
| some typing is saved.

  I am not sure if I am not reading more in this observation than was
  intended, but yes, it is true that, broadly speaking, programming with
  macros and programming with higher-order functions are equivalent:
  that is, in _what_ can be achieved with them, but not necessarily in
  other aspects, such as how easily, readably, maintainably, etc., or in
  matters of style and taste.  One good thing about Common Lisp is that
  it gives you a choice between these two approaches ["unlike" clause
  excised].

  ---Vassil.

-- 
Is your code free of side defects?
From: Lars Rune Nøstdal
Subject: Re: Implementing a stateful function
Date: 
Message-ID: <45f3df7f$0$29078$c83e3ef6@nn1-read.tele2.net>
On Sun, 11 Mar 2007 10:00:48 +0000, none wrote:

> I've been playing around, trying to get a stateful function working.
> 
>  
> 
> (defun make-counter ()
>    (let ((i -1))
>      #'(lambda ()
>          (incf i))))
> 
> (setf counter (make-counter))
> 
> (setf counter1 (make-counter))
> (funcall counter1)
> (funcall counter)
> 
> 
> So far, so good. But I'm wondering if it is possible to eliminate having 
> to write "funcall" all the time, so that I could just do something like
> (counter)
> 
> 
> Another thing I've noticed: when I was writing my password manager, I 
> occasionally found myself passing functions around, and using funcall. 
> It struck me that by doing this, one doesn't need macros; although maybe 
> some typing is saved.

Rainer mentioned `symbol-function' .. here's another way using a thing
similar to `let':

http://nostdal.org/~lars/programming/lisp/aromyxo/util/fflet.lisp


-- 
Lars Rune Nøstdal
http://nostdal.org/
From: Rainer Joswig
Subject: Re: Implementing a stateful function
Date: 
Message-ID: <joswig-03D816.11052611032007@news-europe.giganews.com>
In article <························@news.zen.co.uk>,
 none <···········@(none)"> wrote:

> I've been playing around, trying to get a stateful function working.
> 
>  
> 
> (defun make-counter ()
>    (let ((i -1))
>      #'(lambda ()
>          (incf i))))
> 
> (setf counter (make-counter))
> 
> (setf counter1 (make-counter))
> (funcall counter1)
> (funcall counter)
> 
> 
> So far, so good. But I'm wondering if it is possible to eliminate having 
> to write "funcall" all the time, so that I could just do something like
> (counter)
> 

(setf (symbol-function 'counter) (make-counter))


> 
> Another thing I've noticed: when I was writing my password manager, I 
> occasionally found myself passing functions around, and using funcall. 
> It struck me that by doing this, one doesn't need macros; although maybe 
> some typing is saved.

-- 
http://lispm.dyndns.org
From: Sacha
Subject: Re: Implementing a stateful function
Date: 
Message-ID: <NySIh.58794$ZZ6.414842@phobos.telenet-ops.be>
>> Another thing I've noticed: when I was writing my password manager, I 
>> occasionally found myself passing functions around, and using funcall. 
>> It struck me that by doing this, one doesn't need macros; although maybe 
>> some typing is saved.
> 

That's one of the best arguments against macros I did read here from 
those people advocating for the functional languages. though i believe a 
balance between high order functions and macros leads to writing 
programs that are easier to read and understand.

Sacha
From: Pascal Bourguignon
Subject: Re: Implementing a stateful function
Date: 
Message-ID: <874por6i9x.fsf@voyager.informatimago.com>
Sacha <····@address.spam> writes:

>>> Another thing I've noticed: when I was writing my password manager,
>>> I occasionally found myself passing functions around, and using
>>> funcall. It struck me that by doing this, one doesn't need macros;
>>> although maybe some typing is saved.
>>
>
> That's one of the best arguments against macros I did read here from
> those people advocating for the functional languages. though i believe
> a balance between high order functions and macros leads to writing
> programs that are easier to read and understand.

On the other hand, this is one of the best arguments FOR the macros.

For example, you can implement (while <test> <body>...)
either as:

(tagbody
  :test (unless <test> (go :end))
  <body> ...
  (go :test)
  :end)

or as:

( (lambda (myself test-chunk body-chunk)
    (when (funcall test-chunk)
      (funcall body-chunk)
      (funcall myself myself test-chunk body-chunk)))
  (lambda (myself test-chunk body-chunk)
    (when (funcall test-chunk)
      (funcall body-chunk)
      (funcall myself myself test-chunk body-chunk)))
  (lambda () <test>)
  (lambda () <body> ...) )

; (just imagine having to type these "mere" lambdas every time you
; want a while loop...)

But the main point here is that a macro here can hide these
implementation details.  If your algorithm needed intrinsicaly a
lambda, just write a lambda.  But if the algorithm uses lambda only as
an unimportant implementation detail, and could as well be written
otherwise, then it's a sign you need something to hide the
boilerplate, you need a syntactic abstration!



-- 
__Pascal Bourguignon__                     http://www.informatimago.com/
        Un chat errant
se soulage
        dans le jardin d'hiver
                                        Shiki
From: Barry Margolin
Subject: Re: Implementing a stateful function
Date: 
Message-ID: <barmar-AAA02A.12215911032007@comcast.dca.giganews.com>
In article <························@news.zen.co.uk>,
 none <···········@(none)"> wrote:

> Another thing I've noticed: when I was writing my password manager, I 
> occasionally found myself passing functions around, and using funcall. 
> It struck me that by doing this, one doesn't need macros; although maybe 
> some typing is saved.

Would you really like to have to write things like:

(if (> a b)
    #'(lambda () (format "A is greater~&"))
    #'(lambda () (format "A is not greater~&")))

There are indeed some languages that adopt this approach -- they 
generally have a more terse notation for their equivalent of lambda 
expressions, so you don't realize they're doing this.  Examples I can 
think of off the top of my head are Smalltalk and TCL.

-- 
Barry Margolin, ······@alum.mit.edu
Arlington, MA
*** PLEASE post questions in newsgroups, not directly to me ***
*** PLEASE don't copy me on replies, I'll read them in the group ***
From: Frank Buss
Subject: Re: Implementing a stateful function
Date: 
Message-ID: <14p17uuu3aoju.122cm7ogmcppj.dlg@40tude.net>
none wrote:

> So far, so good. But I'm wondering if it is possible to eliminate having 
> to write "funcall" all the time, so that I could just do something like
> (counter)

You can do this, like demonstrated in the other answers, but I wouldn't
recommend it, because the philosophy of Common Lisp is not to mix it,
because it makes it a bit harder for other Lisp programmers to read your
code. You can use Scheme, if you like this programming style.

It is the old Lisp1 vs. Lisp2 fight and I don't think that there is a
"right" answer.

-- 
Frank Buss, ··@frank-buss.de
http://www.frank-buss.de, http://www.it4-systems.de
From: Rainer Joswig
Subject: Re: Implementing a stateful function
Date: 
Message-ID: <joswig-9BFF74.13460611032007@news-europe.giganews.com>
In article <·······························@40tude.net>,
 Frank Buss <··@frank-buss.de> wrote:

> none wrote:
> 
> > So far, so good. But I'm wondering if it is possible to eliminate having 
> > to write "funcall" all the time, so that I could just do something like
> > (counter)
> 
> You can do this, like demonstrated in the other answers, but I wouldn't
> recommend it, because the philosophy of Common Lisp is not to mix it,
> because it makes it a bit harder for other Lisp programmers to read your
> code. You can use Scheme, if you like this programming style.

I don't think that named functions like this are against the
'Philosophy of Common Lisp'. Neither is Functional Programming.
Setting global symbol's function cells to functions is fine for me.
Also the FFLET is fine for me.

What I personally don't like are code fragments like this:


(((lambda (foo) (bar foo))
  ...)

((((foo bar) ... )


Another example:

; not CL
(let ((foo (bar baz)))
   ((foo ... )))

vs.


; not CL
(let ((foo (bar baz)))
  (let ((foo1 (foo ...)))
   (foo1 ... )))


or even


; CL
(let ((foo (bar baz)))
  (let ((foo1 (funcall foo ...)))
   (funcall foo1 ... )))

or

(fflet ((foo (bar baz)))
  (funcall (foo))



In larger code calling functions that are the results
of functions isn't easily to locate visually. I want
to see FUNCALL as a visual clue that a function call
is involved.




> It is the old Lisp1 vs. Lisp2 fight and I don't think that there is a
> "right" answer.

-- 
http://lispm.dyndns.org
From: Alan Crowe
Subject: Re: Implementing a stateful function
Date: 
Message-ID: <86slcbcrmg.fsf@cawtech.freeserve.co.uk>
Rainer Joswig <······@lisp.de> writes:

> In article <·······························@40tude.net>,
>  Frank Buss <··@frank-buss.de> wrote:
> 
> > none wrote:
> > 
> > > So far, so good. But I'm wondering if it is possible to eliminate having 
> > > to write "funcall" all the time, so that I could just do something like
> > > (counter)
> > 
> > You can do this, like demonstrated in the other answers, but I wouldn't
> > recommend it, because the philosophy of Common Lisp is not to mix it,
> > because it makes it a bit harder for other Lisp programmers to read your
> > code. You can use Scheme, if you like this programming style.
> 
> I don't think that named functions like this are against the
> 'Philosophy of Common Lisp'. Neither is Functional Programming.
> Setting global symbol's function cells to functions is fine for me.
> Also the FFLET is fine for me.
> 
> What I personally don't like are code fragments like this:
> 
> 
> (((lambda (foo) (bar foo))
>   ...)
> 
> ((((foo bar) ... )
> 
> 
> Another example:
> 
> ; not CL
> (let ((foo (bar baz)))
>    ((foo ... )))
> 
> vs.
> 
> 
> ; not CL
> (let ((foo (bar baz)))
>   (let ((foo1 (foo ...)))
>    (foo1 ... )))
> 
> 
> or even
> 
> 
> ; CL
> (let ((foo (bar baz)))
>   (let ((foo1 (funcall foo ...)))
>    (funcall foo1 ... )))
> 
> or
> 
> (fflet ((foo (bar baz)))
>   (funcall (foo))
> 
> 
> 
> In larger code calling functions that are the results
> of functions isn't easily to locate visually. I want
> to see FUNCALL as a visual clue that a function call
> is involved.
> 
> 
> 
> 
> > It is the old Lisp1 vs. Lisp2 fight and I don't think that there is a
> > "right" answer.

Is this a useful compromise? Consider writing code to work
over a general ring, that is, you pass in your own add and
mul, like this:

CL-USER> (defun general-combination (a b x y add mul)
           (funcall add (funcall mul a x)(funcall mul b y)))

The funcalls are obscuring the arithmetic, so instead write

CL-USER> (defun general-combination (a b x y add mul)
           (with-functions (add mul)
             (add (mul a x)(mul b y))))

depending on

CL-USER> (defmacro with-functions (names &body code)
           `(macrolet ,(loop for name in names
                              collect `(,name (&rest args)`(funcall ,',name ,@args)))
             ,@code))

Alan Crowe
Edinburgh
Scotland
From: Rainer Joswig
Subject: Re: Implementing a stateful function
Date: 
Message-ID: <joswig-8E056D.08410412032007@news-europe.giganews.com>
In article <··············@cawtech.freeserve.co.uk>,
 Alan Crowe <····@cawtech.freeserve.co.uk> wrote:

> Rainer Joswig <······@lisp.de> writes:
> 
> > In article <·······························@40tude.net>,
> >  Frank Buss <··@frank-buss.de> wrote:
> > 
> > > none wrote:
> > > 
> > > > So far, so good. But I'm wondering if it is possible to eliminate having 
> > > > to write "funcall" all the time, so that I could just do something like
> > > > (counter)
> > > 
> > > You can do this, like demonstrated in the other answers, but I wouldn't
> > > recommend it, because the philosophy of Common Lisp is not to mix it,
> > > because it makes it a bit harder for other Lisp programmers to read your
> > > code. You can use Scheme, if you like this programming style.
> > 
> > I don't think that named functions like this are against the
> > 'Philosophy of Common Lisp'. Neither is Functional Programming.
> > Setting global symbol's function cells to functions is fine for me.
> > Also the FFLET is fine for me.
> > 
> > What I personally don't like are code fragments like this:
> > 
> > 
> > (((lambda (foo) (bar foo))
> >   ...)
> > 
> > ((((foo bar) ... )
> > 
> > 
> > Another example:
> > 
> > ; not CL
> > (let ((foo (bar baz)))
> >    ((foo ... )))
> > 
> > vs.
> > 
> > 
> > ; not CL
> > (let ((foo (bar baz)))
> >   (let ((foo1 (foo ...)))
> >    (foo1 ... )))
> > 
> > 
> > or even
> > 
> > 
> > ; CL
> > (let ((foo (bar baz)))
> >   (let ((foo1 (funcall foo ...)))
> >    (funcall foo1 ... )))
> > 
> > or
> > 
> > (fflet ((foo (bar baz)))
> >   (funcall (foo))
> > 
> > 
> > 
> > In larger code calling functions that are the results
> > of functions isn't easily to locate visually. I want
> > to see FUNCALL as a visual clue that a function call
> > is involved.
> > 
> > 
> > 
> > 
> > > It is the old Lisp1 vs. Lisp2 fight and I don't think that there is a
> > > "right" answer.
> 
> Is this a useful compromise? Consider writing code to work
> over a general ring, that is, you pass in your own add and
> mul, like this:
> 
> CL-USER> (defun general-combination (a b x y add mul)
>            (funcall add (funcall mul a x)(funcall mul b y)))
> 
> The funcalls are obscuring the arithmetic, so instead write
> 
> CL-USER> (defun general-combination (a b x y add mul)
>            (with-functions (add mul)
>              (add (mul a x)(mul b y))))

As I said, if you can give a function a name
and call it via that name, that's fine for me.
It fits into my visual pattern when reading code and I can
see where the symbol is coming from.
I was talking about calling the result of higher-order functions.

> 
> depending on
> 
> CL-USER> (defmacro with-functions (names &body code)
>            `(macrolet ,(loop for name in names
>                               collect `(,name (&rest args)`(funcall ,',name ,@args)))
>              ,@code))
> 
> Alan Crowe
> Edinburgh
> Scotland

-- 
http://lispm.dyndns.org