Lobbying for functional programming

94 views
Skip to first unread message

Dilawar

unread,
Jul 8, 2013, 11:00:18 AM7/8/13
to wncc...@googlegroups.com

... but one must not be surprised if outsiders don't take Haskell too seriously... [We] say a lot about what functional programming isn't: It has no assignment, no side effect, no flow of control but not much about what it is. The functional programmer sounds rather a medieval monk, denying himself the pleasures of life in hope that it will make him virtuous. To those interested in material benefits, these 'advantages' are totally unconvincing. -- "Why functional programming matters", John Hughes, The Computer Journal, 1989

If someone comes to you with a new programming language, it is usually the time to run in other direction. Who needs one more language, don't we have millions of them already? Besides, if I can do something in a language I know well with the help of libraries, should I invest my time in learning a new language? A young student does not have such worldly concerns and he can be easily stimulated about a new language.

Why functional programming language? And why Haskell? Computer science people have their own reasons on why they prefer one language over other. But what about others, especially electrical people, who can be equally concerned about languages.

Electrical engineers are concerned with hardware and the way things gets executed on it. They have always preferred C to explore their processors and rightfully so. I doubt if any language can beat it on 'fetch and execute' type of processors (except for assembly). There is designing and modeling side of electrical engineering and here we have HDLs, and some other languages. Python is a versatile language designed for convenience: no seg-faults and core dumps. Moving from C to Python is relatively easy. Both languages have same type of flow. C/C++ is often taught in all engineering branches. Python can be learned with little effort.

In functional programming languages, most if not all things are functions. A function takes functions as argument and can return a functions as result. In C/C++, you have to do something similar with function pointers. Its a loose definition which might irritate those who have a theoretically neat mind. Nonetheless, it will serve our purpose for this informal discussion. It is the ease with which we can 'glue' functions together to create a new function which makes functional programming attractive. You can go through a classic work, 'Structure and Interpretation of Computer Programs' to get the taste of functional programming. They have used Scheme which is a dialect of Lisp language, a functional programming language as old as Fortran. Moving from Lisp or Scheme to Haskell is rather simple. But learning Haskell can be challenging for those who comes from C world, especially the concept of Monad.

We can list out some benefits of Haskell over C/C++. The cost is speed and memory. I am not suggesting that Haskell is always slow. It can be very fast. And sometimes as fast as C itself. Both Python and Haskell drops into C to do computationally expensive tasks. Learning to optimize the Haskell code can be challenging. A lot of work is being done on these directions.

The beauty of Haskell (or any functional programming language) lies in that fact that it captures the idea of recursion most naturally. Once you know the syntax, writing recursive function is extremely easy. This is usually their selling point: recursive functions and recursive data-structures are easier to handle. Python has also borrowed some of these functional programming features such as list-comprehension and lambda functions (anonymous function). The C++ 2011 standard has Lambda function. That should be the proof enough of its syntactic beauty.

The core of Haskell is small. If lambda calculus which is Turning complete has only two operations and can model any computation, then why should core of any language have more? Clean syntax and small core of language are beneficial for those who infer the logic by looking at the program. Translation of Haskell code to something else can be easier. Moreover as parallel hardwares are becoming common-place, functional languages will increase in their stature. Figuring out parallel component in C/C++ is not easy. Programmer has to figure it out and code it by himself. People have found that it is relatively much easier for Haskell compiler to figure the parallelism out by itself.

If you have ever written a large program and you must have spent more time debugging the application than coding it. In fact, a saying in programming community is, 'Anyone can code, it takes a programmer to debug'. Haskell reduces chances of a programmer creating bugs significantly by its strong type system. In Electrical Engineering, no one really gives a damn if a language has strong type-system, you should ask your CS friend about it and its merit. A simple example is following to make a point.

Lets take the following example,

typedef speed_t double;
typedef acceleration_t double;
typedef distance_t double;

