Account Options

  1. Sign in
The old Google Groups will be going away soon, but your browser is incompatible with the new version.
Google Groups Home
« Groups Home
Common Lisp style question
There are currently too many topics in this group that display first. To make this topic appear first, remove this option from another topic.
There was an error processing your request. Please try again.
flag
  9 messages - Collapse all  -  Translate all to Translated (View all originals)
The group you are posting to is a Usenet group. Messages posted to this group will make your email address visible to anyone on the Internet.
Your reply message has not been sent.
Your post was successful
 
From:
To:
Cc:
Followup To:
Add Cc | Add Followup-to | Edit Subject
Subject:
Validation:
For verification purposes please type the characters you see in the picture below or the numbers you hear by clicking the accessibility icon. Listen and type the numbers you hear
 
Edward Kenworthy  
View profile  
 More options Jun 4 2011, 3:07 am
From: Edward Kenworthy <edw...@kenworthy.info>
Date: Sat, 4 Jun 2011 08:07:23 +0100
Local: Sat, Jun 4 2011 3:07 am
Subject: Common Lisp style question

Hi

Given:

(defstruct board num-columns num-rows squares)

Which is considered better Common Lisp style?

(defun draw-board (board)
  (let ((num-rows (board-num-rows board))
         (num-columns (board-num-columns board))
         (squares (board-squares board)))
    (loop for row below num-rows
        do (draw-row-pieces squares row num-rows num-columns)
             (draw-row-squares row num-rows num-columns))))

which avoids repeatedly looking up num-rows, num-columns and  squares (but which I worry is more imperative-style), or:

(defun draw-board (board)
    (loop for row below (board-num-rows board)
        do (draw-row-pieces (board-squares board) row (board-num-rows board) (board-num-columns board))
             (draw-row-squares row (board-num-rows board) (board-num-columns board))))

which avoids using a let, feels less imperative, and doesn't prematurely optimise the code and lets the compiler optimise (or at least assumes the compiler will do so) but is also more cluttered.

Any thoughts?

Edward


 
You must Sign in before you can post messages.
To post a message you must first join this group.
Please update your nickname on the subscription settings page before posting.
You do not have the permission required to post.
Purity Control  
View profile  
 More options Jun 4 2011, 4:47 am
From: Purity Control <cr...@craigferry.net>
Date: Sat, 4 Jun 2011 01:47:36 -0700 (PDT)
Local: Sat, Jun 4 2011 4:47 am
Subject: Re: Common Lisp style question
I can't comment on the best lisp style or which is more optimised -
there are more experienced lispers out there that will be able to shed
light on that.

As for styles I prefer the first one as it is clearer in intent (to me
anyway).
However it does look more imperative on the face of it but after much
thinking I'm not sure it is.

You are certainly creating variables that can be modified, but you
don't, you only use them as references. The fact that you could modify
the variable is a property of the struct. So although you don't you
could modify the struct in the second function also.

So I have an additional question.
What do you think makes the language more imperative. Is it the
assignment of variables or is it the use of mutable data structures.
eg. If the data structures were immutable in your example would you
worry about the lets?

On Jun 4, 8:07 am, Edward Kenworthy <edw...@kenworthy.info> wrote:


 
You must Sign in before you can post messages.
To post a message you must first join this group.
Please update your nickname on the subscription settings page before posting.
You do not have the permission required to post.
Purity Control  
View profile  
 More options Jun 4 2011, 5:47 am
From: Purity Control <cr...@craigferry.net>
Date: Sat, 4 Jun 2011 02:47:25 -0700 (PDT)
Local: Sat, Jun 4 2011 5:47 am
Subject: Re: Common Lisp style question
Thinking about the compiler issue a little more.
While I don't know the answer as to which is more optimised. I
wouldn't let it affect my decision on going for the clearer code.
When you choose to develop in a higher level language you make the
implicit decision to trade an element of speed for faster, clearer
code that is easier to develop and maintain.
I think the only time to consider those questions would be if you were
hitting bottle necks in your code.

On Jun 4, 9:47 am, Purity Control <cr...@craigferry.net> wrote:


 
You must Sign in before you can post messages.
To post a message you must first join this group.
Please update your nickname on the subscription settings page before posting.
You do not have the permission required to post.
Peter Frings  
View profile  
 More options Jun 4 2011, 7:33 am
From: Peter Frings <peter.fri...@gmail.com>
Date: Sat, 4 Jun 2011 13:33:32 +0200
Local: Sat, Jun 4 2011 7:33 am
Subject: Re: Common Lisp style question
I prefer the first, with the (let).

As Craig said, it's clearer for the programmer and the ones that need to maintain it afterwards. Let the compiler figure out how to compile it in the most efficient way, and only optimize when it's indeed a verified bottleneck. In Knuth's words: "premature optimization is the root of all evil". (About optimization: often, using a better algorithm gains you more that tweaking a few bits here and there.)

So, go for the most understandable code.

