<rss version="2.0" xmlns:dc="http://purl.org/dc/elements/1.1/" xmlns:trackback="http://madskills.com/public/xml/rss/module/trackback/" xmlns:wfw="http://wellformedweb.org/CommentAPI/" xmlns:slash="http://purl.org/rss/1.0/modules/slash/"><channel><title>LISP</title><link>http://blogs.acceleration.net/russ/category/46.aspx</link><description>All things Lispy</description><managingEditor>Russ</managingEditor><dc:language>en-US</dc:language><generator>.Text Version 0.95.2004.102</generator><item><dc:creator>Russ</dc:creator><title>LINQ preview and Lisp</title><link>http://blogs.acceleration.net/russ/archive/2006/03/01/2835.aspx</link><pubDate>Wed, 01 Mar 2006 11:26:00 GMT</pubDate><guid>http://blogs.acceleration.net/russ/archive/2006/03/01/2835.aspx</guid><description>I really wanted to be excited about .Net 2.0 and C#'s 3.0 LINQ preview.   However, between .Net1.1 and .Net2 coming out, I started to play with LISP.
&lt;br /&gt;&lt;br /&gt;
First, I was digging through some C# 2.0 the other day (now with Generics and "Anonymous Delegates Outer variable capture" (craptastic, hacked, statically typed closures)),  and while reading a blogpost (that I lost, sorry) the blogger maid the excellent point that in removing static typing from some things (generics) they had to add reams of code.  Something there should smell bad already.  If you find yourself adding reams of code (and more, new syntax) to get away with leaving out half of what the language originally made you specify, maybe the language just shouldn't force you to specify that info.  I dunno maybe I'm crazy and/or stupid and/or a type-ist?(someone who's against typing?).
&lt;br /&gt;&lt;br /&gt;
On to LINQ: When I saw &lt;a href="http://weblog.infoworld.com/udell/2005/09/28.html"&gt;this forthcoming tech, LINQ&lt;/a&gt;, I thought, "Hey relational algebra-ish support inside of the language, nice".  Then I looked at the demo and the only thing I can think of is all of the lisp constructs to do this sort of high end querying.  All of the LISP ways that were built into the language long before its standardization (ANSI standardization: December 8, 1994) a decade ago.  I am fairly sure that Ruby blocks can be put to use to accomplish this same sort of thing.  Also Python list comprehensions.  It really makes me question why people are willing to wait another 2-3 years for LINQ when they are a download away from better, more dynamic frameworks.  Oh... right... its next to impossible to talk to sql server outside of an MS environment (vendor lockin).   And as always learning anything new (that isnt ms )

&lt;div style="white-space:pre;"&gt;
;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
;; C# Linq Demo from &lt;a href="http://weblog.infoworld.com/udell/2005/09/28.html"&gt;this .net blog&lt;/a&gt;
;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
XDocument doc = XDocument.Load("blog.xml");
 
var d  = new Dictionary&lt;string,string&gt;();
d.Add("2005/09","September 2005");
d.Add("2005/08","August 2005");
 
var a = new string[] { "greasemonkey", "ajax" };
 
var query =
  from
    item in doc.Descendants("item"),
    key in d.Keys,
    tag in a
  where item.Element("date").Value.Contains(key) &amp;amp;&amp;amp;
        item.Element("tags").Value.Contains(tag)
  orderby
    (string) item.Element("date") descending
  select new XElement("item",
    new XElement("month",d[key]),
    item.Element("date"),
    item.Element("title"),
    item.Element("tags"));
 
foreach (var result in query)
  Console.WriteLine(result);