speed_t a
= 10.0;
acceleration_t b
= 9.8;
distance_t c
= a + b;

Now, its illegal to add speed and acceleration. But it is a legal piece of code in C. Such bugs will be incredibly hard to locate in C. An equivalent code in Haskell would not compile at all. The operator '+' demands that a and b should be of same type which they are not. Since Haskell check types at compile type (unlike python which checks it at run-time), it will easily catch it (and can easily irritate you).

People talk about many other benefits Haskell provides to a programmer. One of them is that one has to type fewer lines. This is certainly true but what is doubtful if producing n line of code in Haskell is less time consuming that writing its equivalent in Python.

My own experience with Haskell language are of mixed kind.  When you are working with recursive stuff, Haskell is very pleasant to code in. When it comes to non-recursive stuff such as graph, Haskell can become painfully slow. It has to convert graph (which is a non-recursive data-structure) to an equivalent recursive structure which takes a lot of memory. You will overflow your stack much easier in Haskell than in Python or C. Hardcore algorithmic people will not find it very exciting.

I'd recommend Haskell not because you are going to use to use it in your professional life. Most probably you won't. It would be a worthy investment just to learn the most rudimentary Haskell. Haskell makes you think and reason at very high level. Mistakes made at high levels become easily apparent in Haskell then they would be in C or Python. Moreover, once you becomes efficient in Haskell, breaking your design in smaller pieces and gluing them together will come naturally to you. If writing modular program is a virtue than Haskell is perhaps one of the best language around. Breaking and gluing functions is the essence of of functional programming. It is made easier in Haskell. Learning Haskell will make you a better programmer even if you are not coding in Haskell.

You can also try Scheme. Best thing about Scheme is that you can call yourself a 'schemer' who schemes everyday, which is million times cooler than calling yourself a 'Haskeller'.

Resources :

[1] Prof. Sanyal page on Haskell. Check out the problem sheet.
[2] Real world Haskell. This is a really good book to start with.
[3] http://learnyouahaskell.com/ . Read it at your own risk. Sometimes he says much about nothing.
[4] Structure and interpretation of computer program. This old classic is freely available. Still a great book to learn about computer programs.

--
Dilawar
EE, IITB

Antariksh Bothale

unread,
Jul 8, 2013, 12:38:48 PM7/8/13
to wncc...@googlegroups.com
You could also try Scaling the walls of functional programming.


--
--
The website for the club is http://stab-iitb.org/wncc
To post to this group, send email to wncc...@googlegroups.com
 
---
You received this message because you are subscribed to the Google Groups "Web and Coding Club IIT Bombay" group.
To unsubscribe from this group and stop receiving emails from it, send an email to wncc_iitb+...@googlegroups.com.
For more options, visit https://groups.google.com/groups/opt_out.
 
 



--
Antariksh Bothale | Business Analyst @ A. T. Kearney

Harsh Gupta

unread,
Jul 9, 2013, 3:01:30 AM7/9/13
to wncc...@googlegroups.com
Structure and Interpretation of Computer Programs(SICP): http://mitpress.mit.edu/sicp/
Using MIT scheme with DrRacket: http://www.neilvandyke.org/racket-sicp/



--
--
The website for the club is http://stab-iitb.org/wncc
To post to this group, send email to wncc...@googlegroups.com
 
---
You received this message because you are subscribed to the Google Groups "Web and Coding Club IIT Bombay" group.
To unsubscribe from this group and stop receiving emails from it, send an email to wncc_iitb+...@googlegroups.com.
For more options, visit https://groups.google.com/groups/opt_out.
 
 



--
Harsh

Kovid Kapoor

unread,
Jul 9, 2013, 7:46:33 AM7/9/13
to wncc...@googlegroups.com
Scala has been getting some support of late, and the founder of the language (Martin Odersky) took a course on Scala on Coursera. It might be running again.
Kovid Kapoor | Analytics Specials | Opera Solutions, LLC | +91-81304-91776

