Friday, March 20, 2009

SICP Problems

Hello All~

Off late besides playing around with Haskell, I started reading the classic "Structured Interpretation Of Computer Programs".
I have been working with Scheme for a week now and am blown away by the intuitiveness and simplicity of the language.

What I don't really understand, is that why don't more people use languages like Haskell and Scheme. It more fun and productive, I guess it's got something to do with the guys that sponsor the popular languages. Sun and IBM for Java, C# for Microsoft.

Anyway's here's a problem from SICP that I was working on yesterday. Thought of sharing the Scheme solution with all.

Problem: Define a procedure that takes three numbers as arguments and returns the sum of sqaures of 2 larger numbers.

#| Function to find the min value |#
(define (mins x y)
(if (> x y) y x))
#| Function to find max value |#
(define (maxs x y)
(if (> x y) x y))

; Function to find the square of a number
(define (square x) (* x x))

;Function to find the sum of sqaure
(define (sum-of-square x y) (+ (square x) (square y)))

#|
The logic we apply to find the 2 larger numbers out of 3 numbers is as follows.
suppose the 3 numbers are a b and c.
Firstly we find the greater number between a and b, using the maxs function that
we have already defined. Now we find the mins(a b) and the number genrated from this
is compared with c using the maxs.

Let us take an example.

sum-of-square-2-greatest 30 20 10

Here a=30, b=20 and c=10

maxs (a b) = 30

d=mins (a b) = 20

max (d, 10) = 20

Hence we will find the sum-of-square of these 2 numbers.

|#

(define (sum-of-square-2-greatest a b c)
(sum-of-square (maxs a b) (maxs (mins a b) c))
)

Friday, September 12, 2008

remove first element from list in Haskell

Hello All~

As you are aware, I am presently reading the beautiful book "The Haskell Road to Logic Maths and Programming" and another book called the "black Swan - The Impact of the highly Improbable" by Nassim Nicholas Taleb. As I have just started reading it yesterday, I will include my views on the book in the coming days.
However what I plan to do today is solve the exercise 1.10 from the haskell book, so here is the problem with my explanation.
Problem: Define a function removeFst that removes the first occurrence of an integer m, from a list of integers. If m does not occur in the list, the list remains unchanged.
For example let us consider the list [1,2,3,4,5,6,2], so if we call the program thus


removeFst 2 [1,2,3,4,5,6,2]


We should get the list [1,3,4,5,6,2] as return. Only the first occurrence of 2 is removed, any other 2 in the list is left unchanged.
I will present 2 solutions.
Solution 1: removes the first occurrence of 2.
Solution 2: removes all occurrences of 2.

Solution 1:

--Test for an empty list, if an empty list throw error.
removeFst n []=error "The list is empty "
--Cover the condition in case there is only 1 element in the array
removeFst n [x]|n==x=[] --Case n==x, show a blank array as output
|otherwise=[x]
--Handle the usual case, in case x==n, we return the tail else
--create a new array and append x recursively.
removeFst n (x:xs)|n==x=xs
|otherwise=x:removeFst n xs


Solution 2:

--Test for an empty list, if an empty list throw error.
removeFst n []=error "The list is empty "
--Cover the condition in case there is only 1 element in the array
removeFst n [x]|n==x=[] --Case n==x, show a blank array as output
|otherwise=[x]
--Handle the usual case, in case x==n, we return the tail else
--create a new array and append x recursively.
removeFst n (x:xs)|n==x=removeFst n xs --Do nothing and move to the next element
|otherwise=x:removeFst n xs