<?xml version="1.0" encoding="UTF-8"?><rss version="2.0"
	xmlns:content="http://purl.org/rss/1.0/modules/content/"
	xmlns:dc="http://purl.org/dc/elements/1.1/"
	xmlns:atom="http://www.w3.org/2005/Atom"
	xmlns:sy="http://purl.org/rss/1.0/modules/syndication/"
		>
<channel>
	<title>Comments on: It&#8217;s imperative that it&#8217;s functional</title>
	<atom:link href="http://www.hackinghat.com/index.php/lisp/its-imperative-that-its-functional/feed" rel="self" type="application/rss+xml" />
	<link>http://www.hackinghat.com/index.php/lisp/its-imperative-that-its-functional</link>
	<description></description>
	<lastBuildDate>Wed, 01 Feb 2012 14:27:00 +0000</lastBuildDate>
	<sy:updatePeriod>hourly</sy:updatePeriod>
	<sy:updateFrequency>1</sy:updateFrequency>
	<generator>http://wordpress.org/?v=3.3.1</generator>
	<item>
		<title>By: Mark Wilson</title>
		<link>http://www.hackinghat.com/index.php/lisp/its-imperative-that-its-functional/comment-page-1#comment-2262</link>
		<dc:creator>Mark Wilson</dc:creator>
		<pubDate>Mon, 17 Dec 2007 13:02:51 +0000</pubDate>
		<guid isPermaLink="false">http://www.hackinghat.com/index.php/lisp/its-imperative-that-its-functional#comment-2262</guid>
		<description>Yeah - I quite liked the Modula-2 stuff but you are far more qualified than me to comment on the suitability of the programming languages we were taught (having not followed that path in my career).

Certainly from an infrastructure perspective a little bit of Windows NT (even OS/2 or LAN Manager) would have set me up far better than any of the stuff we were doing!  And how many times were we told in our OS internals module that the PowerPC would kill off the 80x86 line of CPUs...

Those were the days.</description>
		<content:encoded><![CDATA[<p>Yeah &#8211; I quite liked the Modula-2 stuff but you are far more qualified than me to comment on the suitability of the programming languages we were taught (having not followed that path in my career).</p>
<p>Certainly from an infrastructure perspective a little bit of Windows NT (even OS/2 or LAN Manager) would have set me up far better than any of the stuff we were doing!  And how many times were we told in our OS internals module that the PowerPC would kill off the 80&#215;86 line of CPUs&#8230;</p>
<p>Those were the days.</p>
]]></content:encoded>
	</item>
	<item>
		<title>By: Steve Knight</title>
		<link>http://www.hackinghat.com/index.php/lisp/its-imperative-that-its-functional/comment-page-1#comment-2110</link>
		<dc:creator>Steve Knight</dc:creator>
		<pubDate>Fri, 07 Dec 2007 06:27:08 +0000</pubDate>
		<guid isPermaLink="false">http://www.hackinghat.com/index.php/lisp/its-imperative-that-its-functional#comment-2110</guid>
		<description>Oh yeah!  Modula-2 wasn&#039;t bad.   It was easy to apply the principles to other languages so it had some value.  

I don&#039;t know about your experience, but for me I think if we&#039;d have learnt C and a dynamic language of the time (like Smalltalk) it would have been more valuable than what we did learn.  To me at least.</description>
		<content:encoded><![CDATA[<p>Oh yeah!  Modula-2 wasn&#8217;t bad.   It was easy to apply the principles to other languages so it had some value.  </p>
<p>I don&#8217;t know about your experience, but for me I think if we&#8217;d have learnt C and a dynamic language of the time (like Smalltalk) it would have been more valuable than what we did learn.  To me at least.</p>
]]></content:encoded>
	</item>
	<item>
		<title>By: Mark Wilson</title>
		<link>http://www.hackinghat.com/index.php/lisp/its-imperative-that-its-functional/comment-page-1#comment-2074</link>
		<dc:creator>Mark Wilson</dc:creator>
		<pubDate>Wed, 05 Dec 2007 08:48:27 +0000</pubDate>
		<guid isPermaLink="false">http://www.hackinghat.com/index.php/lisp/its-imperative-that-its-functional#comment-2074</guid>
		<description>Hey Steve - it wasn&#039;t just COBOL... we did Modula-2 as well ;-)</description>
		<content:encoded><![CDATA[<p>Hey Steve &#8211; it wasn&#8217;t just COBOL&#8230; we did Modula-2 as well <img src='http://www.hackinghat.com/wp-includes/images/smilies/icon_wink.gif' alt=';-)' class='wp-smiley' /> </p>
]]></content:encoded>
	</item>
	<item>
		<title>By: Arnar Birgisson</title>
		<link>http://www.hackinghat.com/index.php/lisp/its-imperative-that-its-functional/comment-page-1#comment-1844</link>
		<dc:creator>Arnar Birgisson</dc:creator>
		<pubDate>Tue, 20 Nov 2007 10:30:43 +0000</pubDate>
		<guid isPermaLink="false">http://www.hackinghat.com/index.php/lisp/its-imperative-that-its-functional#comment-1844</guid>
		<description>You can &quot;cheat&quot; a little, using the fact that the sum of the squares of 1,...,n is n(n+1)(2n+1)/6