;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
;; Lisp with Loop which as far as I can tell is about as close as I can get LINQ
;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
(mapc
 (lambda (elem) (print elem))
 
  (loop for item in doc.Decendants("items")
	for key in d.Keys
	for tag in a
	when (and
	      item
	      (find key (value (item.Element "date")))
	      (find tag (value (item.Element "tags"))))
	collect (make-instance 'XElement
			      :tag-name "month"
			      :children (list (make-instance 'XElement :tag-name "month")
					      (item.Element "date")
					      (item.Element "title")
					      (item.Element "tags")))
	into new-elems
	unless item do (return (sort new-rows #'xml-item-date-&amp;lt; )))  
&lt;/string,string&gt;&lt;/div&gt;&lt;br /&gt;

There might be more that I am missing (for example an XML library (use ClosureXML), and a predicate that will work for the sorting he wants).  About the nicest part of the LINQ stuff seems to be the OrderBy clause (also possibly GroupBy which isnt covered in the demo, but I could see being cool). However, sorting is more or less a done job given an appropriate predicate. Also there might be slightly more involved iterating going on in the query, but nothing that couldnt be handled with a modicum more code, wrapped in a macro to give it that linq-ish look, and packaged up to be useful in a week.&lt;img src ="http://blogs.acceleration.net/russ/aggbug/2835.aspx" width = "1" height = "1" /&gt;</description><body xmlns="http://www.w3.org/1999/xhtml">I really wanted to be excited about .Net 2.0 and C#'s 3.0 LINQ preview.   However, between .Net1.1 and .Net2 coming out, I started to play with LISP.
<br /><br />
First, I was digging through some C# 2.0 the other day (now with Generics and "Anonymous Delegates Outer variable capture" (craptastic, hacked, statically typed closures)),  and while reading a blogpost (that I lost, sorry) the blogger maid the excellent point that in removing static typing from some things (generics) they had to add reams of code.  Something there should smell bad already.  If you find yourself adding reams of code (and more, new syntax) to get away with leaving out half of what the language originally made you specify, maybe the language just shouldn't force you to specify that info.  I dunno maybe I'm crazy and/or stupid and/or a type-ist?(someone who's against typing?).
<br /><br />
On to LINQ: When I saw <a href="http://weblog.infoworld.com/udell/2005/09/28.html">this forthcoming tech, LINQ</a>, I thought, "Hey relational algebra-ish support inside of the language, nice".  Then I looked at the demo and the only thing I can think of is all of the lisp constructs to do this sort of high end querying.  All of the LISP ways that were built into the language long before its standardization (ANSI standardization: December 8, 1994) a decade ago.  I am fairly sure that Ruby blocks can be put to use to accomplish this same sort of thing.  Also Python list comprehensions.  It really makes me question why people are willing to wait another 2-3 years for LINQ when they are a download away from better, more dynamic frameworks.  Oh... right... its next to impossible to talk to sql server outside of an MS environment (vendor lockin).   And as always learning anything new (that isnt ms )

<div style="white-space:pre;">
;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
;; C# Linq Demo from <a href="http://weblog.infoworld.com/udell/2005/09/28.html">this .net blog</a>
;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
XDocument doc = XDocument.Load("blog.xml");
 
var d  = new Dictionary<string,string>();
d.Add("2005/09","September 2005");
d.Add("2005/08","August 2005");
 
var a = new string[] { "greasemonkey", "ajax" };
 
var query =
  from
    item in doc.Descendants("item"),
    key in d.Keys,
    tag in a
  where item.Element("date").Value.Contains(key) &amp;&amp;
        item.Element("tags").Value.Contains(tag)
  orderby
    (string) item.Element("date") descending
  select new XElement("item",
    new XElement("month",d[key]),
    item.Element("date"),
    item.Element("title"),
    item.Element("tags"));
 
foreach (var result in query)
  Console.WriteLine(result);

;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
;; Lisp with Loop which as far as I can tell is about as close as I can get LINQ
;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
(mapc
 (lambda (elem) (print elem))
 
  (loop for item in doc.Decendants("items")
	for key in d.Keys
	for tag in a
	when (and
	      item
	      (find key (value (item.Element "date")))
	      (find tag (value (item.Element "tags"))))
	collect (make-instance 'XElement
			      :tag-name "month"
			      :children (list (make-instance 'XElement :tag-name "month")
					      (item.Element "date")
					      (item.Element "title")
					      (item.Element "tags")))
	into new-elems
	unless item do (return (sort new-rows #'xml-item-date-&lt; )))  
</string,string></div><br />

There might be more that I am missing (for example an XML library (use ClosureXML), and a predicate that will work for the sorting he wants).  About the nicest part of the LINQ stuff seems to be the OrderBy clause (also possibly GroupBy which isnt covered in the demo, but I could see being cool). However, sorting is more or less a done job given an appropriate predicate. Also there might be slightly more involved iterating going on in the query, but nothing that couldnt be handled with a modicum more code, wrapped in a macro to give it that linq-ish look, and packaged up to be useful in a week.<img src ="http://blogs.acceleration.net/russ/aggbug/2835.aspx" width = "1" height = "1" /></body></item><item><dc:creator>Russ</dc:creator><title>New Macro With-Gensym</title><link>http://blogs.acceleration.net/russ/archive/2005/08/19/2037.aspx</link><pubDate>Fri, 19 Aug 2005 22:08:00 GMT</pubDate><guid>http://blogs.acceleration.net/russ/archive/2005/08/19/2037.aspx</guid><description>I have been playing with LISP over the last few days to try and hack together a with-gensym macro that removes the need to put all my symbols as gensyms in a macro.  &lt;a href="http://paste.lisp.org/display/10951"&gt;This is what Nathan and I came up with.&lt;/a&gt; It allows you to use a (with-gensym (a b c d) ...body...) block.  You don't need to unquote the symbols in the body, you can just use the symbols like normal, however in the expansion, there is a let making the symbol gensyms, and all of the symbols are replaced with ,symbol-name.   This way I dont ever have to think about gensym again.&lt;img src ="http://blogs.acceleration.net/russ/aggbug/2037.aspx" width = "1" height = "1" /&gt;</description><body xmlns="http://www.w3.org/1999/xhtml">I have been playing with LISP over the last few days to try and hack together a with-gensym macro that removes the need to put all my symbols as gensyms in a macro.  <a href="http://paste.lisp.org/display/10951">This is what Nathan and I came up with.</a> It allows you to use a (with-gensym (a b c d) ...body...) block.  You don't need to unquote the symbols in the body, you can just use the symbols like normal, however in the expansion, there is a let making the symbol gensyms, and all of the symbols are replaced with ,symbol-name.   This way I dont ever have to think about gensym again.<img src ="http://blogs.acceleration.net/russ/aggbug/2037.aspx" width = "1" height = "1" /></body></item><item><dc:creator>Russ</dc:creator><title>Playing with Defcached</title><link>http://blogs.acceleration.net/russ/archive/2005/08/14/2023.aspx</link><pubDate>Sun, 14 Aug 2005 14:30:00 GMT</pubDate><guid>http://blogs.acceleration.net/russ/archive/2005/08/14/2023.aspx</guid><description>When I finished &lt;a href="http://blogs.acceleration.net/russ/archive/2005/08/10/2014.aspx"&gt;
playing with defmemoized&lt;/a&gt; a few days ago I realized that I was very close to having a 
general caching scheme.  At that point I decided that when I got a chance I would write a 
new macro based on defmemoized: &lt;a href="http://paste.lisp.org/display/10649#5"&gt;defcached.&lt;/a&gt;  
To make this a caching scheme, I need to have the ability to tell the cache to expire.  To 
accomplish this I added a new vaiable in to the defining environment (timeout) and a new 
function named [symbol]-set-timeout.  The set timeout function tells it how long it should 
cache the results for each set of parameters passed to it.  Its probably about time to break 
out CLOS and learn it since an object is probably better than just inserting more functions 
into the package  name space.&lt;img src ="http://blogs.acceleration.net/russ/aggbug/2023.aspx" width = "1" height = "1" /&gt;</description><body xmlns="http://www.w3.org/1999/xhtml">When I finished <a href="http://blogs.acceleration.net/russ/archive/2005/08/10/2014.aspx">
playing with defmemoized</a> a few days ago I realized that I was very close to having a 
general caching scheme.  At that point I decided that when I got a chance I would write a 
new macro based on defmemoized: <a href="http://paste.lisp.org/display/10649#5">defcached.</a>  
To make this a caching scheme, I need to have the ability to tell the cache to expire.  To 
accomplish this I added a new vaiable in to the defining environment (timeout) and a new 
function named [symbol]-set-timeout.  The set timeout function tells it how long it should 
cache the results for each set of parameters passed to it.  Its probably about time to break 
out CLOS and learn it since an object is probably better than just inserting more functions 
into the package  name space.<img src ="http://blogs.acceleration.net/russ/aggbug/2023.aspx" width = "1" height = "1" /></body></item><item><dc:creator>Russ</dc:creator><title>Playing with defmemoized, redux</title><link>http://blogs.acceleration.net/russ/archive/2005/08/10/2014.aspx</link><pubDate>Wed, 10 Aug 2005 21:23:00 GMT</pubDate><guid>http://blogs.acceleration.net/russ/archive/2005/08/10/2014.aspx</guid><description>&lt;p&gt;
   So a while back &lt;a title="Birdman" href="http://blogs.acceleration.net/birdman/"&gt;Nathan&lt;/a&gt; made a post about &lt;a href="http://blogs.acceleration.net/birdman/archive/2004/12/27/424.aspx"&gt;playing with macros&lt;/a&gt;.  
   Since I have wanted to start playing with lisp for a while and I finally got slime going at home, I thought this would be as good a jumping in point as any. 
&lt;/p&gt;
&lt;p&gt;
   My first endevors where to simply recreate what &lt;a title="Birdman" href="http://blogs.acceleration.net/birdman/"&gt;Nathan&lt;/a&gt; had written with out cheating too much.  I ended up getting it pretty much right, except that I had missed a #' on the memoize call.
   Since this had taken relativly little time, I converted the macro to use gensyms, and tried to think of something else to do.  One of the things that always intrigued me a little was the way 
   that symbols are strings that can be manipulated like most anything else.  Memoizing is a pretty basic caching scheme, but it was missing something: the ability to clear the cache.
&lt;/p&gt;

&lt;p&gt;
   I decided that I would make defmemoize create two functions; one that would call the original function memoized, and one that would clear that functions cache. 
&lt;/p&gt;

   &lt;div class="code"&gt;
      (defmemoized foo (x y z) (sleep x) (+ y z))
      ;;this should put the functions (foo ,) memoized and (foo-clear-cache) into package scope
   &lt;/div&gt;

&lt;p&gt;
   On my &lt;a href="http://paste.lisp.org/display/10649#1"&gt; first attempt&lt;/a&gt;, it became pretty obvious that I didnt have a clue about the timing of this.  
   So I sat down to reconsider what I was doing and realized that, if I was going to declare memoize (the function) in local scope, I needed to move that into the backquoted area.  
   The reason to declare the function in local scope, is that we can pull the cache hashtable out into a slightly larger local scope so that my clear-cache function has access to the same hashtable.
   This lead to &lt;a href="http://paste.lisp.org/display/10649#2"&gt;version 2.&lt;/a&gt;.
&lt;/p&gt;
&lt;p&gt;
   Now that I had everything scoped correctly, I needed to figure out why my symbol munging wasnt working. 
   &lt;a title="Birdman" href="http://blogs.acceleration.net/birdman/"&gt;Nathan&lt;/a&gt; was working with me and he suggested that I use intern instead of make-symbol.  This sort of worked, except that rather than the symbol I wanted, 
   I got the symbol I wanted inside of pipes (ex: foo-clear-cache -&amp;gt; |foo-clear-cache|).  After some mucking about and trying different things, 
   &lt;a title="Birdman" href="http://blogs.acceleration.net/birdman/"&gt;Nathan&lt;/a&gt; noticed that the &lt;a href="http://www.lispworks.com/documentation/HyperSpec/Body/f_intern.htm"&gt;example code&lt;/a&gt; was using capital letters in the keyword arguments.  This combined with playing with the function for a few minutes, revealed to him, that if the string we are trying to symbolize is in all caps when we intern it, it comes without pipes.  Glad LISP is case insensitive, except when it isnt.
&lt;/p&gt;

&lt;p&gt; After, we figured out the symbol munging, the last thing I had to do was figure out which symbols should be #' when passed to funcall and which shouldnt.  While a little arduous do to CL's 2 cell system,   I finally managed to work it out in my &lt;a href="http://paste.lisp.org/display/10649#4"&gt;final version&lt;/a&gt;.  Along the way I came up with a little test script that allowed me to verify that both functions were working.  
&lt;/p&gt;

&lt;p&gt;
   Over all, I am liking LISP a lot, but there is definatly a learning curve.  I am hoping that now that I have gotten my feet wet a bit, I will be able to get into it a little more.  
&lt;/p&gt;&lt;img src ="http://blogs.acceleration.net/russ/aggbug/2014.aspx" width = "1" height = "1" /&gt;</description><body xmlns="http://www.w3.org/1999/xhtml"><p>
   So a while back <a title="Birdman" href="http://blogs.acceleration.net/birdman/">Nathan</a> made a post about <a href="http://blogs.acceleration.net/birdman/archive/2004/12/27/424.aspx">playing with macros</a>.  
   Since I have wanted to start playing with lisp for a while and I finally got slime going at home, I thought this would be as good a jumping in point as any. 
</p>
<p>
   My first endevors where to simply recreate what <a title="Birdman" href="http://blogs.acceleration.net/birdman/">Nathan</a> had written with out cheating too much.  I ended up getting it pretty much right, except that I had missed a #' on the memoize call.
   Since this had taken relativly little time, I converted the macro to use gensyms, and tried to think of something else to do.  One of the things that always intrigued me a little was the way 
   that symbols are strings that can be manipulated like most anything else.  Memoizing is a pretty basic caching scheme, but it was missing something: the ability to clear the cache.
</p>

<p>
   I decided that I would make defmemoize create two functions; one that would call the original function memoized, and one that would clear that functions cache. 
</p>

   <div class="code">
      (defmemoized foo (x y z) (sleep x) (+ y z))
      ;;this should put the functions (foo ,) memoized and (foo-clear-cache) into package scope
   </div>

<p>
   On my <a href="http://paste.lisp.org/display/10649#1"> first attempt</a>, it became pretty obvious that I didnt have a clue about the timing of this.  
   So I sat down to reconsider what I was doing and realized that, if I was going to declare memoize (the function) in local scope, I needed to move that into the backquoted area.  
   The reason to declare the function in local scope, is that we can pull the cache hashtable out into a slightly larger local scope so that my clear-cache function has access to the same hashtable.
   This lead to <a href="http://paste.lisp.org/display/10649#2">version 2.</a>.
</p>
<p>
   Now that I had everything scoped correctly, I needed to figure out why my symbol munging wasnt working. 
   <a title="Birdman" href="http://blogs.acceleration.net/birdman/">Nathan</a> was working with me and he suggested that I use intern instead of make-symbol.  This sort of worked, except that rather than the symbol I wanted, 
   I got the symbol I wanted inside of pipes (ex: foo-clear-cache -&gt; |foo-clear-cache|).  After some mucking about and trying different things, 
   <a title="Birdman" href="http://blogs.acceleration.net/birdman/">Nathan</a> noticed that the <a href="http://www.lispworks.com/documentation/HyperSpec/Body/f_intern.htm">example code</a> was using capital letters in the keyword arguments.  This combined with playing with the function for a few minutes, revealed to him, that if the string we are trying to symbolize is in all caps when we intern it, it comes without pipes.  Glad LISP is case insensitive, except when it isnt.
</p>

<p> After, we figured out the symbol munging, the last thing I had to do was figure out which symbols should be #' when passed to funcall and which shouldnt.  While a little arduous do to CL's 2 cell system,   I finally managed to work it out in my <a href="http://paste.lisp.org/display/10649#4">final version</a>.  Along the way I came up with a little test script that allowed me to verify that both functions were working.  
</p>

<p>
   Over all, I am liking LISP a lot, but there is definatly a learning curve.  I am hoping that now that I have gotten my feet wet a bit, I will be able to get into it a little more.  
</p><img src ="http://blogs.acceleration.net/russ/aggbug/2014.aspx" width = "1" height = "1" /></body></item></channel></rss>