From: David James Hahn
Subject: incrementing w/out setf
Date: 
Message-ID: <1m1r2iINNf7@uwm.edu>
Hey all.

I'm working on (finishing) a lisp program and I'm trying to improve it.
My prof. has mentioned that if you're writing good code, you really 
shouldn't be using 'setf' or at least sparingly.  

My question-then, I have a function that increments a counter, if the
condition if is met. Right now I just use 'setf', i.e. if the condition
is true, 'setf counter (+ counter 1)'.  I'm totally new to lisp...
this can be done with 'let' can't it?  

	This is what I tried with 'let'

		(defun number-misplaced (state)
		  (let  ((counter 0) 
			 (goal-state (find-goal-state state))
			
			(when (not (equal (caar goal-state) (caar state)))
				(+ counter 1))

				etc... 

	
		Any help or suggestions would be appreciated.!

				Thanks,
					Dave



-- 
David Hahn
University of Wisconsin : Milwaukee 
····@csd4.csd.uwm.edu

From: Stefan Voss
Subject: Re: incrementing w/out setf
Date: 
Message-ID: <1m28vhINNg7v@iraul1.ira.uka.de>
In article <···········@uwm.edu>, ····@csd4.csd.uwm.edu (David James Hahn) writes:
|> Hey all.
|> 
|> [stuff deleted]
|> My question-then, I have a function that increments a counter, if the
|> condition if is met. Right now I just use 'setf', i.e. if the condition
|> is true, 'setf counter (+ counter 1)'.  I'm totally new to lisp...
|> this can be done with 'let' can't it?  
|> 

There are two functions that increment a counter by one. A destructive and a
non-defstructive one.

destructive: (incf counter)
non-destructive (1+ counter)

incf has a second optional parameter that determines the incrementation step.
Because you want to avoid setf, incf should be appropriate for you.

|> [stuff deleted]
|> 	
|> -- 
|> David Hahn
|> University of Wisconsin : Milwaukee 
|> ····@csd4.csd.uwm.edu

Stefan Voss
(····@ira.uka.de)
From: Barry Margolin
Subject: Re: incrementing w/out setf
Date: 
Message-ID: <1m2cf2INNbks@early-bird.think.com>
In article <············@iraul1.ira.uka.de> ····@i40s16.ira.uka.de (Stefan Voss) writes:
>Because you want to avoid setf, incf should be appropriate for you.

(incf counter) is equivalent to (setf counter (1+ counter)).  The original
poster didn't elaborate on the reason for avoiding SETF, but I would expect
any such rule also to apply to all the macros that expand into SETF, such
as INCF and PUSH.  I'm assuming that the intent is to avoid side effects
(i.e. the professor is a functional programming advocate), not to avoid the
SETF operator alone.

-- 
Barry Margolin
System Manager, Thinking Machines Corp.

······@think.com          {uunet,harvard}!think!barmar
From: Barry Margolin
Subject: Re: incrementing w/out setf
Date: 
Message-ID: <1m2c82INNbip@early-bird.think.com>
In article <···········@uwm.edu> ····@csd4.csd.uwm.edu (David James Hahn) writes:
>I'm working on (finishing) a lisp program and I'm trying to improve it.
>My prof. has mentioned that if you're writing good code, you really 
>shouldn't be using 'setf' or at least sparingly.  

This seems like a strange rule, unless you're trying to write purely
functional programs.

>My question-then, I have a function that increments a counter, if the
>condition if is met. Right now I just use 'setf', i.e. if the condition
>is true, 'setf counter (+ counter 1)'.  I'm totally new to lisp...
>this can be done with 'let' can't it?  

The only way I can think of doing it is with a recursive function.  The
counter would be a parameter, and to increment it you would call yourself
recursively with an argument of (1+ counter).

But I don't see the point of this.  Incrementing counters is certainly one
of the cases that should fall under the "sparingly" option.

>		(defun number-misplaced (state)
>		  (let  ((counter 0) 
>			 (goal-state (find-goal-state state))
>			
>			(when (not (equal (caar goal-state) (caar state)))
>				(+ counter 1))

This *computes* counter+1, but it never does anything with it.
-- 
Barry Margolin
System Manager, Thinking Machines Corp.

······@think.com          {uunet,harvard}!think!barmar
From: Raymond K. Fink
Subject: Re: incrementing w/out setf
Date: 
Message-ID: <1993Feb19.154152.28137@inel.gov>
····@csd4.csd.uwm.edu (David James Hahn) writes:

>My prof. has mentioned that if you're writing good code, you really 
>shouldn't be using 'setf' or at least sparingly.  

As a professional Lisp programmer, I have to disagree with this.  Strongly.
Though maybe a matter of style, the use of the generalized setf macro makes 
for much cleaner, readable code IMHO by providing a visual unification
of location references for getting/setting values.  Did any other Lisp
professionals have a strong reaction to this?

>My question-then, I have a function that increments a counter, if the
>condition if is met. Right now I just use 'setf', i.e. if the condition
>is true, 'setf counter (+ counter 1)'.  I'm totally new to lisp...

(incf counter), or (incf counter n) for n != 1 , is the typical way to
do this, and is preferred over the setf in this case.

Ray Fink -- Idaho National Engineering Laboratory -- Idaho Falls ID 
	···@inel.gov			208-525-5431
========== long legal disclaimer follows, press n to skip ===========

Neither the United States Government or the Idaho National Engineering
Laboratory or any of their employees, makes any warranty, whatsoever,
implied, or assumes any legal liability or responsibility regarding any
information, disclosed, or represents that its use would not infringe
privately owned rights.  No specific reference constitutes or implies
endorsement, recommendation, or favoring by the United States
Government or the Idaho National Engineering Laboratory.  The views and
opinions expressed herein do not necessarily reflect those of the
United States Government or the Idaho National Engineering Laboratory,
and shall not be used for advertising or product endorsement purposes.
From: Lou Steinberg
Subject: Re: incrementing w/out setf
Date: 
Message-ID: <LOU.93Feb22130234@atanasoff.rutgers.edu>
···@INEL.GOV (Raymond K. Fink) writes:
   ····@csd4.csd.uwm.edu (David James Hahn) writes:

   >My prof. has mentioned that if you're writing good code, you really 
   >shouldn't be using 'setf' or at least sparingly.  

   As a professional Lisp programmer, I have to disagree with this.  Strongly.

As a long-time Lisp teacher, I have to raise a warning:  before reacting
to what Mr. Hahn's teacher said, we have to consider the context.

When I am teaching Lisp to people with a C/Fortran kind of background,
I often suggest that they use setq as little as possible.  This
encourages them to write lisp in a more lispish style, rather than
translating verbatim from Fortran.  I.e. I want to avoid them writing
something like
(let (x y z a b c)
  ... a bunch of code not referring to x or y ...
  (setq x (read))
  (setq y (foo 7))
  (setq z (bar x y))
  (print bar)
  ...)

When I read Mr. Hahn's posting I assumed his teacher uses set setf for
setting variables in preference to setq, and I assumed the teacher's
statement was really something on the order of "use let and functional
programming style rather than lots of setf's of variables".
--
					Lou Steinberg

uucp:   {pretty much any major site}!rutgers!aramis.rutgers.edu!lou 
internet:   ···@cs.rutgers.edu
From: Raymond K. Fink
Subject: Re: incrementing w/out setf
Date: 
Message-ID: <1993Feb22.231932.8521@inel.gov>
···@cs.rutgers.edu (Lou Steinberg) writes:
>···@INEL.GOV (Raymond K. Fink) writes:
>   ····@csd4.csd.uwm.edu (David James Hahn) writes:
>
>   >My prof. has mentioned that if you're writing good code, you really 
>   >shouldn't be using 'setf' or at least sparingly.  
>
>   As a professional Lisp programmer, I have to disagree with this.  Strongly.
>
>As a long-time Lisp teacher, I have to raise a warning:  before reacting
>to what Mr. Hahn's teacher said, we have to consider the context.
>
>When I am teaching Lisp to people with a C/Fortran kind of background,
>I often suggest that they use setq as little as possible.  This
>encourages them to write lisp in a more lispish style, rather than
>translating verbatim from Fortran.  I.e. I want to avoid them writing
>something like
>(let (x y z a b c)
>  ... a bunch of code not referring to x or y ...
>  (setq x (read))
>  (setq y (foo 7))
>  (setq z (bar x y))
>  (print bar)
>  ...)
 
An excellent example; I've seen a lot of similar code (ugh) when 
trying to use C/Fortran programmers on a Lisp project.  And no doubt
I spent a lot of time saying "don't use setf/setq"

>When I read Mr. Hahn's posting I assumed his teacher uses set setf for
>setting variables in preference to setq, and I assumed the teacher's
>statement was really something on the order of "use let and functional
>programming style rather than lots of setf's of variables".

That would be good advice IMHO.  On the other hand, I assumed that
his teacher was advocating a pure functional style, perhaps eschewing
the use of the generalized setf; for example, to always prefer
(rplaca ..) over (setf (car ..) ..).  I don't care much one way or 
the other about rplaca vs setf car, but I find that (setf (foo-bar-baz x)
is a big win compared to trying to remember whether the relevant function 
is called set-foo-bar-baz or foo-set-bar-baz or foo-bar-set-baz or.....
hence my reaction to the "don't use setf".

Ray Fink -- Idaho National Engineering Laboratory -- Idaho Falls ID 
	···@inel.gov			208-525-5431
========== long legal disclaimer follows, press n to skip ===========

Neither the United States Government or the Idaho National Engineering
Laboratory or any of their employees, makes any warranty, whatsoever,
implied, or assumes any legal liability or responsibility regarding any
information, disclosed, or represents that its use would not infringe
privately owned rights.  No specific reference constitutes or implies
endorsement, recommendation, or favoring by the United States
Government or the Idaho National Engineering Laboratory.  The views and
opinions expressed herein do not necessarily reflect those of the
United States Government or the Idaho National Engineering Laboratory,
and shall not be used for advertising or product endorsement purposes.
From: Milt Epstein
Subject: Re: incrementing w/out setf
Date: 
Message-ID: <C359Br.BwB@cs.uiuc.edu>
In <·················@atanasoff.rutgers.edu> ···@cs.rutgers.edu (Lou Steinberg) writes:

>···@INEL.GOV (Raymond K. Fink) writes:
>   ····@csd4.csd.uwm.edu (David James Hahn) writes:
>
>   >My prof. has mentioned that if you're writing good code, you really 
>   >shouldn't be using 'setf' or at least sparingly.  
>
>   As a professional Lisp programmer, I have to disagree with this.  Strongly.
>
>As a long-time Lisp teacher, I have to raise a warning:  before reacting
>to what Mr. Hahn's teacher said, we have to consider the context.
>
>When I am teaching Lisp to people with a C/Fortran kind of background,
>I often suggest that they use setq as little as possible.  This
>encourages them to write lisp in a more lispish style, rather than
>translating verbatim from Fortran.  I.e. I want to avoid them writing
>something like
>(let (x y z a b c)
>  ... a bunch of code not referring to x or y ...
>  (setq x (read))
>  (setq y (foo 7))
>  (setq z (bar x y))
>  (print bar)
>  ...)
>
>When I read Mr. Hahn's posting I assumed his teacher uses set setf for
>setting variables in preference to setq, and I assumed the teacher's
>statement was really something on the order of "use let and functional
>programming style rather than lots of setf's of variables".

Even worse, imagine the above code without the LET (then all those
variables would be global).  I've taught Intro to AI courses a few
times, and we use Lisp, which is new to most of the students.  We have
on occasion ended up with code that looks like this.  I've found it
easier to just tell them that they can't use SETF or SETQ (at least
for the first few assignments).  So I definitely agree that that is a
strong possibility as to what happened here -- the idea being to try
to force them into good programming habits.

-- 
Milt Epstein
Department of Computer Science
University of Illinois
·······@cs.uiuc.edu