• Skip to primary navigation
  • Skip to main content
  • Skip to primary sidebar

Noulakaz

The blog of Avinash, Christina, Anya and Kyan Meetoo.

  • Home
  • About
  • People
    • Christina & Avinash Meetoo
    • Avinash Meetoo
    • Christina Meetoo
    • Anya Meetoo
    • Kyan Meetoo
  • General
    • News
    • Mauritius
    • Politics
    • Education
    • Business
    • Travel
  • Computing
    • Apple
    • Linux
    • LUGM
    • Programming
    • Web
    • Technology
    • Knowledge Seven
  • Entertainment
    • Music
    • Movies
    • Photography
    • Sports

I like Haskell a lot…

20 April 2007 By Avinash Meetoo 12 Comments

I am a fan of functional programming. I find that paradigm extremely elegant and expressive. I used Scheme when I started university and I was happy. I discovered the Haskell programming language some years ago and it was love at first sight.

According to the official Haskell site,

“Haskell is a general purpose, purely functional programming language featuring static typing, higher order functions, polymorphism, type classes and monadic effects.”

This is a mouthful but is actually a good description of the language. One essential feature of Haskell is its typing system: Haskell features static typing but types are never specified explicitly but are always inferred by the compiler!

Another “feature” is the absence of variables. Yes, you don’t have any variables (i.e. something that can change its value) in Haskell. And if you think about it it’s also the case in SQL, Prolog, XSLT, etc.

Hugs 98 is a nice and easy Haskell programming environment ideal for beginners. GHC is a state-of-the-art Haskell compiler which regularly produces executables that run faster than programs written in C! Both are multi-platform and open source.

Ok. I hope you are salivating. And, cerise sur le gateau, the Haskell syntax is beautiful.

Here is our old friend Quicksort:

quicksort [] = []
quicksort (x:xs) = quicksort [ y | y<-xs, y<x ]
    ++ [x]
    ++ quicksort [ y | y<-xs, y>=x ]

Quicksorting an empty list gives an empty list. Quicksorting a non-empty list (which can always be written as an element x followed by a list of xs (get it?)) is the Quicksort of all elements from xs which are less than x (the pivot) followed by x followed by the Quicksort of all elements from xs which are greater or equal to x. Beautiful isn’t it? In case you don’t think this can be an actual program, you are not alone (read part 7 Lessons learned)

A more involved example is this program I wrote some years ago:

data Tree = Empty | CreateTree Integer Tree Tree

flattenInfix Empty = []
flattenInfix (CreateTree v l r) = flattenInfix(l) ++ [v] ++ flattenInfix(r)

flattenPrefix Empty = []
flattenPrefix (CreateTree v l r) = [v] ++ flattenPrefix(l) ++ flattenPrefix(r)

flattenPostfix Empty = []
flattenPostfix (CreateTree v l r) = flattenPostfix(l) ++ flattenPostfix(r) ++ [v]

insert x Empty = (CreateTree x Empty Empty)
insert x (CreateTree v l r) =
    if x<v then CreateTree v (insert x l) r
    else CreateTree v l (insert x r)

putLeft Empty Empty = Empty
putLeft Empty a = a
putLeft a Empty = a
putLeft a (CreateTree e l r) = (CreateTree e (putLeft a l) r)

remove x Empty = Empty
remove x (CreateTree e l r) =
    if x==e then putLeft l r
    else if x<e then (CreateTree e (remove x l) r)
    else (CreateTree e l (remove x r))

which implements a binary search tree with a number of common operations (three kinds of flatten, insert and remove). Read the code. I’m sure most of you will understand it much more than the equivalent C or C++ program…

The binary tree can be used like this:

tree = (CreateTree 20
    (CreateTree 10
        (CreateTree 5
            (CreateTree 1 Empty Empty)
            (CreateTree 7 Empty Empty))
        (CreateTree 15 Empty Empty))
    (CreateTree 30
        (CreateTree 25 Empty Empty)
        (CreateTree 35 Empty Empty)))

> flattenInfix tree
[1,5,7,10,15,20,25,30,35]

> flattenInfix (insert 4 tree)
[1,4,5,7,10,15,20,25,30,35]

> flattenInfix (remove 30 (insert 4 tree))
[1,4,5,7,10,15,20,25,35]

> flattenInfix
*** Expression : flattenInfix
*** Of type : Tree -> [Integer]

Do you see that Haskell has inferred that flattenInfix is a function which takes a Tree and returns a list of Integers! Can you see why?

Welcome to the world of Haskell!

Filed Under: Education, Programming, Technology

Reader Interactions

