Subject: Re: Does learning Lisp changes the way of your coding in other languages?
From: rpw3@rpw3.org (Rob Warnock)
Date: Wed, 18 Apr 2007 03:20:58 -0500
Newsgroups: comp.lang.lisp
Message-ID: <yqudnbocodd3SbjbnZ2dnUVZ_vXinZ2d@speakeasy.net>
lengyel@gmail.com <lengyel@gmail.com> wrote:
+---------------
| I used to count the number of 1's in the binary expansion of an
| unsigned int in C like this:
| 
| int bits(unsigned n) {
|     int i = 0;
|     while (n > 0) {
|        n &= n-1;
|        i++;
|     }
|    return i;
| }
| 
| In Lisp this became
| 
| (defun lsb (n i)
|            (cond
|              ((eq n 0) i)
|              (T (lsb (logand (- n 1) n) (+ i 1)))
|            )
|          )
| 
| (defun bits (n) (lsb n 0))
+---------------

Which, after learning a little more CL, should have
resulted in just using CL's builtin LOGCOUNT instead...  ;-}  ;-}


-Rob

-----
Rob Warnock			<rpw3@rpw3.org>
627 26th Avenue			<URL:http://rpw3.org/>
San Mateo, CA 94403		(650)572-2607