fibonacci hoe-down

Submitted by metaperl on Sun, 12/18/2005 - 7:23pm.

[17:00:07] > let fibs = 1:1:(zipWith (+) fibs (tail fibs)) in take 10 $ fibs
[17:00:08] [1,1,2,3,5,8,13,21,34,55]
[17:00:14] <_metaperl> the free haskell books are not very good
[17:00:31] they're good enough for me, for now
[17:00:35] thankyou
[17:00:49] <_metaperl> jethr0: he did something like [ | x <- [1, 1] , y <- [2, ] ]
[17:00:52] <_metaperl> I cant remember it
[17:01:07] let fibs = [1,1] ++ [x + y | x <- fibs, y <- tail fibs] in take 10 $ fibs

Do lispers use macros often?

Submitted by Greg Buchholz on Fri, 11/11/2005 - 4:36pm.

Do lispers use macros often, or is that a misconception? I don't know, but here's one data point. Since Peter Seibel's book Practical Common Lisp is online, I thought I'd do a little investigation. With a little wget, lynx, and perl magic I analyzed the word frequency of the book. The word "macro" or one of its forms (such as macrophobic and macrophobia) occur 810 times. For reference, here are the words which occur more often...

  • 854 => 1
  • 875 => name
  • 876 => or
  • 914 => an
  • 944 => code
  • 949 => list
  • 951 => are
  • 1033 => by
  • 1114 => t
  • 1126 => if
  • 1261 => lisp
  • 1279 => value
  • 1337 => function
  • 1351 => be
  • 1402 => this
  • 1532 => as
  • 1609 => can
  • 1700 => with
  • 1781 => for
  • 1963 => it
  • 2433 => is
  • 2583 => that
  • 3329 => in
  • 3452 => and
  • 3836 => you
  • 4774 => of
  • 5951 => a
  • 5970 => to
  • 12616 => the

*Note: For the record, I personally love macros. Well, appropriate macros. Reinventing a new language syntax should be a fairly rare event. A nice language will be built from a relatively small set of powerful axioms which will preclude the need to invent new axioms all the time. Certainly "until" isn't a good example of when they should be used.

Haskell is not an academic toy, but it is not corporate ready either

Submitted by metaperl on Sat, 11/05/2005 - 6:15am.

Today in #haskell, Sylvan said that Haskell had a reputation as an academic toy and that books should start off with Monadic I/O and other "practical" concerns to eliminate that stereotype.

Cale responded by saying that GHCi's REPL provided adequate I/O facilities.

I responded by saying that the most practical thing you could do in learning Haskell is to obtain the ability to decompose a problem into a set of functions. Everything else about Haskell follows from pure lazy functions and until you are comfortable and conversant in this paradigm, you really can't develop any new and useful code. You will simply be an ignorant little monkey, aping things you saw in a tutorial which crippled your ability to analyze and synthesize strongly-typed, lazy functions.

I did not say this then, but I also have this response. The impression of Haskell will not change with another book with practical examples. It will only change when Haskell has huge corporate-ready libraries for every huge corporate issue: when Haskell has a CPAN locked and loaded and ready to go. When Haskell has something as widely used as Mailman. When every website you see is done with Haskell instead of PHP. When you can deploy CORBA apps with Haskell. When you can cache and pre-compute queries for a SQL database in Haskell. When you have the speed and flexibility and eager persistent data loading of mod_perl for Haskell. When you have the best or second best or third best or any high-quality fully functional efficient streaming media player sitting on everyone's Linux desktop. When the favorite free drawing program is written in Haskell. For Haskell equating CPAN. CGI has been a web standard for almost 10 years. Try matching the real-world ready-to-go functionality of - that's one CPAN module in the Perl toolbox. Just one. Then you have 4000 more to go.

Haskell is the most beautiful, elegant, and correctly designed programming language on this planet. It is also the most powerful at chopping apart new specs and writing reliable code in short periods of time - how else could it stomp every other language 2 years in a row in the ICFP? But the only way to remove an impression is with results - tangibles, deliverables and unfortunately the focus on beauty, elegance and correctness seems to get in the way of ever getting anything corporate-ready out the door.

I am grateful to be studying Haskell. It has done wonders for my corporate-level Perl development. The book to read is "The Haskell Road to Logic, Maths and Computer Programming." This book will force you to think clearly. It does not cover monads, but monads can wait until you can specify and solve problems. But my improved thought processes are one thing. Delivering projects on a deadline is another and Perl continues to deliver through a cooperative community based around CPAN. The Haskell community is based around the reason of masterminds doing the intelligent things. The Perl community is based around the needs of people doing practical things in the best way that they can.

Finally, getting corporate ready is not something you can plan. It happens. It happens by necessity. It happens when someone has to swim or sink.

Deriving the function to compute sublists of a list

Submitted by metaperl on Wed, 10/26/2005 - 12:42pm.

Earlier I discussed how = in Haskell is equational. Well, it certainly came to the rescue this time. I was trying to understand the given solution in "The Haskell Road to Logic, Maths, and Programming" for the problem of calculating all the sublists of a list.

powerList [] = [ [] ]
powerList (x:xs) = powerList xs ++ (map (x:) powerList xs)

So I looked at this program, but had no idea how in the world this was the solution. So what I did is I worked upwards from the most trivial case like so:

-- The powerlist of a list with no elements is a list consisting of
-- the empty list

powerList [] = [[]]

-- Now, let's work with a list with 1 element:

powerList [a] = [ [a], [] ]

-- Fair enough, now how about a list with 2 elements:

powerList [a,b] = [ [a,b], [b], [a], [] ]

-- Hmm, but let's rewrite that

