A Small Note On Scheme:-
I got a chance to read the famous text book of Structure and Interpretation of Programs (SICP)written by Harold Abelson and Gerald Jay Sussman. They are professors of Massachusetts Institute of Technology (MIT). This book contain everything about the functional programming language –Scheme. Scheme is one of the two main dialects of the programming language Lisp. At First I felt some difficulty to learn but later I realized that it was too good to learn and was rather interesting too. This book helps us to learn modular programming.
I read the first chapter of SICP and have also done some exercises. Let us start from the basics of Scheme. To start we need a mit-scheme interpreter , it can be installed by using this command sudo apt-get install mit-scheme.
We can take mit-scheme interpreter by just typing mit-scheme in terminal and it will look like this.
Scheme uses prefix notation and the basic arithmetic operations will look like this.
Here the things are named by using define for example
1]=> (define numb 4)
Then the value of numb will be 4.
Compound Procedures:
Compound operation can be given name and then referred to as unit. For example
1]=>(define (square x) (* x x))
This means that
The general form of procedure definition is
(define (<name> <formal parameters>) <body>)
We can use defined square to find square of a number. For example
1]=> (square 2)
;value: 4
Conditional Expressions:
Conditional expressions are of the form
(cond (<p1> <e1>) (<p2> <e2>) (<pn> <en>)) For example to find the absolute value of a number by using conditional expression. 1]=>(define (abs x) (cond ((< x 0) (- x)) (else x)))
The above code defines that if (x < 0) then the value of x will be -x
otherwise the value of x will be x.
After defining this we can find the absolute value by just giving
(abs -4).
Logical operations like and, or, not are also there in scheme.
The general form of these are
(and <exp1> … <expn>)
(or <exp1> … <expn>)
(not <exp>)
Procedures:
Procedures in Scheme are first class, meaning that they can be
passed as arguments,bound to variables and returned as values from
other procedures. Arguments are passed by value.
Linear Recursion and Iteration:
We can take an example to study about recursion and iteration.
The best example is that to find factorial of a number.
(define (factorial n) (if (= n 1) 1 (* n (factorial (- n 1)))))
The above code shows how to find factorial of a number by recursion.
(define (factorial n) (fact-iter 1 1 n)) (define (fact-iter product counter max-count) (if (> counter max-count) product (fact-iter (* counter product) (+ counter 1) max-count)))
The above code shows how to find factorial of a number by iterative method.
The codes of exercises that I have done is available here .