<?xml version="1.0" encoding="UTF-8"?><!-- generator="wordpress/2.0.5" -->
<rss version="2.0" 
	xmlns:content="http://purl.org/rss/1.0/modules/content/">
<channel>
	<title>Comments on: Lisp: Parse and Aggregate a CSV File</title>
	<link>http://www.terminally-incoherent.com/blog/2007/12/22/lisp-parse-and-aggregate-a-csv-file/</link>
	<description>Utterly random, incoherent and disjointed rants and ramblings...</description>
	<pubDate>Sat, 10 Jan 2009 01:41:41 +0000</pubDate>
	<generator>http://wordpress.org/?v=2.0.5</generator>

	<item>
		<title>by: Lisp: Parse and Aggregate a CSV File [ Terminally Incoherent ]</title>
		<link>http://www.terminally-incoherent.com/blog/2007/12/22/lisp-parse-and-aggregate-a-csv-file/#comment-9280</link>
		<pubDate>Sun, 08 Jun 2008 09:22:34 +0000</pubDate>
		<guid>http://www.terminally-incoherent.com/blog/2007/12/22/lisp-parse-and-aggregate-a-csv-file/#comment-9280</guid>
					<description>[...] December 22nd 2007 4:58pm  [-] From: terminally-incoherent.com  Related? [...]</description>
		<content:encoded><![CDATA[<p>[&#8230;] December 22nd 2007 4:58pm  [-] From: terminally-incoherent.com  Related? [&#8230;]
</p>
]]></content:encoded>
				</item>
	<item>
		<title>by: Jaba</title>
		<link>http://www.terminally-incoherent.com/blog/2007/12/22/lisp-parse-and-aggregate-a-csv-file/#comment-7463</link>
		<pubDate>Mon, 24 Dec 2007 09:52:57 +0000</pubDate>
		<guid>http://www.terminally-incoherent.com/blog/2007/12/22/lisp-parse-and-aggregate-a-csv-file/#comment-7463</guid>
					<description>:D intresting, I'll try vim so, thanks!</description>
		<content:encoded><![CDATA[<p>:D intresting, I&#8217;ll try vim so, thanks!
</p>
]]></content:encoded>
				</item>
	<item>
		<title>by: Luke Maciak</title>
		<link>http://www.terminally-incoherent.com/blog/2007/12/22/lisp-parse-and-aggregate-a-csv-file/#comment-7462</link>
		<pubDate>Mon, 24 Dec 2007 08:56:33 +0000</pubDate>
		<guid>http://www.terminally-incoherent.com/blog/2007/12/22/lisp-parse-and-aggregate-a-csv-file/#comment-7462</guid>
					<description>Ah! Let* - that's what I was looking for. Funny thing - I completely forgot that it exists. Thanks!

Re: editor - Vim all the way! I run it as my default text editor in every single OS. ;)</description>
		<content:encoded><![CDATA[<p>Ah! Let* - that&#8217;s what I was looking for. Funny thing - I completely forgot that it exists. Thanks!</p>
<p>Re: editor - Vim all the way! I run it as my default text editor in every single OS. <img src="http://www.terminally-incoherent.com/blog/wp-includes/images/smilies/icon_wink.gif" alt=")" class="wp-smiley" />
</p>
]]></content:encoded>
				</item>
	<item>
		<title>by: Jaba</title>
		<link>http://www.terminally-incoherent.com/blog/2007/12/22/lisp-parse-and-aggregate-a-csv-file/#comment-7461</link>
		<pubDate>Mon, 24 Dec 2007 08:46:08 +0000</pubDate>
		<guid>http://www.terminally-incoherent.com/blog/2007/12/22/lisp-parse-and-aggregate-a-csv-file/#comment-7461</guid>
					<description>I still haven't tried your code (I'm packing for holydays, I've a fly for this evening!), but as requested I tried to make it "a little lispier" without changing what you did. It's just a taste matter, in this case, but the result is really good. Some problems, you simply have to resolve in simil-imperative way. But one thing I really love of lisp is that you CAN do anything, even imperative, you just don't HAVE to. Ever heard of CLOS, the Common Lisp Object System? :D
http://cl-cookbook.sourceforge.net/clos-tutorial/index.html
Yes, in lisp you CAN go object oriented (and LOOK AT THE TUTORIAL, it's soooo simple...), but you don't have to. Usually, you just need some struct, that comes with free get/set functions :D
Just try it, if you wish, and let me know ;) have fun!

-J


&lt;pre lang="lisp"&gt;

  ;;; CVS PARSING FUNCTION - EXECUTABLE FILE  ; tree ";" for big comments, two for full-line comments, one for in-line comments... just conventions, as *global-variable*...
(load "split-sequence.lisp")
(setf aggregate nil) ; setf is setq when needed, but it's more flexible in general
 (parse-csv)	; this way you call a normal function, that can be reused
 
 ;; I formatted it a little more lispy, as teached in "Common Lisp - A gentle introduction to symbolic computation"
 ;; It's esasier with spaces than with tabs. Suggested base tabulation is of 2 spaces
