Something weird happens when a programmer compares snippets in two languages, with even the tiniest subconscious desire to demonstrate that his language is better-endowed. This is 100% certain to happen: "Here's a lovingly-crafted version in my favorite language. And here's yours, coded by drunk monkeys."
By no means am I immune — the second I step on that path, you can bet I'm looking up monkeys in the phone book.
* How about a REAL comparison — what's it like to live-compose music with C? Here's how you do it with Clojure: (http://vimeo.com/22798433)
I'm soon teaching Clojure, and am considering starting with composing sounds. Rather than the boring university tradition of the toy fibonacci function that you'd never actually use for generating fibonacci numbers. (Who learns this way?)
And if you dig deeply, a lot of Clojure's core contributors come from the humanities: Clojure's creator was a music composition major, another majored in film, and another in theater. [1] (They're not showy about it.) So, there's something oddly appropriate about drawing from the humanities.
* Why not ask me to code the Lisp factorial? In Common Lisp:
(defun fib (n)
(loop for f1 = 0 then f2
and f2 = 1 then (+ f1 f2)
repeat n do (print f1)))
And unlike your Clojure code, this actually acts like your C version: printing the results. And it's imperative.
It's more readable than the C version. 4 lines. No boilerplate, preambles, nor semicolons. No weird linenoise like "( i = 0; i < n; i++ )" or "%d\n”. No type declarations. ("void"?) Just right into the action.
And BTW, unlike your C version, mine actually compiles. It's a full program. To make your C version a program, you need to add more boilerplate.
* Here's the #1 google hit for "fibonacci c", which you seem to have adapted your C version from: (http://www.zacharyfox.com/blog/fibonacci-project/fibonacci-in-c)
Except you didn't include the whole thing, like a way to input a number. Incomplete.
And you need incantations like this to run it:
gcc -o test test.c
./test
* You didn't use the #1 google hit for "clojure fibonacci". Not even within the top 5.
Even if we accepted it, which we shouldn't... not only is it very different from your C version, it's does far, far more.
Your C version simply prints numbers. What would it look like if it returned a sequence, like the Clojure version? How about an infinite sequence?
* My Clojure version, adapted from the #1 google hit:
(def fib-seq
((fn fib [a b]
(lazy-seq (cons a (fib b (+ a b)))))
0 1))
Now let's analyze why the Clojure version does far more than the C version. It is not a function, but a variable which holds all the fibonacci numbers. To infinity.
Let's show it in motion. Using the interactive Clojure prompt. (First, we'll make sure we only print out 10 items of a sequence; lest we print the infinite.)
> (set! *print-length* 10)
10
> fib-seq
(0 1 1 2 3 5 8 13 21 34 ...)
> (filter even? fib-seq)
(0 2 8 34 144 610 2584 10946 46368 196418 ...)
It even caches.
* Good vs. evil mythology: Good: fibonacci(5) Evil: (fibonacci 5)
All the best,
Tj
[1] http://www.codequarterly.com/2011/rich-hickey/ http://blog.fogus.me/2010/07/19/take-7-david-nolen/ http://stuartsierra.com/about-me
On Fri, Oct 28, 2011 at 3:24 AM, // ravi <ravi at platosbeard.org> wrote:
> On Oct 27, 2011, at 7:00 PM, Tayssir John Gabbour wrote:
>> On Thu, Oct 27, 2011 at 5:22 PM, // ravi <ravi at platosbeard.org> wrote:
>>>> Barring disability or other misfortune, your post leads me to
>>>> believe that you can choose to be a senior software developer ...
>>>
>>> And what makes you so sure that I am not a senior software developer?
>>
>> I imagine you are, but of course can't be 100% sure. (Would be awful
>> if you're homeless or something, unable to hold a job, and here I am
>> arguing with you while you're just trying to entertain yourself. And
>> it's odd that a privileged senior software dev would joke on leftist
>> forums about being a prole, and make fundamental errors about
>> languages despite being offered evidence.)
>
>
> I think this is the first post where you have actually addressed the issue (about languages). Previously you quoted Crockford, Eich others and the Clojure guy for extra credit to suggest that I am part of some ugly side to programming! Now along with the gratuitous stuff (“fundamental errors”) you have responded to my content. My thoughts: I did not say Lisp is not an imperative language. I compared it to what are traditionally called imperative languages: C, C++, Perl, etc. Yes, I am not an expert on programming languages, and I am not trying to pretend to be one. Nonetheless, a Google search or my own instruction at college (which included a couple of graduate courses in AI, some Lisp stuff, and a built from scratch compiler that implemented a subset of Ada and spat out x86 assembly) tell me that I am committing no fundamental error in calling Lisp (CL) a functional language. At any rate, those are terms I am using to denote the things, and it is the particular thing that I am concerned about: Lisp/Haskell/etc vs C/Perl/etc. My claim is somewhat easily verifiable by just examining the implementation of some function in C/Perl vs Haskell/Clojure/Lisp.
>
> This is a bit of Clojure code that generates the Fibonacci series, that I lifted off an IBM publication:
>
> (defn lazy-seq-fibo
> ([]
> (concat [0 1] (lazy-seq-fibo 0 1)))
> ([a b]
> (let [n (+ a b)]
> (lazy-seq
> (cons n (lazy-seq-fibo b n))))))
>
> Here’s the rough equivalent in C (this one prints the results), without recursion, also lifted off the net:
>
> void fibonacci(int n)
> {
> int a = 0, b = 1;
> int sum;
> int i;
>
> for ( i = 0; i < n; i++ )
> {
> printf("%d\n”, a);
> sum = a + b;
> a = b;
> b = sum;
> }
> }
>
> My claim is that the second is familiar (from training in middle school Algebra), readable and repeatable. The first is likely more elegant and clever, but also more obscure (starting from the Polish notation). I think there are consequences to these differences, and choosing one over the other, even though these consequences were in no way intended.
>
> Nor do I think that people need to know lambda calculus or monads to program in Lisp. I have programmed in Lisp. I was terrible at it, admittedly. I knew the lambda calculus, but not because of CS or Lisp, but I did not know most of the other terms that are tossed about. What I did say was that such concepts and jargon enter early and are emphasised in the literature, just as a whole slew of terms and concepts confront a novice programmer when it comes to OO programming. It is my contention that this creates a barrier. If you managed to learn Lisp on your own and without being faced by a barrage of abstractions (or despite/because of them), more power to you. That has not been my experience, personal, and when it came to witnessing the education of undergraduates (or coworkers).
>
> I could be wrong about all this. I bring it up here on LBO because of two reasons: one, well, it’s interesting to discuss this stuff, and second, I do think there are political considerations. I think it is not a matter of coincidence that non-CS folks were able to jump into the field (in particular web programming) easier with the advent of interpreted [imperative] languages, and the simple[r] UI paradigm of HTML (and later CSS), the same material used to build pretty complex software. The same is true of even Ruby or Python, neither of which gained prominence until CS minded programmers decided that “scripting” languages were too useful to resist.
>
> Perhaps my first post set this thread off on the wrong path. Definitely it is not trivial to come to agreement on the ease of use, underpinnings, etc., of a language (leave alone the implications of such ease of use).
>
> —ravi
>
>
> P.S: Given that my own favourite language, Perl, is on its deathbed, I have been tossing around for something that won’t return me to the dark ages before PCRE. There are things about Python - such as the white space business - that set my teeth on edge. I am thinking of Ruby, partly in the hope that it will keep me employable into my upcoming final decades. But Clojure or Scala or Haskell look very interesting.
>
>
> ___________________________________
> http://mailman.lbo-talk.org/mailman/listinfo/lbo-talk
>