[ [a,b], [b], [a], [] ] = powerList [a] ++ [ [a, b], [b] ]

-- And rewrite once again

powerList [a] ++ [ [a, b], [b] ] = powerList [a] ++ (map (b:) powerList [a])

-- Now let's try 3 elements just to make sure we have a pattern:

powerList [c,a,b] = [[c,a,b], [c,b], [c,a], [c]] ++ powerList [a,b]

-- but with order independance we have:

powerList [c,a,b] = powerList [a,b] ++ [[c,a,b], [c,b], [c,a], [c]]

-- but [[c,a,b], [c,b], [c,a], [c]] = map (c:) powerList [a,b] so:

powerList [c,a,b] = powerList [a,b] ++ map (c:) powerList [a,b]

-- generalizing we have:

powerList (x:xs) = powerList xs ++ (map (x:) powerList xs)

Were there any bitter battles in the design of Haskell?

Submitted by metaperl on Sun, 10/16/2005 - 8:02pm.

I find it hard to believe that a language designed by committee did not have bitter furious fights over some issues in the language. Then again, I suppose the design goals with Haskell were fairly clear. Purely functional, types because they are necesary. Laziness because it fits with pure functions. End of discussion!

By the way, I have to comment that the Haskell community is so much friendlier than Perl.

Why is Clean faster than Haskell?

Submitted by metaperl on Sun, 10/09/2005 - 4:21pm.

If you compare Clean and Haskell in the Debian shootout, Clean is consistently faster than Haskell. These languages are spitting images of each other, save that Clean has some way of avoiding Monads. In addition, Clean I believe is based on graphs instead of lambda calculus - but both of these compute the same things so why would that matter?

The summary table (at bottom, horribly rendered in Firefox), shows Clean winning 10-0.

Also, which language came first? Clean or Haskell?

that's enough of SJT

Submitted by metaperl on Sun, 10/09/2005 - 2:20pm.

i'm tired of beating around the bush. i have played with not being able to think long enough.

learning haskell is not an obligation for me. it is a hobby. i can't ever expect it to compete with C++ in terms of popularity. I chose haskell because it forces you to think clearly. I need to see this as a hobby which might become a profitable job one day... Perl did. Haskell might as well. Haskell is gaining corporate attention. Microsoft has always had its eye on it.

better to master something I like than to keep rushing through this book. Funny "The Haskell Road..." does not cover monads, but I feel confident I will be able to handle them if/when I make it through Doets/van Eijck's book.

The most important thing you can know about Haskell and Functional Programming

Submitted by metaperl on Thu, 10/06/2005 - 6:16pm.
I bought "The Haskell Road to Logic, Maths, and Programming" but never looked at it until recently. Even though I had gone through 16 chapters of Simon Thompson's book, I had failed to grasp just what Haskell was about but I knew there was something that I was missing. And then I saw it in Section 1.9 of "The Haskell Road..." entitled "Haskell Equations and Equational Reasoning"

The Haskell equations f x y = ... used in the definition of a function f are genuine mathematical equations. They state that the left hand side of the right hand side of the equation have the same value. This is very different from the use of = in imperative languages like C or Java. In a C or Java program the statement x = x*y does not mean that x and x*y have the same value, but rather it is a command to throw away the old value of x and put the value of x*y in its place. It is a so-called destructive assignment statement: the old value of a variable is destroyed and replaced by a new one.

Reasoning about Haskell definitions is a lot easier than reasoning about programs that use destructive assignment. In Haskell, standard reasoning about mathematical equations applies. E.g. after the Haskell declarations x= 1 and y = 2, the Haskell declaration x = x + y will raise an error "x" multiply defined. ... = in Haskell has the meaning "is by definition equal to"...

This was a huge landslide victory for me. Because I quit trying to write programs to get data here, data there. Values here, values there. Instead, I simply began to rewrite the original function as a new definition.

I became so confident that I was able to write a program to return all the leaves of a tree. and here it is:

data Tree a = Empty | Node a (Tree a) (Tree a) -- leaves takes a tree and an empty list and returns a list of leaves -- of the tree leaves :: Tree a -&gt; [a] -&gt; [a] leaves tree lis | empty tree = lis -- an empty tree is just the leaves so far -- add on current node if it is terminal.. NO! scratch that! no add -- on! That is an action. We are simply rewriting leaves tree lis -- as something else based on what we found out about leaves tree lis | terminal currentNode = currentNode tree : lis | empty rightBranch = leaves (leftBranch tree) lis | empty leftBranch = leaves (rightBranch tree) lis | otherwise = leaves (leftBranch tree) lis ++ leaves (rightBranch tree) lis

Looking back at "Algorithms in Haskell" by Rabhi and "Craft of FP" by Simon Thompson, they do both make this same statement, but somehow it never really hit me right.

How to create an infinite lazy list of function applications?

Submitted by metaperl on Thu, 10/06/2005 - 12:33pm.

I would like to create an infinite lazy list whose contents consist of:

[ x, f x, f (f x), f (f (f x)) ... ] -- ad infinitum.

How might I do this?

overlapping pattern matching instances: help needed

Submitted by metaperl on Thu, 10/06/2005 - 2:21am.

I am being told that I have overlapping pattern matches. I'm pretty sure it has to do with the otherwise clause and the following subString (x:xs) [] clause, but I don't know how to fix it

prefix [] ys = True
prefix xs [] = False
prefix [x] [y] = (x == y)
prefix (x:xs) (y:ys) = (x == y) && prefix xs ys

subString xs ys
| prefix xs ys = True
| otherwise = subString xs ys'
where ys' = tail ys
subString (x:xs) [] = False