Strange Loops

No Matter Where You Go, There You Are

Two Camls Diverged in a …

| Comments

It turns out that Ocaml is a fairly simple language to learn. Very few keywords (let, type, match and function being the main ones) and the standard assortment of data structures (list, array, tuple, record). I think the best quick introduction to the language is Ocaml For Scientist Chapter 1 combined with Ocaml for Experienced Programmers. Overall I was impressed with Ocaml. Although it sells itself as a functional programming language, the language does have imperative constructs (when you absolutely positively need a for loop). It also has a good number of libraries available online (Ocaml can also use CPAN, yes CPAN! as in perl. Perl4Caml. I haven’t tried it though YMMV). Between Haskell and Ocaml, Haskell definitely felt like the more challenging of the two languages to learn. I would still start with Haskell if I did it over again. But definitely learn both languages!

Guess What This Ad Is For?

| Comments

Here’s to the Crazy Ones.
The misfits.
The rebels.
The troublemakers.
The round pegs in the square holes.
The ones who see things differently.

They’re not fond of rules.
And they have no respect for the status quo.

You can praise them, disagree with them, quote them,
disbelieve them, glorify or vilify them.
About the only thing that you can’t do, is ignore them.

Because they change things.

They invent. They imagine. They heal.
They explore. They create. They inspire.
They push the human race forward.

Maybe they have to be crazy.

How else can you stare at an empty canvas and see a work of art?
Or, sit in silence and hear a song that hasn’t been written?
Or, gaze at a red planet and see a laboratory on wheels?

We make tools for these kinds of people.

While some may see them as the crazy ones, we see genius.

Because the ones who are crazy enough to think that they can change the world,

are the ones who do.

View this ad here.

This Myth Is Busted!

| Comments

Link: Encyclopedia: MythBusters Episodes.

I love this show. It’s one of the cooler shows on TV right now.
Did I mention I saw Jamie Hyneman when I was in San Francisco? E had to help me contain my excitement. IT WAS SO FRIGGING COOL! Probably the most interesting thing that happened to me while I was down there.

Oh Haskell! I Can See You Too

| Comments

Link: Visual Haskell.

When I said Haskell felt more academic…. Forget what I said. I fully expect the Haskell Enterprise Edition out any day now. It’ll probably have a cheesy quote to go along with it like: “Haskell - The purely lazily strongly statically typed choice for the monadic enterprise”.

I’ve Wanted to Learn This for Awhile

| Comments

Tried to learn this back in 2000-2001 but didn’t get very far. Ocaml has a lot of similarities to Haskell. The learning resources available on the web are better now (http://www.ocaml-tutorial.org is excellent). I also spent time looking at the ocaml website and the OReilly Ocaml book (available on line, google for it!) Conceptually similar, both are strong statically typed functional programming languages. I actually prefer the Ocaml syntax after mucking with it for awhile. Haskell has this idea of a layout which makes the program sensitive to indentation ala Python. It’s not so bad once you get used to it but I got bit by it when I started out with Haskell. Ocaml appears to be a more pragmatic, working language (it has imperative features, better libraries) where Haskell tends to be more academic (though Pugs and Darcs are written in Haskell). It’s worthwhile to learn both languages as each bends and twists your mind in different ways.

A Little Camel Said to Me, “Are You Being Objective?”

| Comments

1. Write a function merge_i which takes as input two integer lists sorted inincreasing order and returns a new sorted list containing the elements ofthe first two.

This is one of the exercises in chapter 2 of Developing Applications with Objective Caml. Took me a little while since I don’t use a lot of recursion in my day job. On my first pass, I nailed the base case but the recursive case I messed up. I discovered this handy little trick when using the interpreter.

Objective Caml version 3.08.3

# use “test.ml”
val merge_i : int list -> int list -> int list = <fun>
# #trace merge_i;;
merge_i is now traced.
# merge_i [1;2] [3;4];;
merge_i <– [1; 2]
merge_i –> <fun>
merge_i* <– [3; 4]
merge_i <– [2]
merge_i –> <fun>
merge_i* <– [3; 4]
merge_i <– []
merge_i –> <fun>
merge_i* <– [3; 4]
merge_i* –> [3; 4]
merge_i* –> [2; 3; 4]
merge_i* –> [1; 2; 3; 4]
- : int list = [1; 2; 3; 4]
#

Aha! Initiallly, I was only passing the tail of the two lists on the recursive call. I needed to modify it to pass the tail of the list which I had used and pass the other list untouched. That trace feature in theinterpreter sure is handy (I believe lisp and ruby also have this in their REPL).

The solution came to this:

let rec merge_i (x:int list) (y:int list) =    if x = [] && y = [] then []    else if x = [] then y    else if y = [] then x    else let a = List.hd x         and b = List.hd y         in if a > b             then b :: merge_i x (List.tl y)            else a :: merge_i (List.tl x) y  ;;

(* in the last else, I had done (if a > b then b else a) :: merge_i (List.tl x) (List.tl y) initially *)
(* the if x = [] && y = [] then [] is probably superfluous but it’s late and I’m going to sleep *)

P.S (* *) is an OCaml comment. Very /* */ if you ask me.