Lisp Programming Using DR Scheme
Lisp Programming Using DR Scheme
Scheme’s booleans are #t for true and #f Scheme integers need not be specified in
for false. Scheme has a predicate decimal (base 10) format. They can be
procedure called boolean? that checks if its specified in binary by prefixing the
argument is boolean. numeral with #b. Thus #b1100 is the
SANDEEP KUMAR http://san24mca.blogspot.com/
The procedure not negates its argument, Numbers can tested for equality using the
considered as a boolean. general-purpose equality predicate eqv?.
'E Now
The characters in a given string can be Here’s a way to create a vector of the first
individually accessed and modified. The five integers:
procedure string-ref takes a string and a (0-
based) index, and returns the character at (vector 0 1 2 3 4)
that index: => #(0 1 2 3 4)
Numbers can be converted to strings: The procedures we have seen thus far are
primitive procedures, with standard global
(number->string 16) => "16" variables holding them. Users can create
additional procedure values.
Strings can be converted to numbers. If the
string corresponds to no number, #f is Yet another data type is the port. A port is
returned. the conduit through which input and
output is performed. Ports are usually
(string->number "16") associated with files and consoles.
=> 16
In our “Hello, World!” program, we used
(string->number "Am I a hot number?") the procedure display to write a string to
=> #f the console. display can take two
arguments, one the value to be displayed,
string->number takes an optional second and the other the output port it should be
SANDEEP KUMAR http://san24mca.blogspot.com/
Forms 3.1 Procedures
The reader will have noted that the We have seen quite a few primitive
Scheme example programs provided thus Scheme procedures, eg, cons, string->list,
far are also s-expressions. This is true of and the like. Users can create their own
all Scheme programs: Programs are data. procedures using the special form lambda.
For example, the following defines a
Thus, the character datum #\c is a procedure that adds 2 to its argument:
program, or a form. We will use the more
general term form instead of program, so (lambda (x) (+ x 2))
that we can deal with program fragments
too. The first subform, (x), is the list of
parameters. The remaining subform(s)
Scheme evaluates the form #\c to the value constitute the procedure’s body. This
#\c, because #\c is self-evaluating. Not all procedure can be called on an argument,
s-expressions are self-evaluating. For just like a primitive procedure:
instance the symbol s-expression xyz
evaluates to the value held by the variable ((lambda (x) (+ x 2)) 5)
xyz. The list s-expression (string->number => 7
"16") evaluates to the number 16.
SANDEEP KUMAR http://san24mca.blogspot.com/
3.3 Sequencing
(define area *)
We used the begin special form to bunch
3.1.2 Variable number of arguments together a group of subforms that need to
be evaluated in sequence. Many Scheme
Some procedures can be called at different forms have implicit begins. For example,
times with different numbers of let’s define a 3-argument procedure that
arguments. To do this, the lambda displays its three arguments, with spaces
parameter list is replaced by a single between them. A possible definition is:
symbol. This symbol acts as a variable that
is bound to the list of the arguments that (define display3
the procedure is called on. (lambda (arg1 arg2 arg3)
(begin
(display arg1)
(display " ") (if (< p 90)
(display arg2) 'low-pressure) ;no ``else'' branch
(display " ") => low-pressure
(display arg3)
(newline)))) Scheme provides some other conditional
forms for convenience. They can all be
In Scheme, lambda-bodies are implicit defined as macros (chap 8) that expand
begins. Thus, the begin in display3’s body into if-expressions.
isn’t needed, although it doesn’t hurt.
display3, more simply, is: 4.1 when and unless
else-branch)
(if (< (pressure tube) 60)
If test-expression evaluates to true (ie, any (begin
value other than #f), the “then” branch is (open-valve tube)
evaluated. If not, the “else” branch is (attach floor-pump tube)
evaluated. The “else” branch is optional. (depress floor-pump 5)
(detach floor-pump tube)
(define p 80) (close-valve tube)))
The cond form is convenient for Scheme provides special forms for boolean
expressing nested if-expressions, where conjunction (“and”) and disjunction (“or”).
each “else” branch but the last introduces a (We have already seen (sec 2.1.1)
new if. Thus, the form Scheme’s boolean negation not, which is a
procedure.)
(if (char<? c #\c) -1
(if (char=? c #\c) 0 The special form and returns a true value if
1)) all its subforms are true. The actual value
returned is the value of the final subform.
can be rewritten using cond as: If any of the subforms are false, and
returns #f.
(cond ((char<? c #\c) -1)
((char=? c #\c) 0) (and 1 2) => 2
(else 1)) (and #f 1) => #f
SANDEEP KUMAR http://san24mca.blogspot.com/
The cond is thus a multi-branch The special form or returns the value of its
conditional. Each clause has a test and an first true subform. If all the subforms are
associated action. The first test that false, or returns #f.
succeeds triggers its associated action. The
final else clause is chosen if no other test (or 1 2) => 1
succeeded. (or #f 1) => 1
The cond actions are implicit begins. Both and and or evaluate their subforms
left-to-right. As soon as the result can be
4.3 case determined, and and or will ignore the
remaining subforms.
(and 1 #f expression-guaranteed-to-cause-
error) The form set! modifies the lexical binding
=> #f of a variable.
As with lambda, within the let-body, the (let ((cons (lambda (x y) (+ x y))))
local x (bound to 1) shadows the global x (cons 1 2))
(which is bound to 20). => 3
Inside this let body, the lexical variable new lexical variables like let does. It
cons adds its arguments. Outside, cons modifies the bindings of existing lexical
continues to create dotted pairs. variables, and the modification ceases as
soon as the fluid-let does.
5.2 fluid-let
To drive home this point, consider the
Here is a definition of a rather more program
complicated macro, fluid-let . fluid-let
specifies temporary bindings for a set of (let ((counter 99))
already existing lexical variables. Given a (display (bump-counter)) (newline)
fluid-let expression such as (display (bump-counter)) (newline)
(display (bump-counter)) (newline))
(fluid-let ((x 9) (y (+ y 1)))
(+ x y)) which substitutes let for fluid-let in the
previous example. The output is now
A lexical variable is visible throughout its
scope, provided it isn’t shadowed. 4
Sometimes, it is helpful to temporarily set 5
a lexical variable to a certain value. For 6
this, we use the form fluid-let.
i.e. the global counter, which is initially 3,
(fluid-let ((counter 99)) is updated by each call to bump-counter.
(display (bump-counter)) (newline) The new lexical variable counter, with its
(display (bump-counter)) (newline) initialization of 99, has no impact on the
(display (bump-counter)) (newline)) calls to bump-counter, because although
the calls to bump-counter are within the
This looks similar to a let, but instead of scope of this local counter, the body of
shadowing the global variable counter, it bump-counter isn’t. The latter continues to
temporarily sets it to 99 before continuing refer to the global counter, whose final
with the fluid-let body. Thus the displays value is 6.
in the body produce
SANDEEP KUMAR http://san24mca.blogspot.com/
counter => 6
100
101 fluid-let is a nonstandard special form.
102
overhead.
(reverse! is a useful enough procedure that
Scheme does this by a process called tail- it is provided primitively in many Scheme
call elimination. If you look closely at the dialects, eg, MzScheme and Guile.)
countdown procedure, you will note that
when the recursive call occurs in
countdown’s body, it is the tail call, or the 6.4 Mapping a procedure across a list
very last thing done — each invocation of
countdown either does not call itself, or A special kind of iteration involves
when it does, it does so as its very last act. repeating the same action for each element
To a Scheme implementation, this makes of a list. Scheme offers two procedures for
the recursion indistinguishable from this situation: map and for-each.
The map procedure applies a given
procedure to every element of a given list, Scheme’s reader procedures take an
and returns the list of the results. Eg, optional input port argument. If the port is
not specified, the current input port
(map add2 '(1 2 3)) (usually the console) is assumed.
=> (3 4 5)
Reading can be character-, line- or s-
The for-each procedure also applies a expression-based. Each time a read is
procedure to each element in a list, but performed, the port’s state changes so that
returns void. The procedure application is the next read will read material following
done purely for any side-effects it may what was already read. If the port has no
cause. Eg, more material to be read, the reader
procedure returns a specific datum called
(for-each display the end-of-file or eof object. This datum is
(list "one " "two " "buckle my shoe")) the only value that satisfies the eof-object?
predicate.
has the side-effect of displaying the strings
(in the order they appear) on the console. The procedure read-char reads the next
character from the port. read-line reads the
The procedures applied by map and for- next line, returning it as a string (the final
each need not be one-argument newline is not included). The procedure
procedures. For example, given an n- read reads the next s-expression.
argument procedure, map takes n lists and
applies the procedure to every set of n of 7.2 Writing
arguments selected from across the lists.
Eg, Scheme’s writer procedures take the object
that is to be written and an optional output
(map cons '(1 2 3) '(10 20 30)) port argument. If the port is not specified,
=> ((1 . 10) (2 . 20) (3 . 30)) the current output port (usually the
console) is assumed.
(map + '(1 2 3) '(10 20 30))
SANDEEP KUMAR http://san24mca.blogspot.com/
The procedure newline starts a new line on Assume the file greeting.txt does not exist
the output port. before the following programs are fed to
the listener:
7.3 File ports
(define o (open-output-file "greeting.txt"))
Scheme’s I/O procedures do not need a
port argument if the port happens to be (display "hello" o)
standard input or standard output. (write-char #\space o)
However, if you need these ports (display 'world o)
explicitly, the zero-argument procedures (newline o)
current-input-port and current-output-port
furnish them. Thus, (close-output-port o)
After you have performed I/O on a port, procedure is applied to an input port
you should close it with close-input-port or opened on the file. When the procedure
close-output-port. completes, its result is returned after
ensuring that the port is closed.
In the following, assume the file hello.txt
contains the single word hello. (call-with-input-file "hello.txt"
(lambda (i)
(define i (open-input-file "hello.txt")) (let* ((a (read-char i))
(b (read-char i))
(read-char i) (c (read-char i)))
=> #\h (list a b c))))
=> (#\h #\e #\l)
(define j (read i))
The procedure call-with-output-file does
the analogous services for an output file. String ports need not be explicitly closed.
It is often convenient to associate ports We have already seen the procedure load
with strings. Thus, the procedure open- that loads files containing Scheme code.
input-string associates a port with a given Loading a file consists in evaluating in
string. Reader procedures on this port will sequence every Scheme form in the file.
read off the string: The pathname argument given to load is
reckoned relative to the current working
(define i (open-input-string "hello world")) directory of Scheme, which is normally the
directory in which the Scheme executable
(read-char i) was called.
=> #\h
Files can load other files, and this is useful
(read i) in a large program spanning many files.
=> ello Unfortunately, unless full pathnames are
used, the argument file of a load is
(read i) dependent on Scheme’s current directory.
=> world Supplying full pathnames is not always
convenient, because we would like to
The procedure open-output-string creates move the program files as a unit
an output port that will eventually be used (preserving their relative pathnames),
to create a string: perhaps to many different machines.
a) 7 *(7 + 5) /9
(* 7 ( /(+ 4 5) 9))
SANDEEP KUMAR http://san24mca.blogspot.com/
b)10 * (5 -3) + 8
(+ (* 10 ( - 5 3 )) 8)
c)10+7*15/5
(+ 9 ( * 7 (/ 15 5)))
( + ( - 9 7 ) ( * ( - 6 2 ) ( + 4 7 )))
3.Define two variables ‘a’ and ’ b’ and assign two values to it . Then add
and multiply the two variables to get the result.
(begin
(define a 5)
(define b 4)
(define ( add a b) ( + a b ))
)
Output
> ( add a b)
9
4.Define three variables a,b,c and assign integer values into it. Then evaluate
the expression (a +b) +(c*c)/b
(begin
(define a 5)
(define b 4)
(define c 6)
(define ( sol a b c ) ( + (+ a b )(/ ( * c c ) b)))
)
Output
> ( sol a b c )
18
5.Write a Scheme procedure to display the sum and average of three numbers.
SANDEEP KUMAR http://san24mca.blogspot.com/
Output
> (avg 4 8)
6
ASSINGMENT -2
1 Evaluate the following expression:
5 + ⅓ + (2 - (3 - (6 + ⅓)))
2)Utopias taxes accountants always use programs that compute income taxes
even though the tax rate is a solid never -changing 15% . define the program
tax , which determines the tax on the gross pay also define net pay . the
program determines the net pay of an employee from the number of hours
worked . Assume an hourly rate of $12.
(define (tax nhour )(/(*(* nhour 12) 15 ) 100))
(define (netpay nhour) (display "net pay is $") (-(* nhour 12) (tax nhour)))
(tax 5)
Output
9
> (netpay 5)
net pay is $51
3.An old style movie theater has simple profit program . each customer pay
$5 per ticket .every performance costs the theater $20 ,plus $50 per
attendee, .develop the program total profit it consumes the number of
attendees (of a show ) and produces how much income the attendees produce
.
(begin
(define (attende x )
(define a 0)
(set! a x)
SANDEEP KUMAR http://san24mca.blogspot.com/
Output
> (attende 5 )
total no of attende 5
profit $2.5
attende produce $ 25
4) To find the volume & area of a cylinder which consumes the radius of a
cylinder base disk and its height.
(define (cylinder r h)
(display "Area::")
(display(+ (* 3.14 r h)(* 3.14 r r)))
(display "sq. unit \n Volume::")
(display (* 3.14 r r h))
(display "cubic unit")
)
Output
> (cylinder 3 5)
Area::75.36sq. unit
Volume::141.29999999999998cubic unit
(begin
(define (max a b c)
(define m 0)
(if (and (> a b)
(> a c))
(set! m a))
(if ( and (> b a)
(> b c))
(set! m b))
(if (and ( > c a)
(> c b))
SANDEEP KUMAR http://san24mca.blogspot.com/
(set! m c))
(display "max is ")
(display m)
))
Output
> (max 45 25 5)
max is 45
> (max 34 56 4)
max is 56
> (max 3 56 67)
max is 67
6)Write a program to find the minimum of threee numbers
(begin
(define (min a b c)
(define m 0)
(if (and (< a b)
(< a c))
(set! m a))
(if ( and (< b a)
(< b c))
(set! m b))
(if (and ( < c a)
(< c b))
(set! m c))
(display "minimum is ")
(display m)
))
Output
> (min 2 4 5)
minimum is 2
> (min 56 5 7)
minimum is 5
> (min 34 56 4)
minimum is 4
Output
> (interest 5643)
The Interest is = 282.15
> (interest 2643)
The Interest is = 118.935
> (interest 643)
The Interest is= 25.72
ASSINGMENT -3
Use of car and cdr :
(define (vld_num lst)
(let a ((templst lst))
(if (null? templst) #t
(begin
(if (number? (car templst)) (a (cdr templst))
#f)))))
(define (odd n)
(define i 1)
(let loop()
SANDEEP KUMAR http://san24mca.blogspot.com/
(if (< i n)
(begin
(display i) (display "\n")
(set! i (+ i 2))
(loop)))))
Output
> (odd 7)
1
3
5
2) Write a program to print the given pattern:
1
12
123
1234
(define (pattern n)
(define i 1)
(define l 1)
(let loop()
(if (<= l n)
(begin
(let loop1()
(if (<= i l)
(begin
(display i) (set! i (+ i 1)) (loop1))
(begin
(newline) (set! i 1))))
(set! l (+ l 1)) (loop)))))
Output
> (pattern 6)
1
12
123
1234
12345
123456
SANDEEP KUMAR http://san24mca.blogspot.com/
3) A local discount store has a policy of putting labels with dates on all of its
new merchandise . If as item has not sold within two weeks the store
discounts the item by 25% for the third week , 50% for the fourth week ,
and 75% for the fifth week . After that no additional discount are given .
Develop the function new-price , which takes the initial price of an item
and the number of wiiks since the item was dated and produce the selling
price of the item.
(define (new_price sp wk)
(if (= wk 3)
(begin
(set! sp (- sp (* 0.25 sp)))))
(if (= wk 4)
(begin
(set! sp (- sp (* 0.50 sp)))))
(if (> wk 4)
(begin
(set! sp (- sp (* .75 sp)))))
sp)
Output
> (new_price 3 4)
1.5
> (new_price 3 7)
0.75
> (new_price 3 3)
2.25
Output
> (products 9 6)
180
5 , 6) Develop a function to print the largest element in a list of number and also
find the position of largest number.
Output
> (define lis(list 3 23 4 23))
> (comp_list lis)
The greatest number is:23 position=2
(define (fibonacci n)
(define p 0)
(define c 1)
(define nxt 1)
(define i 2)
(display p)(display " ")(display c)
(let loop()
(if (< i n)
(begin
SANDEEP KUMAR http://san24mca.blogspot.com/
(set! p c) (set! c nxt) (set! nxt (+ c p)) (display " ") (display c) (set! i (+ i 1))
(loop)))))
Output
> (fibonacci 6)
011235
8) Enter three number through list and then display their sum.
(define (sum_list lst)
(define s 0)
(define i 0)
(define l (length lst))
(let loop()
(if (< i l)
(begin
(set! s (+ s (list-ref lst i))) (set! i (+ i 1)) (loop))))
(display "sum =") s)
Output
> (define lst(list 3 23 4 23))
> (sum_list lst)
sum =53
Output
> (define lst(list 3 23 4 23 45 34 23 4 2 56 67 89 23))
> (search lst 23)
23 occurs 4 times.
SANDEEP KUMAR http://san24mca.blogspot.com/
(define (leap_year a)
( if (or (and (= (remainder a 4) 0) ( not (= (remainder a 100 ) 0))) (=
(remainder a 400 ) 0))
(display "leap year")
(display "not leap year")
))
Output
> (leap_year 1978)
not leap year
> (leap_year 2004)
leap year
(define (reverse n)
(define r 0)
(define s 0)
(let loop()
(if (> n 0)
(begin
ASSINGMENT -5
Do Loop
Syntax:
( Do ((initialization (iteration)))
((termination condition))
( body.......
.....
......
))
Example :
(do ((i 1(+ i 1)))
((> i 5))
(begin
(display i)
(newline))
) Test output ....yourself
(define (fact n)
SANDEEP KUMAR http://san24mca.blogspot.com/
(define b 1)
(do ((i n(- i 1)))
((< i 1))
(begin
(set! b (* b i))
)
) (display b))
Output
> (fact 5)
120
2. Print the series 1,1/2,1/3,1/4 …..upto 1/n using DO Loop.
(define (series n )
(do ((i 1(+ i 1)))
((> i n))
(begin
(display " ")
(display (/ 1 i))
)
))
Output
> (series 5)
1 1/2 1/3 1/4 1/5
Output
> (salary "sandeep" 22 4000)
Name=sandeep
Age=22
basic salary=4000 ta=1800 da=2200 hra=1400
salary(basic salary+ta+da+hra)= 9400
4. Input your name, branch and 4 paper marks as argument. Calculate total and
percentage. Calculate the grade as follows
90% and above Grade ‘O’.
80% and above Grade ‘A’
70% and above Grade ‘B’
60% and above Grade ‘C’
Display the suitable mark sheet.
Output
>(grade "san" "mca" 70 70 70 70)
Name=san
Branch=mca
total= 280
percent= 70.0
B grade
ASSINGMENT -6
1. Input principal, time, rate of interest from the key board and find simple
interest and absolute amount.
(display "enter pricipal value= ")
(define p(read))
(newline) (display "enter rate = ")
(define r(read))
(newline) (display "enter time= ")
(define t(read))
(define (SI p r t)
(define si 1)
(newline) (display "Principal= ") (display p)
(newline) (display "Rate= ") (display r)
(newline) (display "Time= ") (display t)
(set! si (/ (* p r t ) 100))
(newline) (display "SIMPLE INTEREST = ") (display si)
SANDEEP KUMAR http://san24mca.blogspot.com/
Output
enter pricipal value = 1000
enter time = 3
Principal= 1000
Rate= 45.5
Time= 3
SIMPLE INTEREST = 1365.0
AMOUNT= 2365.0
2. Find out the area of rectangle ,triangle and circle using appropriate input
from the key board.
Output
enter length of rectangle34
enter width of rectangle 45
length of rectangle= 34
SANDEEP KUMAR http://san24mca.blogspot.com/
Width of rectangle= 45
AREA OF RECTANGLE = 1530
(set! l (+ l 1))
(loop))))
Output
Enter the lower bound 4
SANDEEP KUMAR http://san24mca.blogspot.com/
Output
Enter the number of lines= 5
*
* *
* * *
* * * *
* * * * *
ASSINGMENT -7
Use of Read:
Example1 :
(define (disp)
(display "X value ")
(display x)
(display "\n y value") ; \n for (newline)
(display y)
)
(define p 0)
(define q 0)
Use of Write :
Example:
(write "enter x value ")
(define x(read)) ;reading from the key board
(write "enter y value ")
(define y(read)) ;reading from the key board
SANDEEP KUMAR http://san24mca.blogspot.com/
(define (disp)
(write "X value ")
(write x)
(newline)
(write "Y value ")
(write y)
)
Example1:
Lambda function :
Example1:
(define add2
(lambda (x)
(+ x 2)))
Output:
SANDEEP KUMAR http://san24mca.blogspot.com/
> (add2 5)
7
Example2:
(define area
(lambda (length breadth)
(* length breadth)))
Output:
>(area 5 7)
35
Example3:
(define display3
(lambda (arg1 arg2 arg3)
(display arg1)
(display " ")
(display arg2)
(display " ")
(display arg3)
(newline)))
Output
>(display3 5 6 7)
5 6 7
(define ( calculation x y)
(display (* x y))
(newline)
(display (+ x y))
(newline)
(display (- x y))
)
Execute:
> (calculation 12 10)
SANDEEP KUMAR http://san24mca.blogspot.com/
(define calculation
( lambda (x y)
(display (* x y))
(display (+ x y))
(display (- x y))
))
Execute:
> (calculation 12 10)
1. Using a single lambda function calculate area and perimeter of rectangle,
square and circle
(define figures
(lambda (l b s r)
(display "Rectangle:\nArea=") (display (* l b))
(display "\tPerimeter=") (display (* (+ l b) 2 ))
(display "\nSquare:\nArea=") (display (* s s))
(display "\tPerimeter=") (display (* 4 s))
(display "\nCirle:\nArea=") (display (* 3.14 r r))
(display "\tPerimeter=") (display (* 2 3.14 r))))
Output
> (figures 4 5 6 3)
Rectangle:
Area=20 Perimeter=18
Square:
Area=36 Perimeter=24
Cirle:
Area=28.259999999999998 Perimeter=18.84 |#
Output
> (a_rec 3 4)
SANDEEP KUMAR http://san24mca.blogspot.com/
area of rectangle= 12
(b)Perimeter of rectangle
(define p_rec
(lambda (length breadth)
(display "Perimeter of rectangle= ")
(* 2 ( + length breadth))
)
)
Output
> (p_rec 4 5)
Perimeter of rectangle= 18
(c)Area of square
(define a_sqr
(lambda (h)
(display "area of square= ")
(* h h )))
Output
> (a_sqr 3)
area of square= 9
(d)Perimeter of square
(define p_sqr
(lambda (h)
(display "Perimeter of rectangle= ")
(* 4 h)
)
)
Output
> (p_sqr 3)
Perimeter of rectangle= 12
(e)Area of circle
(define a_cir
(lambda (r)
(display "area of circle= ")
(* 3.14 r r)
)
SANDEEP KUMAR http://san24mca.blogspot.com/
Output
> (a_cir 3)
area of circle= 28.259999999999998
(f)Perimeter of circle
(define p_cir
(lambda (r)
(display "Perimeter of circle= ")
(* 2 3.14 r )
)
)
Output
> (p_cir 3)
Perimeter of circle= 18.84
3 .Add, multiply and subtract two numbers in separate functions and call the
functions within a single function to display the result.
Output
ADDITION= 7
SUBTRACTION= 1
MULTIPLICATION= 12
(define (even_num)
(define n 1)
(let loop()
(if (<= n 50)
(begin
(if (= (remainder n 2) 0)
(begin
(display n) (display "\t")))
(set! n (+ n 1))
(loop)))))
(define (prime)
(define n 1)
(define i 1)
(define c 0)
(let loop()
(if (<= n 50)
SANDEEP KUMAR http://san24mca.blogspot.com/
(begin
(let loop1()
(if (<= i (/ n 2))
(begin
(if (= (remainder n i) 0)
(set! c (+ c 1))) (set! i (+ i 1)) (loop1))))
(if (> c 1)
(begin
(display n) (display "\t")))
(set! n (+ n 1)) (set! c 0)
(set! i 1) (loop)))))
(display "1. Odd numbers within 50.\n2. Even numbers within 50.\n3. Non-
prime numbers within 50.\nENTER CHOICE:")
(define choice(read))
(cond
[(= choice 1) = (odd_num)]
[(= choice 2) = (even_num)]
[(= choice 3) = (prime)])
Output
1. Odd numbers within 50.
2. Even numbers within 50.
3. Non-prime numbers within 50.
ENTER CHOICE:3
4 6 8 9 10 12 14 15 16 18 20 21 22
24 25 26 27 28 30 32 33 34 35 36 38
39 40 42 44 45 46 48 49 50
(define gt2 0)
(define ir 0)
(display name) (display "\t") (display cno)
(display "\nStart Reading:") (display lr)
(display "\nEnd Reading:") (display cr)
(if (> r 200)
(begin
(set! ir (- r 200)) (set! gt2 (* ir 4)) (set! r (- r ir)) (newline)
(display ir) (display " x 4=") (display gt2)))
(if (> r 100)
(begin
(set! ir (- r 100)) (set! gt1 (* ir 2)) (set! r (- r ir)) (newline)
(display ir) (display " x 2=") (display gt1)))
(newline)
(display r) (display " x 0.50=") (display (* r 0.50))
(set! bill (+ gt1 gt2 (* r 0.50)))
(display "\nBill=") (display bill))
Output
> (ebill "sk" 241255 2000 2400)
sk 241255
Start Reading:2000
End Reading:2400
200 x 4=800
100 x 2=200
100 x 0.50=50.0
Bill=1050.0
ASSINGMENT -8
Structure :
make-s :=> a constructor procedure that takes n arguments and returns a new
structure value.
s-field :=> for each field, an accessor procedure that takes a structure value and
extracts the value for field.
set-s-field! :=> for each field, a mutator procedure that takes a structure and a
new field value. The field value in the structure is destructively updated with the
new value, and void is returned.
Example #1:
Example #2 :
(For Structure definition, assigning value, retriving the value.)
(define-struct s (a b c d))
(define x (make-s 100 24.50 'raja "we are good"))
(define y (make-s 10 245.50 "we are good" 'ramji))
1. Create a structure personal and put data in various fields like name, age,
designation, salary. Create three difference instances and assign data into it.
SANDEEP KUMAR http://san24mca.blogspot.com/
Output
Name Sandeep
Age :24
Designation: "Soft eng"
Salary: 34000
Name Rabi
Age :23
Designation: Manager
Salary:44000
Name "Ramesh Das"
Age :23
Designation: CEO
Salary:524000
2. Create a structure student and take various fields like name, date-of-birth,
branch, subject1_mark, subject2_mark, subject3_mark, subject4_mark.
Create two instances and assign values into it. Display the record
information and also total and percentage of marks for each record.
Output
NAME: sandy
DOB: 24dec1985
BRANCH: mca
TOATAL: 258
PERCENTAGE: 64 1/2
NAME: syedul
DOB: 5may1987
BRANCH: mca
TOATAL: 353
PERCENTAGE: 88 1/4
3.Create a structure Sales and take various fields like sale_id, salesman_name,
Sale_date, Amount and assign initial values to it. Then re-assign the value to the
various ;fields of structure.
(define-struct Sales(sale_id sal_name sale_date amt))
(define x (make-Sales 's001 'market '23sep09 4500))
(display "INITIAL VALUE\n")
(display "SALE ID : ") (Sales-sale_id x)
SANDEEP KUMAR http://san24mca.blogspot.com/
4. Create a structure of your own and take any 4 fields and assign the value
in run time ;to it. Display the data.
(define-struct personal(name age branch state))
(define x (make-personal 's 34 'a 'b))
(display "ENTER NAME : ") (set-personal-name! x (read) )
(display "ENTER AGE : ") (set-personal-age! x (read) )
(display "ENTER BRANCH:") (set-personal-branch! x (read) )
(display "ENTER STATE: ") (set-personal-state! x (read) )
(display"Name ") ( personal-name x)
(display"Age :") ( personal-age x)
(display"Branch: ") ( personal-branch x)
(display"State: ") ( personal-state x)
Output
ENTER NAME : SANDEEP
SANDEEP KUMAR http://san24mca.blogspot.com/
ENTER AGE : 24
ENTER BRANCH:MCA
ENTER STATE: BIHAR
Name SANDEEP
Age :24
Branch: MCA
State: BIHAR
ASSINGMENT -9
1. “findMaxNum” takes a list of numbers as input argument and returns the
maximum number from the list.
Example : (findMaxNum (list 10 20 30 70 40)) → 70
(define (findmax l)
(define len (length l))
(define max 0)
(do ((i 1(+ i 1)))
((> i len))
(begin (if (< max (car l))
(begin (set! max (car l))
(set! l (cdr l)))
(set! l (cdr l)))))
(display max))
(findmax l)
Output
500
(define (compare_salary)
(define-struct employee(id name sal))
(define add(list))
(define (addition add)
(define sum 0)(define l (length add))
(define a 0)
(do ((i 1 (+ i 1)))
((> i l))
(if (number? (car add))
(begin (set! sum (+ sum (car add)))(set! add (cdr add)))
(set! add (cdr add))))(display sum))
Output
> (define list1(list 3 34 45 3 4 15 30))
> (addition list1)
134
4. Write scheme function to find the string having maximum length from a
SANDEEP KUMAR http://san24mca.blogspot.com/
string list.
(define str(list))
(define (maxstring str)
(define max 0)
(define l (length str))
(define a "0")
(do ((i 1 (+ i 1)))
((> i l))
(if (< max (string-length (car str)))
(begin (set! max (string-length (car str)))(set! a (car str))
(set! str (cdr str)) )
(set! str (cdr str))
))(display "maximum length of string:")(display max)(newline)(display
"string is:=")(display a))
Output
> (define str_list (list "my" "name" "sandeep kumar" "mithilesh" "syedul"
"abhisek mohanty"))
> (maxstring str_list)
maximum length of string:15
string is:=abhisek mohanty