Manish Goregaokar

unread,
Jul 9, 2013, 7:19:45 PM7/9/13
to wncc...@googlegroups.com
@Dilawar Interesting post, it would fit better on a blog though (wncc has one coming up, along with a script hosting zone -- not finished yet but you may be interested in contributing)

-Manish Goregaokar

Dilawar

unread,
Sep 22, 2013, 2:21:29 PM9/22/13
to wncc...@googlegroups.com
Worth a click

http://www.sac-home.org/index.php

--
Dilawar
NCBS Bangalore
EE IITB

Sudarshan Wadkar

unread,
Sep 22, 2013, 5:09:05 PM9/22/13
to wncc...@googlegroups.com
I am attending a talk tomorrow on this topic. Will share whatever I could grab:

Cheers,
-S

---start copy-paste#sorry for the formatting---

Why Functional Programming Doesn't Matter

 - or -

Why purity and laziness matter less than you think, and types matter more


  

   Much has been said in recent years about the advantages of

   functional programming, including languages like Scheme, Erlang,

   Clojure, Haskell, Scala, F#, SML and OCaml.


   One common view among backers of functional programming, perhaps

   best summarized by John Hughes's paper "Why Functional

   Programming Matters", is that what makes functional languages so

   effective is a trio of features: higher order functions,

   laziness and purity.


   This talk will present an alternate view, based on our

   experiences at Jane Street using OCaml to build production

   software over the past decade.


   We don't disagree with Hughes' view that statically typed

   functional languages are unusually effective tools.  But we

   believe that Hughes' paper gets the order wrong.  In particular,

   we'll argue that an expressive static type system, which Hughes

   doesn't mention, should be near the top of the list, and purity,

   which many think of as the defining aspect of functional

   programming, is near the bottom, and laziness is almost not on the

   list at all.



Dilawar Singh

unread,
Sep 23, 2013, 3:07:55 AM9/23/13
to wncc...@googlegroups.com
There are various planes of analysis one can put oneself on while talking about languages. To list some of them which academia found interesting: how well a language map on to the processors? How easily I can prove the correctness of my program? Does language allows me to design system which are "correct by constructions"? How easily concurrency and parallelism encoded in the semantics? How easy is to translate my algorithm to a program? etc. And then one can ask what I get from functional programming?

Laziness (along with immutability, a variable does not change its value throughout the execution of program) is rather important if you want to do away with control flow. Ordering of statements does not matter now and compiler can argue about the program rather "easily". Formal verification can also be simpler on such schemes. These are important issues in academia and research; it may not be important for others.

Anyway, it would be nice to know how successful functional programming has been in building commercial applications.

--
Dilawar
--
Dilawar
NCBS Bangalore

Sudarshan Wadkar

unread,
Sep 24, 2013, 1:19:47 AM9/24/13
to wncc...@googlegroups.com
I am really sorry guys. I reached there late and the "technical" part of the talk was over. I did ask few of my friends about what they thought of the presentation i.t.o. Functional Programming but everyone was like 'yeah whatever...'

@Dilawar: Yes, functional programming - at least OCaml - is used quite heavily in Jane Street (a high frequency trading firm). You can find more resources/information about how/why/what of OCaml, functional programming on Jane Street's website. The blog looks very informative /me thinks.

Cheers,
-S

Anil Shanbhag

unread,
Sep 24, 2013, 2:52:44 AM9/24/13
to wncc...@googlegroups.com
I see Erlang being used in lot of places. The main reason everyone states in inbuilt concurrency. 
If someone could shed some light on Erlang while we are on functional programming languages. 

About OCaml itself : 

@Dilawer 
Can you highlight on why it is easier to prove functional programs formally ?
Anil Shanbhag

Dilawar Singh

unread,
Sep 24, 2013, 4:34:43 AM9/24/13
to wncc...@googlegroups.com
>@Dilawer
>Can you highlight on why it is easier to prove functional programs formally
>?