(defun parse-csv (&#38;optional (filename (first *args*)))	; &#38;optional says: if you give parse-csv an argument, it's the filename; otherwise, get it from (first *args)
  "Parses a CSV file and DOES THINGS"  ; in-line documentation. Try (documentation 'parse-csv 'function) :D
  (with-open-file (stream filename) 
    (do ((line (read-line stream nil) (read-line stream nil))) 
        ((null line) aggregate) ; returns the aggregate value in output - no need to print it
	    (let* (		; let* is equivalent to let but bounds its arguments one at a time
		    (tmp    (split-sequence:SPLIT-SEQUENCE #\, line )) 	; this can still be made in LET*
		    (key    (intern (first tmp)))
		    (newval (parse-integer (car (last tmp)) :junk-allowed t)) ; CAR is lispy :P
		    (val    (cdr (assoc key aggregate))) ) ; this can stay in LET* too
          (if val   (rplacd (assoc key aggregate) (+ newval val))
	   		        (setq aggregate (acons key newval aggregate))))))


&lt;/pre&gt;

ps I code under linux with clisp (sometimes cmucl), simply using kwrite as editor (no time to learn emacs still... and gedit doesn't have lisp highlighting). Under windows, the best editor i ever used is NOTEPAD++ (google it, you won't regret it)</description>
		<content:encoded><![CDATA[<p>I still haven&#8217;t tried your code (I&#8217;m packing for holydays, I&#8217;ve a fly for this evening!), but as requested I tried to make it &#8220;a little lispier&#8221; without changing what you did. It&#8217;s just a taste matter, in this case, but the result is really good. Some problems, you simply have to resolve in simil-imperative way. But one thing I really love of lisp is that you CAN do anything, even imperative, you just don&#8217;t HAVE to. Ever heard of CLOS, the Common Lisp Object System? <img src="http://www.terminally-incoherent.com/blog/wp-includes/images/smilies/icon_biggrin.gif" alt="D" class="wp-smiley" /><br />
<a href="http://cl-cookbook.sourceforge.net/clos-tutorial/index.html" rel="nofollow">http://cl-cookbook.sourceforge.net/clos-tutorial/index.html</a><br />
Yes, in lisp you CAN go object oriented (and LOOK AT THE TUTORIAL, it&#8217;s soooo simple&#8230;), but you don&#8217;t have to. Usually, you just need some struct, that comes with free get/set functions <img src="http://www.terminally-incoherent.com/blog/wp-includes/images/smilies/icon_biggrin.gif" alt="D" class="wp-smiley" /><br />
Just try it, if you wish, and let me know <img src="http://www.terminally-incoherent.com/blog/wp-includes/images/smilies/icon_wink.gif" alt=")" class="wp-smiley" />  have fun!</p>
<p>-J</p>

<div class="wp_syntax"><div class="code"><pre class="lisp">&nbsp;
  <span style="color: #808080; font-style: italic;">;;; CVS PARSING FUNCTION - EXECUTABLE FILE  ; tree &quot;;&quot; for big comments, two for full-line comments, one for in-line comments... just conventions, as *global-variable*...</span>
<span style="color: #66cc66;">&#40;</span>load <span style="color: #ff0000;">&quot;split-sequence.lisp&quot;</span><span style="color: #66cc66;">&#41;</span>
<span style="color: #66cc66;">&#40;</span><span style="color: #b1b100;">setf</span> aggregate <span style="color: #b1b100;">nil</span><span style="color: #66cc66;">&#41;</span> <span style="color: #808080; font-style: italic;">; setf is setq when needed, but it's more flexible in general</span>
 <span style="color: #66cc66;">&#40;</span>parse-csv<span style="color: #66cc66;">&#41;</span>	<span style="color: #808080; font-style: italic;">; this way you call a normal function, that can be reused</span>
&nbsp;
 <span style="color: #808080; font-style: italic;">;; I formatted it a little more lispy, as teached in &quot;Common Lisp - A gentle introduction to symbolic computation&quot;</span>
 <span style="color: #808080; font-style: italic;">;; It's esasier with spaces than with tabs. Suggested base tabulation is of 2 spaces</span>
<span style="color: #66cc66;">&#40;</span><span style="color: #b1b100;">defun</span> parse-csv <span style="color: #66cc66;">&#40;</span>&amp;amp<span style="color: #808080; font-style: italic;">;optional (filename (first *args*)))	; &amp;amp;optional says: if you give parse-csv an argument, it's the filename; otherwise, get it from (first *args)</span>
  <span style="color: #ff0000;">&quot;Parses a CSV file and DOES THINGS&quot;</span>  <span style="color: #808080; font-style: italic;">; in-line documentation. Try (documentation 'parse-csv 'function) :D</span>
  <span style="color: #66cc66;">&#40;</span>with-open-file <span style="color: #66cc66;">&#40;</span>stream filename<span style="color: #66cc66;">&#41;</span> 
    <span style="color: #66cc66;">&#40;</span><span style="color: #b1b100;">do</span> <span style="color: #66cc66;">&#40;</span><span style="color: #66cc66;">&#40;</span>line <span style="color: #66cc66;">&#40;</span>read-line stream <span style="color: #b1b100;">nil</span><span style="color: #66cc66;">&#41;</span> <span style="color: #66cc66;">&#40;</span>read-line stream <span style="color: #b1b100;">nil</span><span style="color: #66cc66;">&#41;</span><span style="color: #66cc66;">&#41;</span><span style="color: #66cc66;">&#41;</span> 
        <span style="color: #66cc66;">&#40;</span><span style="color: #66cc66;">&#40;</span><span style="color: #b1b100;">null</span> line<span style="color: #66cc66;">&#41;</span> aggregate<span style="color: #66cc66;">&#41;</span> <span style="color: #808080; font-style: italic;">; returns the aggregate value in output - no need to print it</span>
	    <span style="color: #66cc66;">&#40;</span><span style="color: #b1b100;">let</span>* <span style="color: #66cc66;">&#40;</span>		<span style="color: #808080; font-style: italic;">; let* is equivalent to let but bounds its arguments one at a time</span>
		    <span style="color: #66cc66;">&#40;</span>tmp    <span style="color: #66cc66;">&#40;</span>split-sequence:<span style="color: #555;">SPLIT</span>-SEQUENCE #\, line <span style="color: #66cc66;">&#41;</span><span style="color: #66cc66;">&#41;</span> 	<span style="color: #808080; font-style: italic;">; this can still be made in LET*</span>
		    <span style="color: #66cc66;">&#40;</span>key    <span style="color: #66cc66;">&#40;</span><span style="color: #b1b100;">intern</span> <span style="color: #66cc66;">&#40;</span>first tmp<span style="color: #66cc66;">&#41;</span><span style="color: #66cc66;">&#41;</span><span style="color: #66cc66;">&#41;</span>
		    <span style="color: #66cc66;">&#40;</span>newval <span style="color: #66cc66;">&#40;</span>parse-<span style="color: #b1b100;">integer</span> <span style="color: #66cc66;">&#40;</span><span style="color: #b1b100;">car</span> <span style="color: #66cc66;">&#40;</span><span style="color: #b1b100;">last</span> tmp<span style="color: #66cc66;">&#41;</span><span style="color: #66cc66;">&#41;</span> :<span style="color: #555;">junk</span>-allowed t<span style="color: #66cc66;">&#41;</span><span style="color: #66cc66;">&#41;</span> <span style="color: #808080; font-style: italic;">; CAR is lispy :P</span>
		    <span style="color: #66cc66;">&#40;</span>val    <span style="color: #66cc66;">&#40;</span><span style="color: #b1b100;">cdr</span> <span style="color: #66cc66;">&#40;</span><span style="color: #b1b100;">assoc</span> key aggregate<span style="color: #66cc66;">&#41;</span><span style="color: #66cc66;">&#41;</span><span style="color: #66cc66;">&#41;</span> <span style="color: #66cc66;">&#41;</span> <span style="color: #808080; font-style: italic;">; this can stay in LET* too</span>
          <span style="color: #66cc66;">&#40;</span><span style="color: #b1b100;">if</span> val   <span style="color: #66cc66;">&#40;</span><span style="color: #b1b100;">rplacd</span> <span style="color: #66cc66;">&#40;</span><span style="color: #b1b100;">assoc</span> key aggregate<span style="color: #66cc66;">&#41;</span> <span style="color: #66cc66;">&#40;</span>+ newval val<span style="color: #66cc66;">&#41;</span><span style="color: #66cc66;">&#41;</span>
	   		        <span style="color: #66cc66;">&#40;</span><span style="color: #b1b100;">setq</span> aggregate <span style="color: #66cc66;">&#40;</span>acons key newval aggregate<span style="color: #66cc66;">&#41;</span><span style="color: #66cc66;">&#41;</span><span style="color: #66cc66;">&#41;</span><span style="color: #66cc66;">&#41;</span><span style="color: #66cc66;">&#41;</span><span style="color: #66cc66;">&#41;</span></pre></div></div>

<p>ps I code under linux with clisp (sometimes cmucl), simply using kwrite as editor (no time to learn emacs still&#8230; and gedit doesn&#8217;t have lisp highlighting). Under windows, the best editor i ever used is NOTEPAD++ (google it, you won&#8217;t regret it)
</p>
]]></content:encoded>
				</item>
	<item>
		<title>by: Luke Maciak</title>
		<link>http://www.terminally-incoherent.com/blog/2007/12/22/lisp-parse-and-aggregate-a-csv-file/#comment-7460</link>
		<pubDate>Mon, 24 Dec 2007 04:14:38 +0000</pubDate>
		<guid>http://www.terminally-incoherent.com/blog/2007/12/22/lisp-parse-and-aggregate-a-csv-file/#comment-7460</guid>
					<description>Here is a cleaner version with let... Still trying to figure out how to make it more lispy:

&lt;pre lang="lisp"&gt;;;; get split-sequence from http://www.cliki.net/SPLIT-SEQUENCE
(load "split-sequence.lisp")
(setq aggregate '()) ;; the association list to store results

(with-open-file (stream (first *args*)) ;; open the file specified on cli
    (do ((line (read-line stream nil) 	;;; initialize with read-line
               (read-line stream nil))) ;;; step is read-line
        ((null line)) 			;;; termination condition
	
	;; split the output string on a comma using the split-sequence
	(setq tmp (split-sequence:SPLIT-SEQUENCE #\, line ) )

	(let (
		(key 	(intern (first tmp))					) 	;; our key
		(newval (parse-integer (first (last tmp)) :junk-allowed t)	)	;; convert the last value to an int
	     )
		   (setq val 	(cdr (assoc key aggregate))	)	;; check if key is in the assoc; if it's not, VAL will be NIL
		    
		   (if val
	  		(rplacd (assoc key aggregate) (+ newval val)) 	;; if exist update old entry
	   		(setq aggregate (acons key newval aggregate))	;; else add a new entry
		)
	)
    )
    (print aggregate)
)&lt;/pre&gt;</description>
		<content:encoded><![CDATA[<p>Here is a cleaner version with let&#8230; Still trying to figure out how to make it more lispy:</p>

<div class="wp_syntax"><div class="code"><pre class="lisp"><span style="color: #808080; font-style: italic;">;;; get split-sequence from http://www.cliki.net/SPLIT-SEQUENCE</span>
<span style="color: #66cc66;">&#40;</span>load <span style="color: #ff0000;">&quot;split-sequence.lisp&quot;</span><span style="color: #66cc66;">&#41;</span>
<span style="color: #66cc66;">&#40;</span><span style="color: #b1b100;">setq</span> aggregate '<span style="color: #66cc66;">&#40;</span><span style="color: #66cc66;">&#41;</span><span style="color: #66cc66;">&#41;</span> <span style="color: #808080; font-style: italic;">;; the association list to store results</span>
&nbsp;
<span style="color: #66cc66;">&#40;</span>with-open-file <span style="color: #66cc66;">&#40;</span>stream <span style="color: #66cc66;">&#40;</span>first *args*<span style="color: #66cc66;">&#41;</span><span style="color: #66cc66;">&#41;</span> <span style="color: #808080; font-style: italic;">;; open the file specified on cli</span>
    <span style="color: #66cc66;">&#40;</span><span style="color: #b1b100;">do</span> <span style="color: #66cc66;">&#40;</span><span style="color: #66cc66;">&#40;</span>line <span style="color: #66cc66;">&#40;</span>read-line stream <span style="color: #b1b100;">nil</span><span style="color: #66cc66;">&#41;</span> 	<span style="color: #808080; font-style: italic;">;;; initialize with read-line</span>
               <span style="color: #66cc66;">&#40;</span>read-line stream <span style="color: #b1b100;">nil</span><span style="color: #66cc66;">&#41;</span><span style="color: #66cc66;">&#41;</span><span style="color: #66cc66;">&#41;</span> <span style="color: #808080; font-style: italic;">;;; step is read-line</span>
        <span style="color: #66cc66;">&#40;</span><span style="color: #66cc66;">&#40;</span><span style="color: #b1b100;">null</span> line<span style="color: #66cc66;">&#41;</span><span style="color: #66cc66;">&#41;</span> 			<span style="color: #808080; font-style: italic;">;;; termination condition</span>
&nbsp;
	<span style="color: #808080; font-style: italic;">;; split the output string on a comma using the split-sequence</span>
	<span style="color: #66cc66;">&#40;</span><span style="color: #b1b100;">setq</span> tmp <span style="color: #66cc66;">&#40;</span>split-sequence:<span style="color: #555;">SPLIT</span>-SEQUENCE #\, line <span style="color: #66cc66;">&#41;</span> <span style="color: #66cc66;">&#41;</span>
&nbsp;
	<span style="color: #66cc66;">&#40;</span><span style="color: #b1b100;">let</span> <span style="color: #66cc66;">&#40;</span>
		<span style="color: #66cc66;">&#40;</span>key 	<span style="color: #66cc66;">&#40;</span><span style="color: #b1b100;">intern</span> <span style="color: #66cc66;">&#40;</span>first tmp<span style="color: #66cc66;">&#41;</span><span style="color: #66cc66;">&#41;</span>					<span style="color: #66cc66;">&#41;</span> 	<span style="color: #808080; font-style: italic;">;; our key</span>
		<span style="color: #66cc66;">&#40;</span>newval <span style="color: #66cc66;">&#40;</span>parse-<span style="color: #b1b100;">integer</span> <span style="color: #66cc66;">&#40;</span>first <span style="color: #66cc66;">&#40;</span><span style="color: #b1b100;">last</span> tmp<span style="color: #66cc66;">&#41;</span><span style="color: #66cc66;">&#41;</span> :<span style="color: #555;">junk</span>-allowed t<span style="color: #66cc66;">&#41;</span>	<span style="color: #66cc66;">&#41;</span>	<span style="color: #808080; font-style: italic;">;; convert the last value to an int</span>
	     <span style="color: #66cc66;">&#41;</span>
		   <span style="color: #66cc66;">&#40;</span><span style="color: #b1b100;">setq</span> val 	<span style="color: #66cc66;">&#40;</span><span style="color: #b1b100;">cdr</span> <span style="color: #66cc66;">&#40;</span><span style="color: #b1b100;">assoc</span> key aggregate<span style="color: #66cc66;">&#41;</span><span style="color: #66cc66;">&#41;</span>	<span style="color: #66cc66;">&#41;</span>	<span style="color: #808080; font-style: italic;">;; check if key is in the assoc; if it's not, VAL will be NIL</span>
&nbsp;
		   <span style="color: #66cc66;">&#40;</span><span style="color: #b1b100;">if</span> val
	  		<span style="color: #66cc66;">&#40;</span><span style="color: #b1b100;">rplacd</span> <span style="color: #66cc66;">&#40;</span><span style="color: #b1b100;">assoc</span> key aggregate<span style="color: #66cc66;">&#41;</span> <span style="color: #66cc66;">&#40;</span>+ newval val<span style="color: #66cc66;">&#41;</span><span style="color: #66cc66;">&#41;</span> 	<span style="color: #808080; font-style: italic;">;; if exist update old entry</span>
	   		<span style="color: #66cc66;">&#40;</span><span style="color: #b1b100;">setq</span> aggregate <span style="color: #66cc66;">&#40;</span>acons key newval aggregate<span style="color: #66cc66;">&#41;</span><span style="color: #66cc66;">&#41;</span>	<span style="color: #808080; font-style: italic;">;; else add a new entry</span>
		<span style="color: #66cc66;">&#41;</span>
	<span style="color: #66cc66;">&#41;</span>
    <span style="color: #66cc66;">&#41;</span>
    <span style="color: #66cc66;">&#40;</span>print aggregate<span style="color: #66cc66;">&#41;</span>
<span style="color: #66cc66;">&#41;</span></pre></div></div>

]]></content:encoded>
				</item>
	<item>
		<title>by: Luke Maciak</title>
		<link>http://www.terminally-incoherent.com/blog/2007/12/22/lisp-parse-and-aggregate-a-csv-file/#comment-7459</link>
		<pubDate>Mon, 24 Dec 2007 03:56:25 +0000</pubDate>
		<guid>http://www.terminally-incoherent.com/blog/2007/12/22/lisp-parse-and-aggregate-a-csv-file/#comment-7459</guid>
					<description>I fixed your entries. You missed a &#60;/pre&#62; tag here and there. And yeah, the preview button is there for times like this. But no worries. 

Thanks for the code samples! I wish the comments were in English :P</description>
		<content:encoded><![CDATA[<p>I fixed your entries. You missed a &lt;/pre&gt; tag here and there. And yeah, the preview button is there for times like this. But no worries. </p>
<p>Thanks for the code samples! I wish the comments were in English <img src="http://www.terminally-incoherent.com/blog/wp-includes/images/smilies/icon_razz.gif" alt="P" class="wp-smiley" />
</p>
]]></content:encoded>
				</item>
	<item>
		<title>by: Jaba</title>
		<link>http://www.terminally-incoherent.com/blog/2007/12/22/lisp-parse-and-aggregate-a-csv-file/#comment-7458</link>
		<pubDate>Sun, 23 Dec 2007 22:48:06 +0000</pubDate>
		<guid>http://www.terminally-incoherent.com/blog/2007/12/22/lisp-parse-and-aggregate-a-csv-file/#comment-7458</guid>
					<description>(sorry, never posted before on wordpress, I'm seeing only now the "preview" button... this will never occour again, please edit my previous entry, thank you)

You're too polite not to try it immediatly. I'm pasting a lisp implementation of a simple local search algorithm, that tries to find a number with as many "ones" as possible in his binary representation, with solution space 0 to n. Sorry for the documentation in Italian :P

PSEUDO-CODE:

&lt;pre lang="c"&gt;init(start);
i:=istart;
repeat
  generate (j in Nh(i))
  if fit(j) &#62;= fit(i) then i:=j;
until f(j)&lt;/pre&gt;

&lt;pre lang="lisp"&gt;

(defun dec2bin (n)  ; ci serve per ottenere la rappresentazione binaria del max
  "Restituisce la rappresentazione binaria di N, come lista di bit"
  (dec2bin-acc n nil))

(defun dec2bin-acc (n acc)
  "Versione di dec2bin con accumulatore per tail recursion"
  (if (= (length lst) n) lst
    (expand-bin (cons 0 lst) n)))

(defun fitness (n)
  "Restituisce la fitness di un numero"
  (length (remove-if #'zerop n)) )

(defun generate (i top) ;; a partire da questa versione, top è un numero decimale
  "Restituisce il primo tra i migliori vicini di I, stando attento a non sforare TOP"
  (let ((pos (position 0 i :from-end t)))
    (if (null pos) i
      (let ((copy (copy-list i)))
        (setf (nth pos copy) 1)
        (if (= (fitness i) (fitness j)) )

(defun best (lst)
  "Data una lista di codifiche binarie, restituisce quella con fitness migliore"
  (car (sort lst #'best-fit-check)) )

(defun local-search (n)
  "Esegue una ricerca locale con spazio delle soluzioni tra 0 e N"
  (do* ((i     (init n)     (best (list i j)))
        (j     (generate i n) (generate i n))
        (i-dec (bin2dec i)  (bin2dec i))
        (fi    (fitness i)  (fitness i))         ;NOTA: solo per leggibilità, per ottimizzazione rimuoverle
        (fj    (fitness j)  (fitness j)))
       ((or (equal i j) (&#62; fi fj))  ; ecco che vado ad assicurarmi dal loop sul massimo
        (format t #&#124;"~&#38;FINE:&#124;#"~&#38;Soluzione finale:  ~D = ~A - Fitness: ~D" i-dec i fi)
         i-dec)
       (format t "~&#38;Passaggio attuale: ~D = ~A - Fitness: ~D" i-dec i fi) ))
&lt;/pre&gt;

with much care on training on the "tail recursion" technic, that can generate code as fast as fortran does (look at "performance" on CLISP homepage)

Hope this can help, I spent some months to gain functional mentality - and it's still work in progress.

Have fun with lisp

-J</description>
		<content:encoded><![CDATA[<p>(sorry, never posted before on wordpress, I&#8217;m seeing only now the &#8220;preview&#8221; button&#8230; this will never occour again, please edit my previous entry, thank you)</p>
<p>You&#8217;re too polite not to try it immediatly. I&#8217;m pasting a lisp implementation of a simple local search algorithm, that tries to find a number with as many &#8220;ones&#8221; as possible in his binary representation, with solution space 0 to n. Sorry for the documentation in Italian <img src="http://www.terminally-incoherent.com/blog/wp-includes/images/smilies/icon_razz.gif" alt="P" class="wp-smiley" /> </p>
<p>PSEUDO-CODE:</p>

<div class="wp_syntax"><div class="code"><pre class="c">init<span style="color: #66cc66;">&#40;</span>start<span style="color: #66cc66;">&#41;</span>;
i:=istart;
repeat
  generate <span style="color: #66cc66;">&#40;</span>j in Nh<span style="color: #66cc66;">&#40;</span>i<span style="color: #66cc66;">&#41;</span><span style="color: #66cc66;">&#41;</span>
  <span style="color: #b1b100;">if</span> fit<span style="color: #66cc66;">&#40;</span>j<span style="color: #66cc66;">&#41;</span> &amp;gt;= fit<span style="color: #66cc66;">&#40;</span>i<span style="color: #66cc66;">&#41;</span> then i:=j;
until f<span style="color: #66cc66;">&#40;</span>j<span style="color: #66cc66;">&#41;</span></pre></div></div>


<div class="wp_syntax"><div class="code"><pre class="lisp">&nbsp;
<span style="color: #66cc66;">&#40;</span><span style="color: #b1b100;">defun</span> dec2bin <span style="color: #66cc66;">&#40;</span>n<span style="color: #66cc66;">&#41;</span>  <span style="color: #808080; font-style: italic;">; ci serve per ottenere la rappresentazione binaria del max</span>
  <span style="color: #ff0000;">&quot;Restituisce la rappresentazione binaria di N, come lista di bit&quot;</span>
  <span style="color: #66cc66;">&#40;</span>dec2bin-acc n <span style="color: #b1b100;">nil</span><span style="color: #66cc66;">&#41;</span><span style="color: #66cc66;">&#41;</span>
&nbsp;
<span style="color: #66cc66;">&#40;</span><span style="color: #b1b100;">defun</span> dec2bin-acc <span style="color: #66cc66;">&#40;</span>n acc<span style="color: #66cc66;">&#41;</span>
  <span style="color: #ff0000;">&quot;Versione di dec2bin con accumulatore per tail recursion&quot;</span>
  <span style="color: #66cc66;">&#40;</span><span style="color: #b1b100;">if</span> <span style="color: #66cc66;">&#40;</span>= <span style="color: #66cc66;">&#40;</span><span style="color: #b1b100;">length</span> lst<span style="color: #66cc66;">&#41;</span> n<span style="color: #66cc66;">&#41;</span> lst
    <span style="color: #66cc66;">&#40;</span>expand-bin <span style="color: #66cc66;">&#40;</span><span style="color: #b1b100;">cons</span> <span style="color: #cc66cc;">0</span> lst<span style="color: #66cc66;">&#41;</span> n<span style="color: #66cc66;">&#41;</span><span style="color: #66cc66;">&#41;</span><span style="color: #66cc66;">&#41;</span>
&nbsp;
<span style="color: #66cc66;">&#40;</span><span style="color: #b1b100;">defun</span> fitness <span style="color: #66cc66;">&#40;</span>n<span style="color: #66cc66;">&#41;</span>
  <span style="color: #ff0000;">&quot;Restituisce la fitness di un numero&quot;</span>
  <span style="color: #66cc66;">&#40;</span><span style="color: #b1b100;">length</span> <span style="color: #66cc66;">&#40;</span>remove-<span style="color: #b1b100;">if</span> #'<span style="color: #b1b100;">zerop</span> n<span style="color: #66cc66;">&#41;</span><span style="color: #66cc66;">&#41;</span> <span style="color: #66cc66;">&#41;</span>
&nbsp;
<span style="color: #66cc66;">&#40;</span><span style="color: #b1b100;">defun</span> generate <span style="color: #66cc66;">&#40;</span>i top<span style="color: #66cc66;">&#41;</span> <span style="color: #808080; font-style: italic;">;; a partire da questa versione, top è un numero decimale</span>
  <span style="color: #ff0000;">&quot;Restituisce il primo tra i migliori vicini di I, stando attento a non sforare TOP&quot;</span>
  <span style="color: #66cc66;">&#40;</span><span style="color: #b1b100;">let</span> <span style="color: #66cc66;">&#40;</span><span style="color: #66cc66;">&#40;</span>pos <span style="color: #66cc66;">&#40;</span>position <span style="color: #cc66cc;">0</span> i :<span style="color: #555;">from</span>-end t<span style="color: #66cc66;">&#41;</span><span style="color: #66cc66;">&#41;</span><span style="color: #66cc66;">&#41;</span>
    <span style="color: #66cc66;">&#40;</span><span style="color: #b1b100;">if</span> <span style="color: #66cc66;">&#40;</span><span style="color: #b1b100;">null</span> pos<span style="color: #66cc66;">&#41;</span> i
      <span style="color: #66cc66;">&#40;</span><span style="color: #b1b100;">let</span> <span style="color: #66cc66;">&#40;</span><span style="color: #66cc66;">&#40;</span>copy <span style="color: #66cc66;">&#40;</span>copy-<span style="color: #b1b100;">list</span> i<span style="color: #66cc66;">&#41;</span><span style="color: #66cc66;">&#41;</span><span style="color: #66cc66;">&#41;</span>
        <span style="color: #66cc66;">&#40;</span><span style="color: #b1b100;">setf</span> <span style="color: #66cc66;">&#40;</span><span style="color: #b1b100;">nth</span> pos copy<span style="color: #66cc66;">&#41;</span> <span style="color: #cc66cc;">1</span><span style="color: #66cc66;">&#41;</span>
        <span style="color: #66cc66;">&#40;</span><span style="color: #b1b100;">if</span> <span style="color: #66cc66;">&#40;</span>= <span style="color: #66cc66;">&#40;</span>fitness i<span style="color: #66cc66;">&#41;</span> <span style="color: #66cc66;">&#40;</span>fitness j<span style="color: #66cc66;">&#41;</span><span style="color: #66cc66;">&#41;</span> <span style="color: #66cc66;">&#41;</span>
&nbsp;
<span style="color: #66cc66;">&#40;</span><span style="color: #b1b100;">defun</span> best <span style="color: #66cc66;">&#40;</span>lst<span style="color: #66cc66;">&#41;</span>
  <span style="color: #ff0000;">&quot;Data una lista di codifiche binarie, restituisce quella con fitness migliore&quot;</span>
  <span style="color: #66cc66;">&#40;</span><span style="color: #b1b100;">car</span> <span style="color: #66cc66;">&#40;</span>sort lst #'best-fit-check<span style="color: #66cc66;">&#41;</span><span style="color: #66cc66;">&#41;</span> <span style="color: #66cc66;">&#41;</span>
&nbsp;
<span style="color: #66cc66;">&#40;</span><span style="color: #b1b100;">defun</span> local-search <span style="color: #66cc66;">&#40;</span>n<span style="color: #66cc66;">&#41;</span>
  <span style="color: #ff0000;">&quot;Esegue una ricerca locale con spazio delle soluzioni tra 0 e N&quot;</span>
  <span style="color: #66cc66;">&#40;</span><span style="color: #b1b100;">do</span>* <span style="color: #66cc66;">&#40;</span><span style="color: #66cc66;">&#40;</span>i     <span style="color: #66cc66;">&#40;</span>init n<span style="color: #66cc66;">&#41;</span>     <span style="color: #66cc66;">&#40;</span>best <span style="color: #66cc66;">&#40;</span><span style="color: #b1b100;">list</span> i j<span style="color: #66cc66;">&#41;</span><span style="color: #66cc66;">&#41;</span><span style="color: #66cc66;">&#41;</span>
        <span style="color: #66cc66;">&#40;</span>j     <span style="color: #66cc66;">&#40;</span>generate i n<span style="color: #66cc66;">&#41;</span> <span style="color: #66cc66;">&#40;</span>generate i n<span style="color: #66cc66;">&#41;</span><span style="color: #66cc66;">&#41;</span>
        <span style="color: #66cc66;">&#40;</span>i-dec <span style="color: #66cc66;">&#40;</span>bin2dec i<span style="color: #66cc66;">&#41;</span>  <span style="color: #66cc66;">&#40;</span>bin2dec i<span style="color: #66cc66;">&#41;</span><span style="color: #66cc66;">&#41;</span>
        <span style="color: #66cc66;">&#40;</span>fi    <span style="color: #66cc66;">&#40;</span>fitness i<span style="color: #66cc66;">&#41;</span>  <span style="color: #66cc66;">&#40;</span>fitness i<span style="color: #66cc66;">&#41;</span><span style="color: #66cc66;">&#41;</span>         <span style="color: #808080; font-style: italic;">;NOTA: solo per leggibilità, per ottimizzazione rimuoverle</span>
        <span style="color: #66cc66;">&#40;</span>fj    <span style="color: #66cc66;">&#40;</span>fitness j<span style="color: #66cc66;">&#41;</span>  <span style="color: #66cc66;">&#40;</span>fitness j<span style="color: #66cc66;">&#41;</span><span style="color: #66cc66;">&#41;</span><span style="color: #66cc66;">&#41;</span>
       <span style="color: #66cc66;">&#40;</span><span style="color: #66cc66;">&#40;</span><span style="color: #b1b100;">or</span> <span style="color: #66cc66;">&#40;</span><span style="color: #b1b100;">equal</span> i j<span style="color: #66cc66;">&#41;</span> <span style="color: #66cc66;">&#40;</span>&amp;gt<span style="color: #808080; font-style: italic;">; fi fj))  ; ecco che vado ad assicurarmi dal loop sul massimo</span>
        <span style="color: #66cc66;">&#40;</span>format t #|<span style="color: #ff0000;">&quot;~&amp;amp;FINE:|#&quot;</span>~&amp;amp<span style="color: #808080; font-style: italic;">;Soluzione finale:  ~D = ~A - Fitness: ~D&quot; i-dec i fi)</span>
         i-dec<span style="color: #66cc66;">&#41;</span>
       <span style="color: #66cc66;">&#40;</span>format t <span style="color: #ff0000;">&quot;~&amp;amp;Passaggio attuale: ~D = ~A - Fitness: ~D&quot;</span> i-dec i fi<span style="color: #66cc66;">&#41;</span> <span style="color: #66cc66;">&#41;</span><span style="color: #66cc66;">&#41;</span></pre></div></div>

<p>with much care on training on the &#8220;tail recursion&#8221; technic, that can generate code as fast as fortran does (look at &#8220;performance&#8221; on CLISP homepage)</p>
<p>Hope this can help, I spent some months to gain functional mentality - and it&#8217;s still work in progress.</p>
<p>Have fun with lisp</p>
<p>-J
</p>
]]></content:encoded>
				</item>
	<item>
		<title>by: Jaba</title>
		<link>http://www.terminally-incoherent.com/blog/2007/12/22/lisp-parse-and-aggregate-a-csv-file/#comment-7457</link>
		<pubDate>Sun, 23 Dec 2007 22:42:23 +0000</pubDate>
		<guid>http://www.terminally-incoherent.com/blog/2007/12/22/lisp-parse-and-aggregate-a-csv-file/#comment-7457</guid>
					<description>You're too polite not to try it immediatly. I'm pasting a lisp implementation of a simple local search algorithm, that tries to find a number with as many "ones" as possible in his binary representation, with solution space 0 to n. Sorry for the documentation in Italian :P

PSEUDO-CODE:
&lt;pre lang="c"&gt;init(start);
i:=istart;
repeat
  generate (j in Nh(i))
  if fit(j) &#62;= fit(i) then i:=j;
until f(j) &lt;/pre&gt;


&lt;pre lang="lisp"&gt;(defun dec2bin (n)  ; ci serve per ottenere la rappresentazione binaria del max
  "Restituisce la rappresentazione binaria di N, come lista di bit"
  (dec2bin-acc n nil))

(defun dec2bin-acc (n acc)
  "Versione di dec2bin con accumulatore per tail recursion"
  (if (= (length lst) n) lst
    (expand-bin (cons 0 lst) n)))

(defun fitness (n)
  "Restituisce la fitness di un numero"
  (length (remove-if #'zerop n)) )

(defun generate (i top) ;; a partire da questa versione, top è un numero decimale
  "Restituisce il primo tra i migliori vicini di I, stando attento a non sforare TOP"
  (let ((pos (position 0 i :from-end t)))
    (if (null pos) i
      (let ((copy (copy-list i)))
        (setf (nth pos copy) 1)
        (if (= (fitness i) (fitness j)) )

(defun best (lst)
  "Data una lista di codifiche binarie, restituisce quella con fitness migliore"
  (car (sort lst #'best-fit-check)) )

(defun local-search (n)
  "Esegue una ricerca locale con spazio delle soluzioni tra 0 e N"
  (do* ((i     (init n)     (best (list i j)))
        (j     (generate i n) (generate i n))
        (i-dec (bin2dec i)  (bin2dec i))
        (fi    (fitness i)  (fitness i))         ;NOTA: solo per leggibilità, per ottimizzazione rimuoverle
        (fj    (fitness j)  (fitness j)))
       ((or (equal i j) (&#62; fi fj))  ; ecco che vado ad assicurarmi dal loop sul massimo
        (format t #&#124;"~&#38;FINE:&#124;#"~&#38;Soluzione finale:  ~D = ~A - Fitness: ~D" i-dec i fi)
         i-dec)
       (format t "~&#38;Passaggio attuale: ~D = ~A - Fitness: ~D" i-dec i fi) ))
&lt;/pre&gt;

Hope this can help, I spent some months to gain functional mentality - and it's still work in progress.

Have fun with lisp

-J</description>
		<content:encoded><![CDATA[<p>You&#8217;re too polite not to try it immediatly. I&#8217;m pasting a lisp implementation of a simple local search algorithm, that tries to find a number with as many &#8220;ones&#8221; as possible in his binary representation, with solution space 0 to n. Sorry for the documentation in Italian <img src="http://www.terminally-incoherent.com/blog/wp-includes/images/smilies/icon_razz.gif" alt="P" class="wp-smiley" /> </p>
<p>PSEUDO-CODE:</p>

<div class="wp_syntax"><div class="code"><pre class="c">init<span style="color: #66cc66;">&#40;</span>start<span style="color: #66cc66;">&#41;</span>;
i:=istart;
repeat
  generate <span style="color: #66cc66;">&#40;</span>j in Nh<span style="color: #66cc66;">&#40;</span>i<span style="color: #66cc66;">&#41;</span><span style="color: #66cc66;">&#41;</span>
  <span style="color: #b1b100;">if</span> fit<span style="color: #66cc66;">&#40;</span>j<span style="color: #66cc66;">&#41;</span> &amp;gt;= fit<span style="color: #66cc66;">&#40;</span>i<span style="color: #66cc66;">&#41;</span> then i:=j;
until f<span style="color: #66cc66;">&#40;</span>j<span style="color: #66cc66;">&#41;</span></pre></div></div>


<div class="wp_syntax"><div class="code"><pre class="lisp"><span style="color: #66cc66;">&#40;</span><span style="color: #b1b100;">defun</span> dec2bin <span style="color: #66cc66;">&#40;</span>n<span style="color: #66cc66;">&#41;</span>  <span style="color: #808080; font-style: italic;">; ci serve per ottenere la rappresentazione binaria del max</span>
  <span style="color: #ff0000;">&quot;Restituisce la rappresentazione binaria di N, come lista di bit&quot;</span>
  <span style="color: #66cc66;">&#40;</span>dec2bin-acc n <span style="color: #b1b100;">nil</span><span style="color: #66cc66;">&#41;</span><span style="color: #66cc66;">&#41;</span>
&nbsp;
<span style="color: #66cc66;">&#40;</span><span style="color: #b1b100;">defun</span> dec2bin-acc <span style="color: #66cc66;">&#40;</span>n acc<span style="color: #66cc66;">&#41;</span>
  <span style="color: #ff0000;">&quot;Versione di dec2bin con accumulatore per tail recursion&quot;</span>
  <span style="color: #66cc66;">&#40;</span><span style="color: #b1b100;">if</span> <span style="color: #66cc66;">&#40;</span>= <span style="color: #66cc66;">&#40;</span><span style="color: #b1b100;">length</span> lst<span style="color: #66cc66;">&#41;</span> n<span style="color: #66cc66;">&#41;</span> lst
    <span style="color: #66cc66;">&#40;</span>expand-bin <span style="color: #66cc66;">&#40;</span><span style="color: #b1b100;">cons</span> <span style="color: #cc66cc;">0</span> lst<span style="color: #66cc66;">&#41;</span> n<span style="color: #66cc66;">&#41;</span><span style="color: #66cc66;">&#41;</span><span style="color: #66cc66;">&#41;</span>
&nbsp;
<span style="color: #66cc66;">&#40;</span><span style="color: #b1b100;">defun</span> fitness <span style="color: #66cc66;">&#40;</span>n<span style="color: #66cc66;">&#41;</span>
  <span style="color: #ff0000;">&quot;Restituisce la fitness di un numero&quot;</span>
  <span style="color: #66cc66;">&#40;</span><span style="color: #b1b100;">length</span> <span style="color: #66cc66;">&#40;</span>remove-<span style="color: #b1b100;">if</span> #'<span style="color: #b1b100;">zerop</span> n<span style="color: #66cc66;">&#41;</span><span style="color: #66cc66;">&#41;</span> <span style="color: #66cc66;">&#41;</span>
&nbsp;
<span style="color: #66cc66;">&#40;</span><span style="color: #b1b100;">defun</span> generate <span style="color: #66cc66;">&#40;</span>i top<span style="color: #66cc66;">&#41;</span> <span style="color: #808080; font-style: italic;">;; a partire da questa versione, top è un numero decimale</span>
  <span style="color: #ff0000;">&quot;Restituisce il primo tra i migliori vicini di I, stando attento a non sforare TOP&quot;</span>
  <span style="color: #66cc66;">&#40;</span><span style="color: #b1b100;">let</span> <span style="color: #66cc66;">&#40;</span><span style="color: #66cc66;">&#40;</span>pos <span style="color: #66cc66;">&#40;</span>position <span style="color: #cc66cc;">0</span> i :<span style="color: #555;">from</span>-end t<span style="color: #66cc66;">&#41;</span><span style="color: #66cc66;">&#41;</span><span style="color: #66cc66;">&#41;</span>
    <span style="color: #66cc66;">&#40;</span><span style="color: #b1b100;">if</span> <span style="color: #66cc66;">&#40;</span><span style="color: #b1b100;">null</span> pos<span style="color: #66cc66;">&#41;</span> i
      <span style="color: #66cc66;">&#40;</span><span style="color: #b1b100;">let</span> <span style="color: #66cc66;">&#40;</span><span style="color: #66cc66;">&#40;</span>copy <span style="color: #66cc66;">&#40;</span>copy-<span style="color: #b1b100;">list</span> i<span style="color: #66cc66;">&#41;</span><span style="color: #66cc66;">&#41;</span><span style="color: #66cc66;">&#41;</span>
        <span style="color: #66cc66;">&#40;</span><span style="color: #b1b100;">setf</span> <span style="color: #66cc66;">&#40;</span><span style="color: #b1b100;">nth</span> pos copy<span style="color: #66cc66;">&#41;</span> <span style="color: #cc66cc;">1</span><span style="color: #66cc66;">&#41;</span>
        <span style="color: #66cc66;">&#40;</span><span style="color: #b1b100;">if</span> <span style="color: #66cc66;">&#40;</span>= <span style="color: #66cc66;">&#40;</span>fitness i<span style="color: #66cc66;">&#41;</span> <span style="color: #66cc66;">&#40;</span>fitness j<span style="color: #66cc66;">&#41;</span><span style="color: #66cc66;">&#41;</span> <span style="color: #66cc66;">&#41;</span>
&nbsp;
<span style="color: #66cc66;">&#40;</span><span style="color: #b1b100;">defun</span> best <span style="color: #66cc66;">&#40;</span>lst<span style="color: #66cc66;">&#41;</span>
  <span style="color: #ff0000;">&quot;Data una lista di codifiche binarie, restituisce quella con fitness migliore&quot;</span>
  <span style="color: #66cc66;">&#40;</span><span style="color: #b1b100;">car</span> <span style="color: #66cc66;">&#40;</span>sort lst #'best-fit-check<span style="color: #66cc66;">&#41;</span><span style="color: #66cc66;">&#41;</span> <span style="color: #66cc66;">&#41;</span>
&nbsp;
<span style="color: #66cc66;">&#40;</span><span style="color: #b1b100;">defun</span> local-search <span style="color: #66cc66;">&#40;</span>n<span style="color: #66cc66;">&#41;</span>
  <span style="color: #ff0000;">&quot;Esegue una ricerca locale con spazio delle soluzioni tra 0 e N&quot;</span>
  <span style="color: #66cc66;">&#40;</span><span style="color: #b1b100;">do</span>* <span style="color: #66cc66;">&#40;</span><span style="color: #66cc66;">&#40;</span>i     <span style="color: #66cc66;">&#40;</span>init n<span style="color: #66cc66;">&#41;</span>     <span style="color: #66cc66;">&#40;</span>best <span style="color: #66cc66;">&#40;</span><span style="color: #b1b100;">list</span> i j<span style="color: #66cc66;">&#41;</span><span style="color: #66cc66;">&#41;</span><span style="color: #66cc66;">&#41;</span>
        <span style="color: #66cc66;">&#40;</span>j     <span style="color: #66cc66;">&#40;</span>generate i n<span style="color: #66cc66;">&#41;</span> <span style="color: #66cc66;">&#40;</span>generate i n<span style="color: #66cc66;">&#41;</span><span style="color: #66cc66;">&#41;</span>
        <span style="color: #66cc66;">&#40;</span>i-dec <span style="color: #66cc66;">&#40;</span>bin2dec i<span style="color: #66cc66;">&#41;</span>  <span style="color: #66cc66;">&#40;</span>bin2dec i<span style="color: #66cc66;">&#41;</span><span style="color: #66cc66;">&#41;</span>
        <span style="color: #66cc66;">&#40;</span>fi    <span style="color: #66cc66;">&#40;</span>fitness i<span style="color: #66cc66;">&#41;</span>  <span style="color: #66cc66;">&#40;</span>fitness i<span style="color: #66cc66;">&#41;</span><span style="color: #66cc66;">&#41;</span>         <span style="color: #808080; font-style: italic;">;NOTA: solo per leggibilità, per ottimizzazione rimuoverle</span>
        <span style="color: #66cc66;">&#40;</span>fj    <span style="color: #66cc66;">&#40;</span>fitness j<span style="color: #66cc66;">&#41;</span>  <span style="color: #66cc66;">&#40;</span>fitness j<span style="color: #66cc66;">&#41;</span><span style="color: #66cc66;">&#41;</span><span style="color: #66cc66;">&#41;</span>
       <span style="color: #66cc66;">&#40;</span><span style="color: #66cc66;">&#40;</span><span style="color: #b1b100;">or</span> <span style="color: #66cc66;">&#40;</span><span style="color: #b1b100;">equal</span> i j<span style="color: #66cc66;">&#41;</span> <span style="color: #66cc66;">&#40;</span>&amp;gt<span style="color: #808080; font-style: italic;">; fi fj))  ; ecco che vado ad assicurarmi dal loop sul massimo</span>
        <span style="color: #66cc66;">&#40;</span>format t #|<span style="color: #ff0000;">&quot;~&amp;amp;FINE:|#&quot;</span>~&amp;amp<span style="color: #808080; font-style: italic;">;Soluzione finale:  ~D = ~A - Fitness: ~D&quot; i-dec i fi)</span>
         i-dec<span style="color: #66cc66;">&#41;</span>
       <span style="color: #66cc66;">&#40;</span>format t <span style="color: #ff0000;">&quot;~&amp;amp;Passaggio attuale: ~D = ~A - Fitness: ~D&quot;</span> i-dec i fi<span style="color: #66cc66;">&#41;</span> <span style="color: #66cc66;">&#41;</span><span style="color: #66cc66;">&#41;</span></pre></div></div>

<p>Hope this can help, I spent some months to gain functional mentality - and it&#8217;s still work in progress.</p>
<p>Have fun with lisp</p>
<p>-J
</p>
]]></content:encoded>
				</item>
	<item>
		<title>by: Jaba</title>
		<link>http://www.terminally-incoherent.com/blog/2007/12/22/lisp-parse-and-aggregate-a-csv-file/#comment-7456</link>
		<pubDate>Sun, 23 Dec 2007 22:40:58 +0000</pubDate>
		<guid>http://www.terminally-incoherent.com/blog/2007/12/22/lisp-parse-and-aggregate-a-csv-file/#comment-7456</guid>
					<description>You're too polite not to try it immediatly. I'm pasting a lisp implementation of a simple local search algorithm, that tries to find a number with as many "ones" as possible in his binary representation, with solution space 0 to n. Sorry for the documentation in Italian :P

PSEUDO-CODE:
&lt;pre lang="c"&gt;
init(start);
i:=istart;
repeat
  generate (j in Nh(i))
  if fit(j) &#62;= fit(i) then i:=j;
until f(j)&lt;/pre&gt;

WORKING LISP IMPLEMENTATION:
with much care on training on the "tail recursion" technic, that can generate code as fast as fortran does (look at "performance" on CLISP homepage)
&lt;pre lang="lisp"&gt;
(defun dec2bin (n)  ; ci serve per ottenere la rappresentazione binaria del max
  "Restituisce la rappresentazione binaria di N, come lista di bit"
  (dec2bin-acc n nil))

(defun dec2bin-acc (n acc)
  "Versione di dec2bin con accumulatore per tail recursion"
  (if (= (length lst) n) lst
    (expand-bin (cons 0 lst) n)))

(defun fitness (n)
  "Restituisce la fitness di un numero"
  (length (remove-if #'zerop n)) )

(defun generate (i top) ;; a partire da questa versione, top è un numero decimale
  "Restituisce il primo tra i migliori vicini di I, stando attento a non sforare TOP"
  (let ((pos (position 0 i :from-end t)))
    (if (null pos) i
      (let ((copy (copy-list i)))
        (setf (nth pos copy) 1)
        (if (= (fitness i) (fitness j)) )

(defun best (lst)
  "Data una lista di codifiche binarie, restituisce quella con fitness migliore"
  (car (sort lst #'best-fit-check)) )

(defun local-search (n)
  "Esegue una ricerca locale con spazio delle soluzioni tra 0 e N"
  (do* ((i     (init n)     (best (list i j)))
        (j     (generate i n) (generate i n))
        (i-dec (bin2dec i)  (bin2dec i))
        (fi    (fitness i)  (fitness i))         ;NOTA: solo per leggibilità, per ottimizzazione rimuoverle
        (fj    (fitness j)  (fitness j)))
       ((or (equal i j) (&#62; fi fj))  ; ecco che vado ad assicurarmi dal loop sul massimo
        (format t #&#124;"~&#38;FINE:&#124;#"~&#38;Soluzione finale:  ~D = ~A - Fitness: ~D" i-dec i fi)
         i-dec)
       (format t "~&#38;Passaggio attuale: ~D = ~A - Fitness: ~D" i-dec i fi) ))
&lt;/pre&gt;

Hope this can help, I spent some months to gain functional mentality - and it's still work in progress.

Have fun with lisp

-J</description>
		<content:encoded><![CDATA[<p>You&#8217;re too polite not to try it immediatly. I&#8217;m pasting a lisp implementation of a simple local search algorithm, that tries to find a number with as many &#8220;ones&#8221; as possible in his binary representation, with solution space 0 to n. Sorry for the documentation in Italian <img src="http://www.terminally-incoherent.com/blog/wp-includes/images/smilies/icon_razz.gif" alt="P" class="wp-smiley" /> </p>
<p>PSEUDO-CODE:</p>

<div class="wp_syntax"><div class="code"><pre class="c">init<span style="color: #66cc66;">&#40;</span>start<span style="color: #66cc66;">&#41;</span>;
i:=istart;
repeat
  generate <span style="color: #66cc66;">&#40;</span>j in Nh<span style="color: #66cc66;">&#40;</span>i<span style="color: #66cc66;">&#41;</span><span style="color: #66cc66;">&#41;</span>
  <span style="color: #b1b100;">if</span> fit<span style="color: #66cc66;">&#40;</span>j<span style="color: #66cc66;">&#41;</span> &amp;gt;= fit<span style="color: #66cc66;">&#40;</span>i<span style="color: #66cc66;">&#41;</span> then i:=j;
until f<span style="color: #66cc66;">&#40;</span>j<span style="color: #66cc66;">&#41;</span></pre></div></div>

<p>WORKING LISP IMPLEMENTATION:<br />
with much care on training on the &#8220;tail recursion&#8221; technic, that can generate code as fast as fortran does (look at &#8220;performance&#8221; on CLISP homepage)</p>

<div class="wp_syntax"><div class="code"><pre class="lisp"><span style="color: #66cc66;">&#40;</span><span style="color: #b1b100;">defun</span> dec2bin <span style="color: #66cc66;">&#40;</span>n<span style="color: #66cc66;">&#41;</span>  <span style="color: #808080; font-style: italic;">; ci serve per ottenere la rappresentazione binaria del max</span>
  <span style="color: #ff0000;">&quot;Restituisce la rappresentazione binaria di N, come lista di bit&quot;</span>
  <span style="color: #66cc66;">&#40;</span>dec2bin-acc n <span style="color: #b1b100;">nil</span><span style="color: #66cc66;">&#41;</span><span style="color: #66cc66;">&#41;</span>
&nbsp;
<span style="color: #66cc66;">&#40;</span><span style="color: #b1b100;">defun</span> dec2bin-acc <span style="color: #66cc66;">&#40;</span>n acc<span style="color: #66cc66;">&#41;</span>
  <span style="color: #ff0000;">&quot;Versione di dec2bin con accumulatore per tail recursion&quot;</span>
  <span style="color: #66cc66;">&#40;</span><span style="color: #b1b100;">if</span> <span style="color: #66cc66;">&#40;</span>= <span style="color: #66cc66;">&#40;</span><span style="color: #b1b100;">length</span> lst<span style="color: #66cc66;">&#41;</span> n<span style="color: #66cc66;">&#41;</span> lst
    <span style="color: #66cc66;">&#40;</span>expand-bin <span style="color: #66cc66;">&#40;</span><span style="color: #b1b100;">cons</span> <span style="color: #cc66cc;">0</span> lst<span style="color: #66cc66;">&#41;</span> n<span style="color: #66cc66;">&#41;</span><span style="color: #66cc66;">&#41;</span><span style="color: #66cc66;">&#41;</span>
&nbsp;
<span style="color: #66cc66;">&#40;</span><span style="color: #b1b100;">defun</span> fitness <span style="color: #66cc66;">&#40;</span>n<span style="color: #66cc66;">&#41;</span>
  <span style="color: #ff0000;">&quot;Restituisce la fitness di un numero&quot;</span>
  <span style="color: #66cc66;">&#40;</span><span style="color: #b1b100;">length</span> <span style="color: #66cc66;">&#40;</span>remove-<span style="color: #b1b100;">if</span> #'<span style="color: #b1b100;">zerop</span> n<span style="color: #66cc66;">&#41;</span><span style="color: #66cc66;">&#41;</span> <span style="color: #66cc66;">&#41;</span>
&nbsp;
<span style="color: #66cc66;">&#40;</span><span style="color: #b1b100;">defun</span> generate <span style="color: #66cc66;">&#40;</span>i top<span style="color: #66cc66;">&#41;</span> <span style="color: #808080; font-style: italic;">;; a partire da questa versione, top è un numero decimale</span>
  <span style="color: #ff0000;">&quot;Restituisce il primo tra i migliori vicini di I, stando attento a non sforare TOP&quot;</span>
  <span style="color: #66cc66;">&#40;</span><span style="color: #b1b100;">let</span> <span style="color: #66cc66;">&#40;</span><span style="color: #66cc66;">&#40;</span>pos <span style="color: #66cc66;">&#40;</span>position <span style="color: #cc66cc;">0</span> i :<span style="color: #555;">from</span>-end t<span style="color: #66cc66;">&#41;</span><span style="color: #66cc66;">&#41;</span><span style="color: #66cc66;">&#41;</span>
    <span style="color: #66cc66;">&#40;</span><span style="color: #b1b100;">if</span> <span style="color: #66cc66;">&#40;</span><span style="color: #b1b100;">null</span> pos<span style="color: #66cc66;">&#41;</span> i
      <span style="color: #66cc66;">&#40;</span><span style="color: #b1b100;">let</span> <span style="color: #66cc66;">&#40;</span><span style="color: #66cc66;">&#40;</span>copy <span style="color: #66cc66;">&#40;</span>copy-<span style="color: #b1b100;">list</span> i<span style="color: #66cc66;">&#41;</span><span style="color: #66cc66;">&#41;</span><span style="color: #66cc66;">&#41;</span>
        <span style="color: #66cc66;">&#40;</span><span style="color: #b1b100;">setf</span> <span style="color: #66cc66;">&#40;</span><span style="color: #b1b100;">nth</span> pos copy<span style="color: #66cc66;">&#41;</span> <span style="color: #cc66cc;">1</span><span style="color: #66cc66;">&#41;</span>
        <span style="color: #66cc66;">&#40;</span><span style="color: #b1b100;">if</span> <span style="color: #66cc66;">&#40;</span>= <span style="color: #66cc66;">&#40;</span>fitness i<span style="color: #66cc66;">&#41;</span> <span style="color: #66cc66;">&#40;</span>fitness j<span style="color: #66cc66;">&#41;</span><span style="color: #66cc66;">&#41;</span> <span style="color: #66cc66;">&#41;</span>
&nbsp;
<span style="color: #66cc66;">&#40;</span><span style="color: #b1b100;">defun</span> best <span style="color: #66cc66;">&#40;</span>lst<span style="color: #66cc66;">&#41;</span>
  <span style="color: #ff0000;">&quot;Data una lista di codifiche binarie, restituisce quella con fitness migliore&quot;</span>
  <span style="color: #66cc66;">&#40;</span><span style="color: #b1b100;">car</span> <span style="color: #66cc66;">&#40;</span>sort lst #'best-fit-check<span style="color: #66cc66;">&#41;</span><span style="color: #66cc66;">&#41;</span> <span style="color: #66cc66;">&#41;</span>
&nbsp;
<span style="color: #66cc66;">&#40;</span><span style="color: #b1b100;">defun</span> local-search <span style="color: #66cc66;">&#40;</span>n<span style="color: #66cc66;">&#41;</span>
  <span style="color: #ff0000;">&quot;Esegue una ricerca locale con spazio delle soluzioni tra 0 e N&quot;</span>
  <span style="color: #66cc66;">&#40;</span><span style="color: #b1b100;">do</span>* <span style="color: #66cc66;">&#40;</span><span style="color: #66cc66;">&#40;</span>i     <span style="color: #66cc66;">&#40;</span>init n<span style="color: #66cc66;">&#41;</span>     <span style="color: #66cc66;">&#40;</span>best <span style="color: #66cc66;">&#40;</span><span style="color: #b1b100;">list</span> i j<span style="color: #66cc66;">&#41;</span><span style="color: #66cc66;">&#41;</span><span style="color: #66cc66;">&#41;</span>
        <span style="color: #66cc66;">&#40;</span>j     <span style="color: #66cc66;">&#40;</span>generate i n<span style="color: #66cc66;">&#41;</span> <span style="color: #66cc66;">&#40;</span>generate i n<span style="color: #66cc66;">&#41;</span><span style="color: #66cc66;">&#41;</span>
        <span style="color: #66cc66;">&#40;</span>i-dec <span style="color: #66cc66;">&#40;</span>bin2dec i<span style="color: #66cc66;">&#41;</span>  <span style="color: #66cc66;">&#40;</span>bin2dec i<span style="color: #66cc66;">&#41;</span><span style="color: #66cc66;">&#41;</span>
        <span style="color: #66cc66;">&#40;</span>fi    <span style="color: #66cc66;">&#40;</span>fitness i<span style="color: #66cc66;">&#41;</span>  <span style="color: #66cc66;">&#40;</span>fitness i<span style="color: #66cc66;">&#41;</span><span style="color: #66cc66;">&#41;</span>         <span style="color: #808080; font-style: italic;">;NOTA: solo per leggibilità, per ottimizzazione rimuoverle</span>
        <span style="color: #66cc66;">&#40;</span>fj    <span style="color: #66cc66;">&#40;</span>fitness j<span style="color: #66cc66;">&#41;</span>  <span style="color: #66cc66;">&#40;</span>fitness j<span style="color: #66cc66;">&#41;</span><span style="color: #66cc66;">&#41;</span><span style="color: #66cc66;">&#41;</span>
       <span style="color: #66cc66;">&#40;</span><span style="color: #66cc66;">&#40;</span><span style="color: #b1b100;">or</span> <span style="color: #66cc66;">&#40;</span><span style="color: #b1b100;">equal</span> i j<span style="color: #66cc66;">&#41;</span> <span style="color: #66cc66;">&#40;</span>&amp;gt<span style="color: #808080; font-style: italic;">; fi fj))  ; ecco che vado ad assicurarmi dal loop sul massimo</span>
        <span style="color: #66cc66;">&#40;</span>format t #|<span style="color: #ff0000;">&quot;~&amp;amp;FINE:|#&quot;</span>~&amp;amp<span style="color: #808080; font-style: italic;">;Soluzione finale:  ~D = ~A - Fitness: ~D&quot; i-dec i fi)</span>
         i-dec<span style="color: #66cc66;">&#41;</span>
       <span style="color: #66cc66;">&#40;</span>format t <span style="color: #ff0000;">&quot;~&amp;amp;Passaggio attuale: ~D = ~A - Fitness: ~D&quot;</span> i-dec i fi<span style="color: #66cc66;">&#41;</span> <span style="color: #66cc66;">&#41;</span><span style="color: #66cc66;">&#41;</span></pre></div></div>

<p>Hope this can help, I spent some months to gain functional mentality - and it&#8217;s still work in progress.</p>
<p>Have fun with lisp</p>
<p>-J
</p>
]]></content:encoded>
				</item>
	<item>
		<title>by: Luke Maciak</title>
		<link>http://www.terminally-incoherent.com/blog/2007/12/22/lisp-parse-and-aggregate-a-csv-file/#comment-7455</link>
		<pubDate>Sun, 23 Dec 2007 22:25:30 +0000</pubDate>
		<guid>http://www.terminally-incoherent.com/blog/2007/12/22/lisp-parse-and-aggregate-a-csv-file/#comment-7455</guid>
					<description>The less than character gets eaten by wordpress because it is a start of a HTML tag. The &#60;pre lang="lisp"&#62; &#60;/pre&#62; environment however is handled by my code highlighting plugin which will automatically escape the &#60; character by converting it into &#38;lt; so you can just copy and paste code directly into the commend box. :)</description>
		<content:encoded><![CDATA[<p>The less than character gets eaten by wordpress because it is a start of a HTML tag. The &lt;pre lang=&#8221;lisp&#8221;&gt; &lt;/pre&gt; environment however is handled by my code highlighting plugin which will automatically escape the &lt; character by converting it into &amp;lt; so you can just copy and paste code directly into the commend box. <img src="http://www.terminally-incoherent.com/blog/wp-includes/images/smilies/icon_smile.gif" alt=")" class="wp-smiley" />
</p>
]]></content:encoded>
				</item>
</channel>
</rss>

<!-- Dynamic Page Served (once) in 2.288 seconds -->