As for the question whether it's too imperative, I would answer no. While the language allows you to modify those variables,  you don't: you use them as constants. It's not because the language allows mutation, that your code is imperative. It's the code. (Which would've been my answer to the *ear-muffs* discussion a couple of days ago as well.)

A similar question is when using (nreverse) when returning a newly constructed list (common idiom in recursion with accumulators). It's not strictly functional, since you modify a variable. But for all practical purposes, it is functional (and it's also a bit of premature optimization :-) ).

So, adhere to the *ideas* of FP and avoid `extremism'. Else learn Haskell where even (random) is strict :-)

Cheers,
Peter.


 
You must Sign in before you can post messages.
To post a message you must first join this group.
Please update your nickname on the subscription settings page before posting.
You do not have the permission required to post.
Edward Kenworthy  
View profile  
 More options Jun 4 2011, 10:31 am
From: Edward Kenworthy <edw...@kenworthy.info>
Date: Sat, 4 Jun 2011 15:31:23 +0100
Local: Sat, Jun 4 2011 10:31 am
Subject: Re: Common Lisp style question
I was thinking the use of let to create local variables (as opposed to creating a closure) was what felt 'imperative'- immutability issues etc didn't concern me at all.

Creating the local variables felt 'right' to me as well- but then I have 30 years experience of developing in Algol-family languages so I can't necessarily trust my instincts when writing Lisp code!

On 4 Jun 2011, at 09:47, Purity Control wrote:


 
You must Sign in before you can post messages.
To post a message you must first join this group.
Please update your nickname on the subscription settings page before posting.
You do not have the permission required to post.
Purity Control  
View profile  
 More options Jun 4 2011, 12:06 pm
From: Purity Control <cr...@craigferry.net>
Date: Sat, 4 Jun 2011 09:06:39 -0700 (PDT)
Local: Sat, Jun 4 2011 12:06 pm
Subject: Re: Common Lisp style question
I might not be understanding properly what you are trying to do, but
in this instance I think a closure would be a bad idea.
Then only thing you are binding to is the board struct.
A closure binds its variables when it is created.
The idea of draw-board is to show your current status.
A closure would end up showing the status of the board when you
created the closure.

On Jun 4, 3:31 pm, Edward Kenworthy <edw...@kenworthy.info> wrote:


 
You must Sign in before you can post messages.
To post a message you must first join this group.
Please update your nickname on the subscription settings page before posting.
You do not have the permission required to post.
Sequiturian  
View profile  
 More options Nov 6 2012, 1:45 pm
From: Sequiturian <lord.lar...@gmail.com>
Date: Tue, 6 Nov 2012 10:45:06 -0800 (PST)
Local: Tues, Nov 6 2012 1:45 pm
Subject: Re: Common Lisp style question

Edward,

I prefer the second style; not because it's less imperative-style, but
because I prefer not to have to go chasing down lexical variables all over
the code in order to understand a function. For me, the fewer lexical
variable used in lisp code, the better to see where all the intermediate
results are coming from; hence, the easier to understand the code and see
that is free of bugs.

LL


 
You must Sign in before you can post messages.
To post a message you must first join this group.
Please update your nickname on the subscription settings page before posting.
You do not have the permission required to post.
Munawar Cheema  
View profile  
 More options Dec 30 2012, 8:54 am
From: Munawar Cheema <munawar.a.che...@gmail.com>
Date: Sun, 30 Dec 2012 05:54:53 -0800 (PST)
Local: Sun, Dec 30 2012 8:54 am
Subject: Re: Common Lisp style question

I like the second style for pretty much the same reasons.

Also, I believe the first is not "imperative style" and perfectly fine as I
remember either Sussman or Abelson clearly stating in the SICP videos that
the intial binding in a let is initialization and not the same as assigning
to a variable already created.


 
You must Sign in before you can post messages.
To post a message you must first join this group.
Please update your nickname on the subscription settings page before posting.
You do not have the permission required to post.
Nicholas Patrick  
View profile  
 More options Jan 2, 8:38 am
From: Nicholas Patrick <npatric...@gmail.com>
Date: Wed, 2 Jan 2013 07:38:58 -0600
Local: Wed, Jan 2 2013 8:38 am
Subject: Re: Common Lisp style question

A solution that is somewhat in-between the two options could be to use
with-slots...which may or may not be supported by your lisp implementation
given that it's not in the spec for structures.  However, it is concise and
achieves what you're looking for.
http://www.gigamonkeys.com/book/object-reorientation-classes.html

This discussion gives an explanation of some of the issues (portability)
with using with-slots on a structure.
https://groups.google.com/forum/?fromgroups=#!msg/comp.lang.lisp/JYQZ...

On Sun, Dec 30, 2012 at 7:54 AM, Munawar Cheema
<munawar.a.che...@gmail.com>wrote:


 
You must Sign in before you can post messages.
To post a message you must first join this group.
Please update your nickname on the subscription settings page before posting.
You do not have the permission required to post.
End of messages
« Back to Discussions « Newer topic     Older topic »