Live data from Hacker News

A Year of Functional Programming

japgolly.blogspot.com.au

111–120 of 172 posts

Re: A Year of Functional Programming

#111

It's not that I don't think that programming in a functional style isn't a good idea , it's just that the discussions about functional programming and the languages people use to implement its style so often suggest: If you use language 'x', then using mutation is wrong. and recently, I've been thinking about the practically important but socially awkward question: What language is best for implementing mutable state…

No, you shouldn't give up mutability or in-place algorithms. As you said, performance concerns demand them at times.

Haskell has a nice approach to that problem. Data is immutable by default, but mutable primitives are available. The type system forces you to be explicit about when you're using mutability, so that mutable data can never accidentally creep into logic where you relied on immutability for correctness.

Broadly speaking, most mutable data types fall into two categories: Those that are some flavor of "IO," and those that are some flavor of "ST." In brief, the IO type is Haskell's way of dealing with operations that logically must be performed in the right order. (In-place operations require as much.) You can think of IO as one continuous chain of operations that begins and ends with the lifetime of the program. In other words, you can't just drop in and out of IO at will; the chain must be unbroken.

ST is an interesting variant on the same idea, except it does allow you to drop in and out at will. That is, I can enter ST at any point in my program, including the functionally pure parts. How is this possible? Because it imposes a restriction that's not present with IO: You must not touch the outside world from within ST. In any ST function, you get your own little mutable sandbox, but as with any pure function, you can only see that which is passed to you, and all you can do to the outside world is return a value. So you can, e.g., take an immutable list, create a copy that you sort in-place, then return the sorted list as an immutable value.

IO and ST are each flexible in their own ways. With IO, you can do almost anything, including mutating something that's passed in by reference. That can be important for some programs' performance. ST is flexible in a different way: You can sneak it into pure functions.

Re: A Year of Functional Programming

#112

It's not that I don't think that programming in a functional style isn't a good idea , it's just that the discussions about functional programming and the languages people use to implement its style so often suggest: If you use language 'x', then using mutation is wrong. and recently, I've been thinking about the practically important but socially awkward question: What language is best for implementing mutable state…

I suppose I can only speak for myself, but as a functional programmer I would never ask you to give up mutation completely. I do think it's nice that most FP languages give us pure functions and persistent data structures that are easy to use and relatively performant, so we don't have to go out of our way to provide a pure (by construction) abstraction when we want to. The key idea is to be honest when you are askin…

Maybe, what I am thinking is that a language which makes it easy to program in a functional style, should make it equally easy to program in an imperative one. By extension, this may mean that a good language for functional programming ought to make it easier to program imperatively than languages designed to facilitate imperative programming.

Of course, this begs the question of what is easier and harder and better and worse. There's a case to be made that Haskell's monads make imperative programming easier. It's essentially a mathematical argument - rather abstracto-theoretical versus the sort of concrete arguments that get advanced to avoid discussions of why it might be better to reason with lambda-calculus versus using von-Neumann as a model.

To put it another way, being able to abstract away von-Neumann into mathematics is useful. But the von-Neumann model is useful because it is a model that we can easily get our heads around.

Re: A Year of Functional Programming

#113

Earlier quoted context omitted.

What we want is a comparison: what was the OO code, and what did the functional code turn out to be? That way, we can judge for ourselves. Typically, these examples take low-end crappy OO code and convert it to high-end elegant FP code. But this doesn't really convince anyone.

I don't know the OP's examples, but I think he is saying something subtly different: that he took (his own) low-end crappy OO code and converted it to high-end elegant OO code, using the insight gained from how he would have written it as FP.

[deleted]

Re: A Year of Functional Programming

#114

Earlier quoted context omitted.

I suppose I can only speak for myself, but as a functional programmer I would never ask you to give up mutation completely. I do think it's nice that most FP languages give us pure functions and persistent data structures that are easy to use and relatively performant, so we don't have to go out of our way to provide a pure (by construction) abstraction when we want to. The key idea is to be honest when you are askin…

Maybe, what I am thinking is that a language which makes it easy to program in a functional style, should make it equally easy to program in an imperative one. By extension, this may mean that a good language for functional programming ought to make it easier to program imperatively than languages designed to facilitate imperative programming. Of course, this begs the question of what is easier and harder and better…

If you asked a Haskell expert what their favorite imperative language is, they'll probably say "Haskell."

That is because it "makes it easier to program imperatively" more than any other language I've met.

Re: A Year of Functional Programming

#115
post #70

It's not that I don't think that programming in a functional style isn't a good idea , it's just that the discussions about functional programming and the languages people use to implement its style so often suggest: If you use language 'x', then using mutation is wrong. and recently, I've been thinking about the practically important but socially awkward question: What language is best for implementing mutable state…

Having finally decided to read Knuth's main works, I have to confess I am being drawn more and more heavily into this way of seeing things. The beauty of how Dancing Links works is rather remarkable. Similarly, finally understanding how to read some of the algorithms as specified has helped to see just how "imperative" algorithms can be analyzed rather comprehensively. Also, as a major aside, I would encourage more f…