Comments

  1. Eddy Young says

    21 April 2007 at 00:17

    Haskell is all over programming.reddit.com, and I can understand why that is so. The idea of programs that access data only in read-only mode is attractive: multi-threading is vastly simplified without the risk of blocking calls.

    But I still can’t get my head around Haskell! And, I’m particularly good at learning new languages. Maybe, the concepts of object-oriented programming are just too ingrained in me :-)

  2. avinash says

    21 April 2007 at 07:51

    Functional programming has a big future for the reason you mention: referential transparency.

    Google and the parallel processing people are already using FP concepts. And the future of concurrent programming might be Data Parallel Haskell…

  3. selven says

    25 April 2007 at 23:50

    hmm.. you seem to be someone who falls in love at first sight a lot!
    ruby, now haskell :p

    Another “feature” is the absence of variables. Yes, you don’t have any variables (i.e. something that can change its value)

    hmm.. i would feel out of this world without variables.
    Anywayz, i guess i’ll have to touch some haskell also for my eoy project

  4. avinash says

    26 April 2007 at 01:10

    Hi Selven,

    I only write about things I like, that’s why you feel that I regularly fall in love with new things.

    Incidentally, here are some languages I like a lot: C, Scheme, Java, Ruby, Smalltalk, Haskell, SQL and AWK.

    As for the lack of variables, have a look at declarative programming…

  5. Cubicle Prisoner says

    27 August 2007 at 16:43

    What an elegant quicksort example. And completely pointless, of course, because quicksort relies on direct indexing/updating in place to be quick, which you don’t get in Haskell.

  6. avinash says

    27 August 2007 at 22:37

    The Glasgow Haskell Compiler is actually very good and the code it produces is more efficient that the latest Java 6 JIT compiler…

    I guess that’s good enough for me… especially from my pedagogical perspective.

  7. Jordi says

    26 October 2007 at 09:16

    One of the courses in the CS program of my university uses the Clean programming language. I don’t really know any Haskell, but Clean is supposed to be very much like it. I think some of the differences are how they handle I/O (Haskell uses monads, while in Clean you can also choose to use the uniqueness type), performance (Clean is very fast) and cross platform support (Clean is primarily developed for Windows). Aside from that there are some small syntactic differences.

    I quite liked Clean during the initial stage of the course, where the problems we had to solve were small. In the final project however, we had to use this large framework the teachers made for us, and I started to have a lot more trouble. I don’t know how this is in Haskell, but Clean’s error messages aren’t exactly clear. Also, I really missed using variables and side-effects when debugging (you can’t just put a ‘print’ statement somewhere). I hope someday I will learn to effectively work with these features/deficiencies.

    It is unfortunate that Clean is so rarely used. However, I will try to learn Haskell too, because I think that it will probably be around for longer and be more actively developed and supported.

  8. avinash says

    26 October 2007 at 13:00

    I’ve heard a lot of positive opinions on Clean. I should give it a look.

    Haskell is great also. Hugs is a very good environment to learn Haskell. And GHC rocks. I am especially looking forward to the concurrent extensions to GHC, specifically the Data Parallel part being worked on by Simon Peyton-Jones.

  9. Adam says

    12 November 2007 at 10:39

    @Cubicle Prisoner

    When you are using immutable data structures in-place updates and direct indexing can be done under the covers. That you can’t do these things directly in Haskell doesn’t mean the compiler is equally incapable.

  10. Sam says

    12 November 2007 at 16:00

    Cubicle Prisoner: You’re more or less right. Haskell’s basic list type is a linked list; mergesort is more appropriate for those.

    But then, who cares about sorting algorithms? Just use List.sort, and leave it to the library writers. (GHC’s library uses a mergesort, in case you’re wondering.) Or put your data in a Map or Set, and use toAscList to list the elements in order.

    Jordi: If you use GHC (not sure about other Haskell implementations) you can use Debug.Trace.trace to print a message when an expression is evaluated. I’ve seen it described as “a refreshing desert in the oasis of referential transparency”. But the main debugging method I use is to take expressions apart until I can see the error. Referential transparency really helps there.

  11. Catalina says

    9 April 2008 at 22:05

    Can you tell me more about the putLeft function you wrote?…What does it do?

  12. avinash says

    9 April 2008 at 23:12

    Hi Catalina,

    putLeft is a helper function for remove.

    putLeft takes two trees as parameters (the first tree has values which are ALL strictly less than those in the second tree) and creates a new tree which is a binary search tree.

    To do that, all the nodes from the first tree are inserted into the second tree as left and as down as possible.

Leave a Reply

Your email address will not be published. Required fields are marked *

Primary Sidebar

Our Personal Websites

Avinash Meetoo
Christina Meetoo
Anya Meetoo
Kyan Meetoo

You may also like

Default ThumbnailWhy are there so many programming languages? Default ThumbnailTwo fantastic books I’m currently reading Default ThumbnailMy first MSc module: Software Architecture Default ThumbnailJohn Backus, father of FORTRAN, dies at 82

Random Posts

What is a heavy user?

High-End Hi-Fi Audio Companies

Series (TV Shows) we watch on Netflix

Recent Comments

  • Memento Mori by Depeche Mode on My Top 50 Depeche Mode songs
  • Memento Mori by Depeche Mode on I have my dream HiFi setup: Audiolab and Elac
  • Avinash Meetoo on High-End Hi-Fi Audio Companies

