Sunday, December 30, 2007

3 liner quicksort


qsort([]) -> [];
qsort([Pivot|T]) ->
qsort([X || X <- T, X < Pivot]) ++ [Pivot] ++ qsort([X || X <- T, X >= Pivot]).


Here is some explanation of above code.
- When the input list is empty, output empty list.
- Otherwise, divide the input list into 2 lists such that, one list will have all the elements whose value are less than Pivot and the other list will have all the elements having values greater than or equal to Pivot. 1st element from input list is a Pivot.
- Do above steps recursively on all the lists that you create and then join them keeping Pivot in middle of lists.

I think the explanation is way too complicated than the code itself ;)

Day by day, I have started realizing the power of pattern matching, list data structure and functional programming.

Note: Above code is taken from Programming Erlang. Similar code is possible with other FP languages like haskell.

Enjoy!

Thursday, December 27, 2007

Monday, May 28, 2007

My friend Soumitra has started writing his gtalk status messages in a blog. He picks very interesting small small sentences for this. Mostly in marathi. Some of them are "too good".

Blog is named संक्षिप्त संदेश! And, interesting thing for me is, he writes this using paahijen's Scratchpad.

So, Enjoy the posts!

Friday, April 27, 2007

data to dollars

data => $s?
information => more $s?
knowledge => much more $s?

Only thing is, its hard to predict when '=>' will happen. :)

Wednesday, April 18, 2007

getting message in

Getting the message in is as important as getting the message out!

Very well said Jason

Friday, April 06, 2007

promoting

Getting Real is a very interesting book to read and do. Its not just some "theory" thing, its practical.

In chapter13, author mentions
The best promotional tool is a great product. Word will get out if you've got an app that people find really useful.

Still, you need an ace promotional site too.

This just makes sense.

Wednesday, April 04, 2007

sony book reader

Sony now has this new and cool device. Its called "Sony Reader". I remember there are many companies who have tried the same, but the main differentiator is the use of e-ink technology. I will always prefer a paper like display over CRT, LCD, TFT for reading.

At a first glance looks of the device, features and specification looks good to me. Still, if Sony comes up with features like undelining some lines using stylus while reading the book, looking at what all points I have underlined, bookmarking and index of my bookmarked pages, I will be happy to buy one.

Don't know if these features are already supported, but I couldn't see them listed on those pages.

Thursday, March 29, 2007

हर हर महादेव

हिंदी मे गूगल आया रे ...

Finally Google comes into transliteration space. This show how important local language text is.
Still ... I love ScratchPad on paahijen. Its much more intuitive.

Wednesday, March 28, 2007

lambda magic


(define (mcons x y)
(lambda (p)
(cond ((= p 1) x)
((= p 2) y))))

(define (mcar x) (x 1))

(define (mcdr x) (x 2))

; Using above procedures
(define a (mcons 11 33))
(mcar a)
(mcdr a)


Some excerpts from SICP on above way of implementation.

We never actually said what a pair was, only that the language supplied procedures cons, car, and cdr for operating on pairs. But the only thing we need to know about these three operations is that if we glue two objects together using cons we can retrieve the objects using car and cdr. That is, the operations satisfy the condition that, for any objects x and y, if z is (cons x y) then (car z) is x and (cdr z) is y. Indeed, we mentioned that these three procedures are included as primitives in our
language. However, any triple of procedures that satisfies the above condition can be used as the basis for implementing pairs. This point is illustrated strikingly by the fact that we could implement cons, car, and cdr without using any data structures at all but only using procedures.

This procedural implementation of pairs is a valid implementation, and if we access pairs using only cons, car, and cdr we cannot distinguish this implementation from one that uses ``real'' data structures.

The point of exhibiting the procedural representation of pairs is not that our language works this way (Scheme, and Lisp systems in general, implement pairs directly, for efficiency reasons) but that it could work this way.

This example also demonstrates that the ability to manipulate procedures as objects automatically provides the ability to represent compound data. This may seem a curiosity now, but procedural representations of data will play a central role in our programming repertoire. This style of programming is often called message passing.

Tuesday, March 27, 2007

web - going in spirals

Mr. Abhijit makes very good point here of web going in spirals and not in circles. One word changes the whole meaning!
Really, Think of Ajax as the one step ahead to the same old "client-server" as a concept.

Very cool observation, Abhijit!

advertise your website

I have seen some posts on paahijen.com (TagWise) which advertise "Solar eclipse photos" taken by someone.

Its a interesting way to attract people to see your work and nice opportunistic advertisement. :)

See those posts:
Solar Eclipse - March 19 2007
Solar Eclipse

Enjoy !

Friday, March 16, 2007

pascal's triangle

While reading SICP recently, I came across a exercise which asks reader to generate Pascal's Triangle using recursion.

There are many interesting observations about values that get generated in this triangle. One thing I noticed is, sum all the values generated at each level = 2^(level-1) if you start counting levels from 1.

So I thought, lets give it a try .. and it was not difficult to come up with this version.

# Pascal's triangle

# store to avoid recomputation of the same values
$val_store = {}

def getval(row, position)
val = 0
if position <= 0 or row <= 0 or position > row
val = 0
end
if position == 1 or row == 1 or row == position
val = 1
else
# first check in the store
if $val_store["#{row}:#{position}"]
val = $val_store["#{row}:#{position}"]
else
val = getval(row - 1, position - 1) + getval(row - 1 , position)
$val_store["#{row}:#{position}"] = val
end
end
return val
end

def get_row_vals(row)
row_vals = []
i = 0
row.times {
i = i + 1
row_vals << getval(row, i)
}
return row_vals
end

rows = ARGV[0].to_i || 20
i = 1
rows.times {
sum = 0

row_vals = get_row_vals i
row_vals.each { |val|
sum += val
}
puts "Sum: #{sum} Row #{i}: #{row_vals.inspect}"
i = i + 1
}
puts "Store size: #{$val_store.length}"


Next thing ... try to implement using lisp?