I assume by Knuth's main works you're meaning his The Art of Computer Programming. If you've got a decent math background (college level with some discrete math and calculus, it will still be pretty difficult for solo study by someone with just a high school math background) and find the stuff in Volume 1 to be a bit too difficult, I recommend Concrete Mathematics. It's essentially the math portions of Chapter 1 extended to a full text. I found it much easier to get into. Some portions are essentially calculus for discrete functions. Reading it made a lot of the calculus things that I had difficulty with suddenly click (I passed Calculus II by willpower, not understanding).

Re: A Year of Functional Programming

#116
post #63

Earlier quoted context omitted.

> One is code reuse: yes FP code is definitely more reusable. The problem is that the kind of code it enables reuse of more than OO is very small, simple loops. It sounds like you just haven't used Haskell enough to have the full extent of the reusability become apparent. I agree that on the surface it seems like you're right. But that particular example of reuse is just the tip of the iceberg. It's actually a very p…

> With Haskell their reuse was an order of magnitude higher as measured by the number of external library dependencies! If we're talking real world -- as we should -- then this should be contrasted with the tradeoffs and alternatives. The most important kind of code reuse, IMO, actually happens at a larger scale, and in the past few years has worked remarkably well, since the advent of open-source. I'm talking about…

> The lack of Haskell libraries and bindings relative to other languages reduces this most important kind of reuse.

This is an instance of begging the question too. Newer, less popular languages will always have fewer libraries and bindings relative to more popular languages. It might be an argument against using Haskell at the current point in time, but it has nothing to do with the merits of the language itself.

But the reality is that Haskell is actually doing quite well in terms of libraries and bindings. Pick anything you can think of...odds are probably pretty good that Haskell has bindings to it. As an example, let's look at the two projects you mentioned: ZooKeeper and Hadoop. Yep, there are Haskell bindings to both of them. For ZooKeeper there's hzk (http://hackage.haskell.org/package/hzk), and for Hadoop there's hadron (https://github.com/Soostone/hadron) and there's even a nice presentation about it (http://vimeo.com/90189610).

Re: A Year of Functional Programming

#117

Earlier quoted context omitted.

Maybe, what I am thinking is that a language which makes it easy to program in a functional style, should make it equally easy to program in an imperative one. By extension, this may mean that a good language for functional programming ought to make it easier to program imperatively than languages designed to facilitate imperative programming. Of course, this begs the question of what is easier and harder and better…

If you asked a Haskell expert what their favorite imperative language is, they'll probably say "Haskell." That is because it "makes it easier to program imperatively" more than any other language I've met.

Only after the learning curve, though. It isn't immediately approachable as an imperative language.

Re: A Year of Functional Programming

#118
post #70

Earlier quoted context omitted.

Having finally decided to read Knuth's main works, I have to confess I am being drawn more and more heavily into this way of seeing things. The beauty of how Dancing Links works is rather remarkable. Similarly, finally understanding how to read some of the algorithms as specified has helped to see just how "imperative" algorithms can be analyzed rather comprehensively. Also, as a major aside, I would encourage more f…

I assume by Knuth's main works you're meaning his The Art of Computer Programming . If you've got a decent math background (college level with some discrete math and calculus, it will still be pretty difficult for solo study by someone with just a high school math background) and find the stuff in Volume 1 to be a bit too difficult, I recommend Concrete Mathematics . It's essentially the math portions of Chapter 1 ex…

:) I did indeed mean that. And, oddly enough, I did just pick up Concrete Mathematics. I am just close enough to "get" much of the math. What I can't do is make some of those leaps myself. Worse, I'm not entirely clear how they relate to what I do, day to day.

Despite all of that. I am loving every moment of these books. Highly highly recommended.

Re: A Year of Functional Programming

#119

Earlier quoted context omitted.

If you asked a Haskell expert what their favorite imperative language is, they'll probably say "Haskell." That is because it "makes it easier to program imperatively" more than any other language I've met.

Only after the learning curve, though. It isn't immediately approachable as an imperative language.

Yes, but suppose you could write the same program in two languages, a classic imperative one, and a purist and difficult FP language.

For the sake of argument, let's assume the classic imperative language is easier to approach for rookies. If it's also the easiest language to write bugs and make mistakes with, wouldn't the "harder" FP language still be a net win? As long as its learning curve isn't unapproachably steep -- i.e. so steep that your time to market becomes awful -- what is the advantage of the imperative language's ease to hit the ground running and writing lots of bugs?

Re: A Year of Functional Programming

#120

Ask HN: As somewhat of an old timer, I have a couple of questions about FP, not really worthy of creating their own thread: 1. I learned a rule (in my Pascal textbook), "avoid globals." Is FP just an embodiment of that principle? 2. I do a lot of programming involving hardware, often homemade. Hardware has state, such as whether a lamp is turned on or off. Does this just mean FP is inappropriate, or are there techniq…

1. I think "avoid globals" is a reasonable way into FP, but FP itself involves a lot more than just avoiding coupling via global state. I would say the main components are - nested definitions; - lexical scoping; - first class functions; - static typing; and - a whole bunch of programming techniques (e.g. monads) that have grown around them. 2. You certainly can use FP ideas in low-level programming but you'll have d…

>- static typing;

I'm not an expert on the theory or design of programming languages, but isn't static typing orthogonal to FP?

Post reply on HN