Archives

  • March 2023 (5)
  • February 2023 (1)
  • December 2022 (1)
  • November 2022 (1)
  • October 2022 (4)
  • August 2022 (3)
  • July 2022 (3)
  • June 2022 (5)
  • May 2022 (5)
  • January 2022 (3)
  • December 2021 (2)
  • November 2021 (1)
  • October 2021 (1)
  • September 2021 (4)
  • August 2021 (2)
  • July 2021 (14)
  • May 2021 (2)
  • April 2021 (4)
  • March 2021 (9)
  • February 2021 (2)
  • January 2021 (1)
  • October 2020 (1)
  • September 2020 (1)
  • August 2020 (2)
  • July 2020 (5)
  • June 2020 (3)
  • May 2020 (5)
  • April 2020 (6)
  • March 2020 (2)
  • February 2020 (2)
  • January 2020 (2)
  • October 2019 (1)
  • September 2019 (2)
  • July 2019 (2)
  • June 2019 (1)
  • May 2019 (3)
  • April 2019 (2)
  • March 2019 (1)
  • February 2019 (1)
  • January 2019 (3)
  • December 2018 (1)
  • October 2018 (3)
  • August 2018 (2)
  • July 2018 (2)
  • June 2018 (1)
  • May 2018 (1)
  • April 2018 (1)
  • February 2018 (1)
  • December 2017 (1)
  • October 2017 (1)
  • September 2017 (1)
  • August 2017 (1)
  • July 2017 (1)
  • May 2017 (4)
  • April 2017 (3)
  • March 2017 (4)
  • February 2017 (5)
  • January 2017 (3)
  • October 2016 (1)
  • September 2016 (1)
  • August 2016 (4)
  • July 2016 (1)
  • June 2016 (1)
  • March 2016 (3)
  • February 2016 (3)
  • January 2016 (1)
  • December 2015 (1)
  • November 2015 (2)
  • September 2015 (1)
  • August 2015 (3)
  • March 2015 (1)
  • December 2014 (1)
  • November 2014 (4)
  • October 2014 (1)
  • March 2014 (2)
  • February 2014 (3)
  • December 2013 (1)
  • October 2013 (1)
  • September 2013 (1)
  • August 2013 (1)
  • July 2013 (1)
  • June 2013 (2)
  • May 2013 (1)
  • March 2013 (3)
  • January 2013 (2)
  • December 2012 (3)
  • November 2012 (4)
  • September 2012 (3)
  • August 2012 (2)
  • July 2012 (3)
  • June 2012 (2)
  • May 2012 (1)
  • April 2012 (2)
  • February 2012 (1)
  • January 2012 (4)
  • December 2011 (2)
  • November 2011 (1)
  • October 2011 (4)
  • September 2011 (2)
  • August 2011 (1)
  • July 2011 (2)
  • June 2011 (4)
  • April 2011 (7)
  • March 2011 (2)
  • February 2011 (1)
  • January 2011 (3)
  • November 2010 (3)
  • October 2010 (1)
  • September 2010 (2)
  • August 2010 (4)
  • July 2010 (2)
  • June 2010 (1)
  • May 2010 (3)
  • April 2010 (4)
  • March 2010 (3)
  • February 2010 (3)
  • January 2010 (5)
  • December 2009 (2)
  • November 2009 (3)
  • October 2009 (1)
  • September 2009 (5)
  • August 2009 (3)
  • July 2009 (1)
  • June 2009 (3)
  • May 2009 (2)
  • April 2009 (7)
  • March 2009 (12)
  • February 2009 (10)
  • January 2009 (5)
  • December 2008 (4)
  • November 2008 (11)
  • October 2008 (6)
  • September 2008 (7)
  • August 2008 (3)
  • July 2008 (8)
  • June 2008 (6)
  • May 2008 (5)
  • April 2008 (7)
  • March 2008 (6)
  • February 2008 (3)
  • January 2008 (6)
  • December 2007 (11)
  • November 2007 (10)
  • October 2007 (7)
  • September 2007 (9)
  • August 2007 (3)
  • July 2007 (7)
  • June 2007 (8)
  • May 2007 (14)
  • April 2007 (11)
  • March 2007 (18)
  • February 2007 (14)
  • January 2007 (15)
  • December 2006 (16)
  • November 2006 (10)
  • October 2006 (7)
  • September 2006 (8)
  • August 2006 (8)
  • July 2006 (6)
  • June 2006 (4)
  • May 2006 (13)
  • April 2006 (10)
  • March 2006 (11)
  • February 2006 (7)
  • January 2006 (14)
  • December 2005 (8)
  • November 2005 (6)
  • October 2005 (7)
  • September 2005 (2)
  • August 2005 (6)
  • July 2005 (2)
  • June 2005 (6)
  • May 2005 (15)
  • April 2005 (12)
  • March 2005 (3)
  • February 2005 (8)
  • January 2005 (3)
  • December 2004 (1)
  • November 2004 (2)
  • October 2004 (2)
  • September 2004 (3)
  • August 2004 (3)
  • July 2004 (3)
  • June 2004 (3)
  • May 2004 (6)
  • April 2004 (10)
  • March 2004 (12)
Creative Commons License This work is licensed by Avinash Meetoo under a Creative Commons Attribution-NonCommercial-ShareAlike 4.0 Unported License.