Lisp,Python and Java

While acting on my new year resolutions, I started reading the SICP book .
The book focuses on the aspects of computer programming fundamentals and making the reader learn this by problem solving through LISP  programming language.

So in the first chapter i had to solve some problems , and one of them was :

Define a procedure that takes three numbers as arguments and returns the sum of the squares of the two larger numbers.

So my implementation of this problem in LISP is here :

(defun square(x) (* x x) )
(defun sum-of-square(x y) (+ (square x) (square y)))
(defun grt(a b) (if(> a b) a b))
(defun grt-2-sum-of-square (a b c )
(sum-of-square (grt a b) (grt b c) )
)
(grt-2-sum-of-square 1 2 3)

I implemented the same problem with python as following :


#Returns Square
def square(x):
return x*x
#Returns Sum of the Squares
def sum_of_square(x,y):
return square(x)+square(y)
#Returns greater number
def grt(a,b):
if (a>b):
return a
else:
return b
#Returns the sum fo square of greater numbers
def grt_2_sum_of_square(x,y,z):
a=grt(x,y)
b=grt(y,z)
return sum_of_square(a,b)
# If main mein module is called
if __name__==”__main__”:
#print result
print grt_2_sum_of_square(1,2,3)

and implementation of same problem in Java goes like this :


public class Test {
// Returns the Square
public int square(int x){
return x*x;
}
// Returns sum of the Squares
public int sum_of_square(int x,int y){
return square(x)+square(y);
}
//Returns greater number
public int grt(int x,int y){
return (x>y)?x:y;
}
//Return the sum fo square of greater numbers
public int grt_2_sum_of_square(int x,int y,int z){
int a=grt(x,y);
int b=grt(y,z);
return sum_of_square(a,b);
}
// Main Method
public static void main(String a[]){
//instantiates class
Test t=new Test();
int res=t.grt_2_sum_of_square(1, 2, 3);
System.out.print(res);
}
}

There are easier ways of implementing it in python and Java though . But i preferred these as i implemented the same LISP construct. I am amused by the simplicity and cleanliness of LISP code here and no. of lines i had to write for doing this.

I wish i should have been taught LISP in school and i would have been a better programmer :)

Python gives me pleasure of code elegance and simplicity, but Java is the one i am working with . The one that got me a Job.

Learning new things make me feel better, that there is no end to learning.

Powered by ScribeFire.


  1. paddy3118

    Hi Jasdeep,
    That Python solution truly awful :-) Sorry to say it but I considered being more circumspect, and I can see that you wanted to code it like the Lisp solution, but given the problem and a chance to solve it without reference to solutions on other languages then I would write something like:

    >>> def sumofmax2(a,b,c): return sum(sorted([x*x for x in [a,b,c]])[-2:])

    >>> sumofmax2(2,1,3)
    13
    >>>

    Where I have kept the function signature quite like your Lisp one.
    A further Python idiom would be to allow two or more arguments and find the sum of the two largest like this:

    >>> def sumofmax2(*args): return sum(sorted([x*x for x in args])[-2:])

    >>> sumofmax2(2,1,3)
    13
    >>> sumofmax2(2,-5,1,3)
    34
    >>>

    I am not sure of how much Python you know, but lists are very useful things in Python :-)

    - Paddy.

  2. ਜਸਦੀਪ

    Hi Paddy..

    Thanks for sharing your thoughts.
    I appreciate your criticism. Yes, i am a python beginner , I have read more python literature than practicing it. I will now practice it more and more.

    Python is really great , it did the same thing in one line .

    -Jasdeep

  3. Chris

    You could actually get that LISP solution down to just 3 lines of code:

    (define (grt-2-sum-of-square x y z)
    (define (square n) (* n n))
    (+ (square (max x y)) (square (max (min x y) z))))

Leave a Comment