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?