Primarily technical blog on Lisp, .NET, C# development.

Tuesday, May 19, 2009

Outside-in

I am trying to read OnLisp by Paul Graham again and I really liked the turn your imperative code outside-in in chapter 3.

This is almost a reminder to myself.

Let's say I have an imperative function:
(defun imp-fn (x)
(let ((a 2) result)
(setf result (+ x a))
result)

I have seen a lot of code like that.

Turning it outside-in:
1) It returns result what is result?
we can replace it by the expression: (+ x a)
2) what is a?
we can replace it by its expression: (+ x 2)
3) then the result is:
(defun not-so-imp (x)
(+ x 2))

This is a very simple example but the idea remains the same. The OnLisp book has another example which is better than this one, but this one is good enough to show off the idea.

No comments: