Archive for February, 2007

Wondrous Functional Languages

Posted in programming on February 27th, 2007 by Christopher Owen – 1 Comment

My colleague Matt wrote about a recent Ruby Quiz which emphasised short, elegant solutions. He was particularly impressed with a solution which used a property of Ruby hashes to generate a wondrous number sequence for any given integer in a single line of code.

That got me thinking about the solution in Scheme, a language I find very appealing, and Haskell, which I had started picking up a few months ago but had to abandon due to lack of time. Even so I managed to remember enough to get this basic solution within a few minutes:

wondrous :: Int -> [Int]
wondrous 1 = [1]
wondrous x
	| even x = x : wondrous (x `div` 2)
	| otherwise = x : wondrous (3*x+1)

Here is the most obvious implementation in Scheme:

(define wondrous
  (lambda (x)
    (cond
      ((= x 1)(1))
      ((= (modulo x 2) 0) (cons x (wondrous (quotient x 2))))
      (else (cons x (wondrous (+ (* 3 x) 1))))
    )
  )
)

As you can see both of these functional languages are really suited to expressing such algorithms, particularly Haskell with its excellent pattern matching expressions. I have no doubts that there are far more elegant solutions in both languages; I’ll post them if I get suitably inspired.

Felis Chrisus

Posted in Uncategorized on February 25th, 2007 by Christopher Owen – Be the first to comment

In some aspects of my life I feel like the proverbial cat: when I am inside I want to be outside, and when I am out I want to be in. I know not from where this decisive indecision comes from. Often I find I can oscillate between my two states of indecision freely, some mad pendulum motivated by chaos alone it would seem. The thing that gets me though is sometimes, just sometimes, I walk through the door to the other side, turn around, and find the door shut and unyielding; locked forever and never to be open again no matter how longingly I peer through the window.

Then one day, beyond prediction and imagination and without context: a house is built around me while I thought I was outside, giving me two places I want to be rather than here. A new turmoil to be sure; one that leaves me reeling: wondrous, breathless and afraid.