Ok, let me try.

The root of functional programming is lambda calculus. Now the usual way of
thinking about a function is that it takes fews arguments and return a result or
"takes few points and return one point" (kind of f: R -> R abstraction). Now if
I were to prove that my function is correct, I have to verify all possibilities
of input patterns. Some smart things can be done about it but the basic idea is
same, check for all possible inputs directly or indirectly.

Now the lambda calculus can be seen as "applying terms to term" and reducing
them to a simpler one. To draw an analogy, it is the way we did mathematics in
high-school. Now let me put an example to contrast, say I were to compute the
following

y = sin^2 x + cos^2 x

In non-lazy languages, each argument will be computed: sin x and cos x has to be
computed for each x and then squared and added to give y. Lazy languages does
not compute as soon as they see the argument, they wait till user demands the
result. Now if I want to print y in functional language, the compiler has
chances to optimize it (we tells the compiler that we know the term "sin^2 x +
cos^ x" can be replaced by the constant term "1"). No need to compute here. I
can also argue formally now that the result must be 1 (give that I know which
terms are to be reduced to what term) and not some approx of 1 as computed by
eager languages.

This is rather an example which occurs rarely in practice but it is put here to
draw a sharp contrast. First in functional languages, functions are not
something which operates on points; they are more like terms (see Term rewriting
systems). Therefore we can apply functions to functions i.e. sin(cos (tan x))).

Another very useful feature in functional languages is immutability: if a is
given a value 5, it will remain 5 i.e. assignments are absent. This makes
compiler's job very easy when it comes to track the variables. On the flip side,
a lot of memory is consumed by functional languages. Immutability is very useful
feature if one wants to argue formally about a program; now I dont have to worry
about if the value of a will be changed later.

Haskell has been used quite successfully to generate proofs of mathematical
theorems which I take to be the power of a language to reason formally.

http://gowers.wordpress.com/2013/04/02/a-second-experiment-concerning-mathematical-writing/

A formal treatment is by M. Ganeshlingam (which I don't quite follow
completely).

http://people.ds.cam.ac.uk/mg262/

--
Dilawar
NCBS Bangalore
EE, IIT Bombay

Dilawar Singh

unread,
Sep 27, 2013, 4:50:59 AM9/27/13
to wncc...@googlegroups.com

http://ieeexplore.ieee.org/iel3/3826/11178/00545595.pdf
"I am done simulating. Now what?"


Its a very nice paper which brings out the limitation of simulation when
dealing with microprocessors and other digital systems.

--
Dilawar
NCBS Bangalore
EE, IIT Bombay

On Tue, Sep 24, 2013 at 12:22:44PM +0530, Anil Shanbhag wrote:

Dilawar Singh

unread,
Oct 3, 2013, 4:55:07 AM10/3/13
to wncc...@googlegroups.com
An interesting presentation by a game-developer. Some slides talks about benefits and limitation of Haskell.

www.cs.princeton.edu/~dpw/popl/06/Tim-POPL.ppt



For more options, visit https://groups.google.com/groups/opt_out.


 --
--
The website for the club is http://stab-iitb.org/wncc
To post to this group, send email to wncc...@googlegroups.com

---
You received this message because you are subscribed to the Google
Groups "Web and Coding Club IIT Bombay" group.
To unsubscribe from this group and stop receiving emails from it, send

For more options, visit https://groups.google.com/groups/opt_out.




--
Dilawar
NCBS Bangalore

--
--
The website for the club is http://stab-iitb.org/wncc
To post to this group, send email to wncc...@googlegroups.com

---
You received this message because you are subscribed to the Google Groups
"Web and Coding Club IIT Bombay" group.
To unsubscribe from this group and stop receiving emails from it, send an

For more options, visit https://groups.google.com/groups/opt_out.


 --
