Why have the let function when you already have setf?

Discussion of Common Lisp

Re: Why have the let function when you already have setf?

Postby yougene » Sun Jan 31, 2010 11:32 am

Is it possible to use setf without side effects if it's used to only modify variables passed in as parameters or generated within the function?

Is that how you write good lisp code?
yougene
 
Posts: 23
Joined: Thu Jan 21, 2010 1:23 pm

Re: Why have the let function when you already have setf?

Postby nuntius » Sun Jan 31, 2010 1:49 pm

yougene wrote:Is it possible to use setf without side effects if it's used to only modify variables passed in as parameters or generated within the function?


Have you programmed in other languages? LET is simply one way to define a local (lexically scoped) variable in lisp. In languages like perl, you might say "local x" or "my x". In C/C++/Java, you say "{int x; ...}". In lisp, you use "(let (x) ...)". SETF is the mutation/assignment operator; "(setf x 5)" is like "x=5" in other languages; it does not create a new variable.

Maybe the following snippet will help you.
Code: Select all
(defun f (x)
  (setf x 5) ; nobody outside F sees this
  x) ; until this return
(defun g (x)
  (let ((y (+ x 5)))
     (setf x 5)
     y)) ; here, the new value of X is never seen


That LET also shadows dynamic variables is a historical accident. Old lisps (before Scheme) didn't have lexical variables! Instead, there were several interesting schemes for how variables passed values.
User avatar
nuntius
 
Posts: 500
Joined: Sat Aug 09, 2008 10:44 am
Location: Burlington, MA

Re: Why have the let function when you already have setf?

Postby yougene » Sun Jan 31, 2010 2:22 pm

I understand what let does and what scope is, but thanks for the feedback.

My question was about how to program without side effects. Whether properly programming in lisp is simply a matter of maintaining referential transparency.
yougene
 
Posts: 23
Joined: Thu Jan 21, 2010 1:23 pm

Re: Why have the let function when you already have setf?

Postby gugamilare » Sun Jan 31, 2010 6:27 pm

yougene wrote:Is it possible to use setf without side effects if it's used to only modify variables passed in as parameters or generated within the function?

Is that how you write good lisp code?


You are right, setf will only have side effects if you use it to modify a global variable or a local variable outside of the current function (like, when creating a function, you can modify the values being defined in its surroundings). Sometimes, whether you use setf or let is just a matter of personal preference. For instance when you want your function to accept numbers and strings representing numbers, one way you can do to make sure that the variable holds a number is setf-ing it or rebinding it with let.

In Lisp, we consider setf to be a bad choice when it is not necessary. Lisp is about brevity, not walk on circles around what you are trying to do. In C, programmers often assign the result of a function just to pass it to another function, and they do that when they learn Lisp, which is considered bad Lisp. It is considered to be more Lispy style to nest the function calls, unless the nesting get too haired, in which case you generally use let, but a setf can also be used if it makes sense.

Also, in general, Lispers avoid side effects, but sometimes they are very useful. There are other languages where side effects are condemned with punishment, but that is not the case with Common Lisp ;)
gugamilare
 
Posts: 406
Joined: Sat Mar 07, 2009 6:17 pm
Location: Brazil

Re: Why have the let function when you already have setf?

Postby yougene » Sun Jan 31, 2010 7:43 pm

Thanks, I think I'm starting to get this functional programming thingamajig.
yougene
 
Posts: 23
Joined: Thu Jan 21, 2010 1:23 pm

Re: Why have the let function when you already have setf?

Postby hewih » Sun Feb 07, 2010 3:39 am

i'm just a newbie too and i don't know if this is considered good style but my functions usually start with a lot of let* bindings with function calls to get their values. this is especially true if i need a value more than once. in the actual function body there are usually only very few calculations going on.
User avatar
hewih
 
Posts: 30
Joined: Tue Jan 19, 2010 9:36 am

Re: Why have the let function when you already have setf?

Postby gugamilare » Sun Feb 07, 2010 7:47 am

hewih wrote:i'm just a newbie too and i don't know if this is considered good style but my functions usually start with a lot of let* bindings with function calls to get their values. this is especially true if i need a value more than once. in the actual function body there are usually only very few calculations going on.


Well, many good libraries do stuff this way, so I think you are safe :)
gugamilare
 
Posts: 406
Joined: Sat Mar 07, 2009 6:17 pm
Location: Brazil

Previous

Return to Common Lisp

Who is online

Users browsing this forum: Google [Bot] and 3 guests