A little arithmetic gives that the difference between the sum of squares and square of sum of 1,...,n is
(3n^4 + 2n^3 - 3n^2 - 2n)/12

Plug in n=100 and get 25164150</description>
		<content:encoded><![CDATA[<p>You can &#8220;cheat&#8221; a little, using the fact that the sum of the squares of 1,&#8230;,n is n(n+1)(2n+1)/6</p>
<p>A little arithmetic gives that the difference between the sum of squares and square of sum of 1,&#8230;,n is<br />
(3n^4 + 2n^3 &#8211; 3n^2 &#8211; 2n)/12</p>
<p>Plug in n=100 and get 25164150</p>
]]></content:encoded>
	</item>
	<item>
		<title>By: Steve Knight</title>
		<link>http://www.hackinghat.com/index.php/lisp/its-imperative-that-its-functional/comment-page-1#comment-1822</link>
		<dc:creator>Steve Knight</dc:creator>
		<pubDate>Mon, 19 Nov 2007 06:37:20 +0000</pubDate>
		<guid isPermaLink="false">http://www.hackinghat.com/index.php/lisp/its-imperative-that-its-functional#comment-1822</guid>
		<description>Nice! Thanks.</description>
		<content:encoded><![CDATA[<p>Nice! Thanks.</p>
]]></content:encoded>
	</item>
	<item>
		<title>By: JFT</title>
		<link>http://www.hackinghat.com/index.php/lisp/its-imperative-that-its-functional/comment-page-1#comment-1792</link>
		<dc:creator>JFT</dc:creator>
		<pubDate>Sat, 17 Nov 2007 13:36:06 +0000</pubDate>
		<guid isPermaLink="false">http://www.hackinghat.com/index.php/lisp/its-imperative-that-its-functional#comment-1792</guid>
		<description>The make-range function termination test should be (&quot;less-than&quot; cur start) the symbol got eatten again!</description>
		<content:encoded><![CDATA[<p>The make-range function termination test should be (&#8220;less-than&#8221; cur start) the symbol got eatten again!</p>
]]></content:encoded>
	</item>
	<item>
		<title>By: JFT</title>
		<link>http://www.hackinghat.com/index.php/lisp/its-imperative-that-its-functional/comment-page-1#comment-1791</link>
		<dc:creator>JFT</dc:creator>
		<pubDate>Sat, 17 Nov 2007 13:33:19 +0000</pubDate>
		<guid isPermaLink="false">http://www.hackinghat.com/index.php/lisp/its-imperative-that-its-functional#comment-1791</guid>
		<description>Trying a larger post (sorry I&#039;m new at blogging!)

Seems like the it worked although formating got lost!

The idea here is that the function doing the real work is hidden inside and take an additional parameter (helper). &quot;Labels&quot; is used so the locally defined function can recurse. The original function name is now only use to jumpstart the helper!


Now I&#039;d like to suggest another way to do this exercise, it is less performan in strict language than with lazy language but it has some value to learn functional programming IMO.

If you take the problem more literally it&#039;s about working on the &quot;list of natural&quot;. So let&#039;s first make a small function to get such a list (I&#039;m more of and Haskeller so bear with me on CL ;) )

(defun make-range (start end)
       (labels ((helper (cur sofar)
                   (if (&amp;lt cur start)
                       sofar
                       (helper (1- cur) (cons cur sofar)))))
            (helper end ())))

Once we have that the rest of of the problem get&#039;s even simpler and even more generic!
(setq nats (make-range 1 100))

What is the list of the 100 first natural squared?
(mapcar (lambda (x) (expt x 2)) nats)
Let&#039;s generalize this to any list
(defun square-list (l)
	(mapcar (lambda (x) (expt x 2)) l))

What about doing a summation?
(defun sum (l)
	(reduce #&#039;+ l))

If we put it all together:
(let ((nats (make-range 1 100)))
   (- (expt (sum nats) 2)
	  (sum (square-list nats))))

Finally a last level of candy:
(defun square (x)
       (flet ((sq (n) (* n n)))
         (cond ((numberp x) (sq x))
           ((listp x) (mapcar #&#039;sq x))
           (1 (error &quot;unsupported type&quot;)))))

which afford us:
(let ((nats (make-range 1 100)))
   (- (square (sum nats))
      (sum (square nats))))

You have a nicer solution, which read almost like the problem and utilities to get range of numbers, sums &amp; square :)

P.S. In Haskell all of this is simply
nats = [1..100] in (sum nats)^2 - (sum (map (^2) nats))
and assuming I am not using sum, which is part standard library, and rather use the Haskell equivalent of reduce here&#039;s what I would get:
let nats = [1..100] in (foldl1 (+) nats)^2 - (foldl1 (+) (map (^2) nats))</description>
		<content:encoded><![CDATA[<p>Trying a larger post (sorry I&#8217;m new at blogging!)</p>
<p>Seems like the it worked although formating got lost!</p>
<p>The idea here is that the function doing the real work is hidden inside and take an additional parameter (helper). &#8220;Labels&#8221; is used so the locally defined function can recurse. The original function name is now only use to jumpstart the helper!</p>
<p>Now I&#8217;d like to suggest another way to do this exercise, it is less performan in strict language than with lazy language but it has some value to learn functional programming IMO.</p>
<p>If you take the problem more literally it&#8217;s about working on the &#8220;list of natural&#8221;. So let&#8217;s first make a small function to get such a list (I&#8217;m more of and Haskeller so bear with me on CL <img src='http://www.hackinghat.com/wp-includes/images/smilies/icon_wink.gif' alt=';)' class='wp-smiley' />  )</p>
<p>(defun make-range (start end)<br />
       (labels ((helper (cur sofar)<br />
                   (if (&amp;lt cur start)<br />
                       sofar<br />
                       (helper (1- cur) (cons cur sofar)))))<br />
            (helper end ())))</p>
<p>Once we have that the rest of of the problem get&#8217;s even simpler and even more generic!<br />
(setq nats (make-range 1 100))</p>
<p>What is the list of the 100 first natural squared?<br />
(mapcar (lambda (x) (expt x 2)) nats)<br />
Let&#8217;s generalize this to any list<br />
(defun square-list (l)<br />
	(mapcar (lambda (x) (expt x 2)) l))</p>
<p>What about doing a summation?<br />
(defun sum (l)<br />
	(reduce #&#8217;+ l))</p>
<p>If we put it all together:<br />
(let ((nats (make-range 1 100)))<br />
   (- (expt (sum nats) 2)<br />
	  (sum (square-list nats))))</p>
<p>Finally a last level of candy:<br />
(defun square (x)<br />
       (flet ((sq (n) (* n n)))<br />
         (cond ((numberp x) (sq x))<br />
           ((listp x) (mapcar #&#8217;sq x))<br />
           (1 (error &#8220;unsupported type&#8221;)))))</p>
<p>which afford us:<br />
(let ((nats (make-range 1 100)))<br />
   (- (square (sum nats))<br />
      (sum (square nats))))</p>
<p>You have a nicer solution, which read almost like the problem and utilities to get range of numbers, sums &amp; square <img src='http://www.hackinghat.com/wp-includes/images/smilies/icon_smile.gif' alt=':)' class='wp-smiley' /> </p>
<p>P.S. In Haskell all of this is simply<br />
nats = [1..100] in (sum nats)^2 &#8211; (sum (map (^2) nats))<br />
and assuming I am not using sum, which is part standard library, and rather use the Haskell equivalent of reduce here&#8217;s what I would get:<br />
let nats = [1..100] in (foldl1 (+) nats)^2 &#8211; (foldl1 (+) (map (^2) nats))</p>
]]></content:encoded>
	</item>
	<item>
		<title>By: JFT</title>
		<link>http://www.hackinghat.com/index.php/lisp/its-imperative-that-its-functional/comment-page-1#comment-1790</link>
		<dc:creator>JFT</dc:creator>
		<pubDate>Sat, 17 Nov 2007 12:25:16 +0000</pubDate>
		<guid isPermaLink="false">http://www.hackinghat.com/index.php/lisp/its-imperative-that-its-functional#comment-1790</guid>
		<description>Ok take 3 :P Trying with HTML escaping!
(defun sum-of-series (n &amp;optional f)
  (labels ((helper (current total)
             (let ((n-dash (if (null f)
                             current
                             (funcall f current))))
                (if (&amp;lt current 1)
                    total
                    (helper (1- current)
                            (+ n-dash total))))))
      (helper n 0)))</description>
		<content:encoded><![CDATA[<p>Ok take 3 <img src='http://www.hackinghat.com/wp-includes/images/smilies/icon_razz.gif' alt=':P' class='wp-smiley' />  Trying with HTML escaping!<br />
(defun sum-of-series (n &amp;optional f)<br />
  (labels ((helper (current total)<br />
             (let ((n-dash (if (null f)<br />
                             current<br />
                             (funcall f current))))<br />
                (if (&amp;lt current 1)<br />
                    total<br />
                    (helper (1- current)<br />
                            (+ n-dash total))))))<br />
      (helper n 0)))</p>
]]></content:encoded>
	</item>
	<item>
		<title>By: JFT</title>
		<link>http://www.hackinghat.com/index.php/lisp/its-imperative-that-its-functional/comment-page-1#comment-1789</link>
		<dc:creator>JFT</dc:creator>
		<pubDate>Sat, 17 Nov 2007 12:15:42 +0000</pubDate>
		<guid isPermaLink="false">http://www.hackinghat.com/index.php/lisp/its-imperative-that-its-functional#comment-1789</guid>
		<description>You&#039;re welcome :)

Out of curiosity how do I escape the &quot;less than&quot; and &quot;greater than&quot; sign ?</description>
		<content:encoded><![CDATA[<p>You&#8217;re welcome <img src='http://www.hackinghat.com/wp-includes/images/smilies/icon_smile.gif' alt=':)' class='wp-smiley' /> </p>
<p>Out of curiosity how do I escape the &#8220;less than&#8221; and &#8220;greater than&#8221; sign ?</p>
]]></content:encoded>
	</item>
	<item>
		<title>By: Steve Knight</title>
		<link>http://www.hackinghat.com/index.php/lisp/its-imperative-that-its-functional/comment-page-1#comment-1785</link>
		<dc:creator>Steve Knight</dc:creator>
		<pubDate>Sat, 17 Nov 2007 06:27:23 +0000</pubDate>
		<guid isPermaLink="false">http://www.hackinghat.com/index.php/lisp/its-imperative-that-its-functional#comment-1785</guid>
		<description>Hi JFT,

Wordpress, I think, truncates after unescaped &lt; and &gt;.   Not sure though I&#039;ll take a look.  

I think I understand what you mean though.   Even though my code looks like the &lt;code&gt;sum-of-series&lt;/code&gt; is the last item it&#039;s actuall part of a &lt;code&gt;+&lt;/code&gt; statement and that&#039;s the problem, it&#039;s simply not tail recursive because there&#039;s a summation that happens at the tail, not a recursive call.   Doh!  Thanks for pointing that out I&#039;ll give it a try!

Steve</description>
		<content:encoded><![CDATA[<p>Hi JFT,</p>
<p>WordPress, I think, truncates after unescaped &lt; and &gt;.   Not sure though I&#8217;ll take a look.  </p>
<p>I think I understand what you mean though.   Even though my code looks like the <code>sum-of-series</code> is the last item it&#8217;s actuall part of a <code>+</code> statement and that&#8217;s the problem, it&#8217;s simply not tail recursive because there&#8217;s a summation that happens at the tail, not a recursive call.   Doh!  Thanks for pointing that out I&#8217;ll give it a try!</p>
<p>Steve</p>
]]></content:encoded>
	</item>
</channel>
</rss>