--
The website for the club is http://stab-iitb.org/wncc
To post to this group, send email to wncc...@googlegroups.com

---
You received this message because you are subscribed to the Google Groups
"Web and Coding Club IIT Bombay" group.
To unsubscribe from this group and stop receiving emails from it, send an

For more options, visit https://groups.google.com/groups/opt_out.




--
Anil Shanbhag

--
--
The website for the club is http://stab-iitb.org/wncc
To post to this group, send email to wncc...@googlegroups.com

---
You received this message because you are subscribed to the Google Groups "Web and Coding Club IIT Bombay" group.
To unsubscribe from this group and stop receiving emails from it, send an email to wncc_iitb+unsubscribe@googlegroups.com.

For more options, visit https://groups.google.com/groups/opt_out.



--
Dilawar
NCBS Bangalore

Sushant

unread,
Oct 21, 2013, 11:03:49 AM10/21/13
to wncc...@googlegroups.com

This resource for learning Haskell and getting glued to it is trending off late on HN.
Worth giving a shot!
http://yannesposito.com/Scratch/en/blog/Haskell-the-Hard-Way/

Regards,
Sushant

Dilawar Singh

unread,
Oct 21, 2013, 11:47:22 AM10/21/13
to wncc...@googlegroups.com
Thanks Sushant.

I had a look and there is one thing a reader should be aware of. Equivalent of
printf("%s", "Arrey duniya walo"); is the last thing you learn in Haskell
because doing and understanding whats going on in this statement is very hard.
May be I can nitpick on this.

A statement in C/C++, int a = 5;

One can think an equivalent model, 'The variable a is the name of state
which stores a value of type int and it is 5 at this moment'. Now a can
change its value anytime in a program e.g. a = a + 1 etc. Therefore a is
mutable. If it were not, you can not change its value.

after int a = 5; is executed. Somewhere in memory,
+----------------+
| a | -> 0xff727137 (some random address)
+-------+--------+
| int | 5 |
| (32) | |
+----------------+

Haskell does not allow mutability. Mutability has side effects (state 'a' is
changed from 5 to 6 after executing (a += 1)). In Haskell, we dont do that.
Mutability is lame!

So does "printf("%s", "Arrey Duniya walo"). Then how do you print any
statement in Haskell? Wait, opening a file and writing to it also have side-
effect; how do you open files and write to them? And wait some more, don't
you update the seed or something like that when you generate random numbers.
How do you do that?

To do all of them, Haskell has a concept called `monad`: now this is something
not really easy to grasp. They are also not easy to implement and all books
introduce them somewhere in the middle, therefore they never starts with "hello
world" program. They become harder to understand, if you have already learn a
language like C/Python. If you know nothing about programming, then Haskell will
not be counter-intuitive at times.

This guy does start with "Hello world" program, perhaps to make a newcomer
comfortable. But anyway, he is aware that there is no shortcut to learn this
language. He might be having something up his sleeve.

--
Dilawar
>> [1] Prof. Sanyal page on Haskell<http://www.cse.iitb.ac.in/~as/fpcourse/fpcourse.html>.
>> Check out the problem sheet.
>> [2] Real world Haskell <http://book.realworldhaskell.org/read/>. This is
>> a really good book to start with.
>> [3] http://learnyouahaskell.com/ . Read it at your own risk. Sometimes he
>> says much about nothing.
>> [4] Structure and interpretation of computer program. This old classic is
>> freely available. Still a great book to learn about computer programs.
>>
>> --
>> Dilawar
>> EE, IITB
>>
>
>--
>--
>The website for the club is http://stab-iitb.org/wncc
>To post to this group, send email to wncc...@googlegroups.com
>
>---
>You received this message because you are subscribed to the Google Groups "Web and Coding Club IIT Bombay" group.
>To unsubscribe from this group and stop receiving emails from it, send an email to wncc_iitb+...@googlegroups.com.
Reply all
Reply to author
Forward
0 new messages