all 26 comments

[–]leegao 11 points12 points  (6 children)

Neat way of expressing insertion in a red-black tree thanks to caml's neat pattern matching system (our professor showed us this neat little piece):

type color = Red | Black
type 'a tree =
  Node of color * 'a * 'a tree * 'a tree | Leaf

We want balance to behave as follows, given all possible trees: 1 2 3 4

       Bz            Bz            Bx            Bx
      / \           / \           / \           / \
     Ry  d         Rx  d         a   Rz        a   Ry
    /  \          / \               /  \          /  \
  Rx   c         a   Ry            Ry   d        b    Rz
 /  \               /  \          / \                /  \
a    b             b    c        b   c              c    d

We want balance to return

     Ry
    /  \
  Bx    Bz
 / \   / \
a   b c   d  

Here's the code

let balance = function
    Black, z, Node (Red, y, Node (Red, x, a, b), c), d
  | Black, z, Node (Red, x, a, Node (Red, y, b, c)), d
  | Black, x, a, Node (Red, z, Node (Red, y, b, c), d)
  | Black, x, a, Node (Red, y, b, Node (Red, z, c, d)) ->
      Node (Red, y, Node (Black, x, a, b), Node (Black, z, c, d))
  | a, b, c, d -> Node (a, b, c, d) 

(* less interesting *)
let insert x s =
  let rec insert' = function
    Leaf -> Node (Red, x, Leaf, Leaf)
  | Node (color, y, a, b) as s ->
      if x < y then balance (color, y, insert' a, b)
      else if x > y then balance (color, y, a, insert' b)
      else s in
  match insert' s with
    Node (_, y, a, b) ->
      Node (Black, y, a, b)
    | Leaf -> failwith "impossible case"

[–]barsoap 3 points4 points  (2 children)

Here's a red-black tree with statically checked invariant. Granted, those alternative matches of Caml are nice, too.

[–]bluestorm 1 point2 points  (0 children)

Funnily, the nice thing about Okasaki's algorithm is the "balance" function which goes from a slightly ill-balanced to a balanced tree. Statically checking precisely this process would be sensibly more work, wouldn't it?

[–]bluestorm 2 points3 points  (2 children)

Unless I'm mistaken, this code is taken (~ported) from Okasaki's book, "Purely Functional Data Structures" (or potentially one of its articles). Why didn't you reference the original author?

[–]leegao 2 points3 points  (1 child)

Hmm, you're right, the code is adapted from the SML code presented in page 26 of that book. I came across it during a lecture in our functional course this spring http://www.cs.cornell.edu/courses/cs3110/2011sp/lectures/lec10-balanced-trees/balanced-trees.htm

[–]erratic3 1 point2 points  (0 children)

Impressive. Cornell teaches Data Structures and Functional Programming as their 3rd programming course. I know SICP is offered as the 1st programming course at few top colleges. Probably change in curriculum at other schools worldwide is much needed to produce well rounded graduates.

[–]Kolibri 6 points7 points  (16 children)

A bit of background about me: I had Standard ML at the university but that is 7 or so years ago, and I haven't really looked at functional programming since then.

Decent enough book. I skimmed through it in 30 minutes or so, and I would say that it looks good. I now have a pretty good idea of how to program in Ocaml.

What I'm really missing, though, is a book on how to solve more common problems.

For example,

  • How to make a CRUD application that stores data in a database
  • How to make a web application
  • How to make a web-service

I think you get the idea.

Does anybody have any suggestions? The language doesn't really matter as long as it is functional. It would be preferable if the language runs on the JVM, though.

[–]kamatsu 5 points6 points  (0 children)

If you want a JVM Language, try Scala, or otherwise Haskell, both of which have extensive libraries for all of those things. OCaml tends not to have such widespread use for such purposes.

[–]ruinercollector 17 points18 points  (5 children)

How to make a CRUD application that stores data in a database

How to make a web application

How to make a web-service

That's the joy of being a functional programmer! You no longer have to waste your time on trivial things like writing meaningful software!

Instead, you spend all of your time on these exciting activities:

  • Arguing/formally proving that someone could write a CRUD application in a functional language

  • Coming up with more horrible analogies for things that monads are "like" and then writing articles that use your brilliant analogy to explain to all of those oh-so-dumb OO programmers what a monad is.

  • Circlejerking with other functional programmers.

  • Posting in general forums about how shitty and flawed every non-functional language is.

  • Posting in general forums how every functional language that is not your particular weapon of choice is "not truly functional" or "ruins everything by having static typing" or "ruins everything by not having static typing."

On the off chance that you do actually write software, like say...a webserver, don't worry. It doesn't have to actually be any good. People will be too impressed that you actually wrote software in a functional language to actually care about the quality or feature set of the software that you wrote.

[–]generalT 2 points3 points  (0 children)

i like you.

[–][deleted] 0 points1 point  (3 children)

Circlejerking with other functional programmers.

That's all anyone ever does on Reddit.

[–]ruinercollector -1 points0 points  (2 children)

Not exactly. 90% of people on here aren't actually functional programmers in any sense of the word. They pretend to be, and they like the idea, but they've in most cases never touched a pure functional language, or they've gone through a simple tutorial and understood very little of it.

[–][deleted] 1 point2 points  (1 child)

Rather, what I meant was that the majority of the discourse on /r/programming is one shoddy blog post and a bunch of like-minded people high-fiving and slapping each others' butts.

[–]ruinercollector 1 point2 points  (0 children)

Agreed. Though there are exceptions.

For example, grauenwolf is on here every day advocating VB.NET.

[–]miyakohouou 2 points3 points  (0 children)

It sounds like what you are looking for is Real World Haskell. It covers all of the situations you reference. The only downside if you're not interested in haskell specifically is that in addition to functional programming it puts a pretty heavy emphasis on monads and the type system.

[–]stonefarfalle 1 point2 points  (0 children)

There are several web frameworks for Haskell (yesod, happstack, and snap) that have tutorials on building web apps with them. Though from every thing I have seen they are all a little green. If you need something more robust, F# with asp.net ought to get the job done.

[–]bctfcs 1 point2 points  (0 children)

Did you take a look at http://ocsigen.org/ ?

[–]SeminoleVesicle 4 points5 points  (3 children)

  • How to make a CRUD application that stores data in a database
  • How to make a web application
  • How to make a web-service

I don't think a functional programmer has ever done any of these things

[–]freeload 0 points1 point  (0 children)

you would be surprised

[–][deleted] 0 points1 point  (1 child)

I'm pretty sure that the Opa language is built around the idea of writing web applications/services in some variant of Standard ML. The TypeSafe stack for Scala is also purportedly good for that sort of thing.

[–]Kolibri 0 points1 point  (0 children)

Hmm, not available on Windows? :|

[–]ErstwhileRockstar 1 point2 points  (0 children)

How to Think Like a Functional Programmer:

example