0% found this document useful (0 votes)
60 views

Lesson 2b PDF

This document provides an introduction to Scheme programming and includes examples of: 1. Basic operations like arithmetic, logical operations, and string operations 2. Defining values and expressions using define, if, cond, and let 3. Evaluating series using recursion to calculate sums and products 4. Common list operations like car, cdr, cons, and working with lists

Uploaded by

Emon Roy
Copyright
© © All Rights Reserved
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
60 views

Lesson 2b PDF

This document provides an introduction to Scheme programming and includes examples of: 1. Basic operations like arithmetic, logical operations, and string operations 2. Defining values and expressions using define, if, cond, and let 3. Evaluating series using recursion to calculate sums and products 4. Common list operations like car, cdr, cons, and working with lists

Uploaded by

Emon Roy
Copyright
© © All Rights Reserved
Available Formats
Download as PDF, TXT or read online on Scribd
You are on page 1/ 9

Welcome to Scheme Programming

CSE 425 - Concepts of Programming Language

Md. Mahfuzur Rahman


ECE Department
North South University
CSE425 - Concepts of Programming Language MMR4, NSU

1 Basic Operations
1 ( d i s p l a y 4)
2 ; : d i s p l a y s 4 i n t h e monitor
3
4 (+ 1 2 3 4 5 )
5 ; : 15
6
7 (∗ 1 2 3 4 5 6 7 8 9 10)
8 ; : 3628800
9
10 (+ ( ∗ 40 ( / 9 5 ) ) 3 2 )
11 ;: 40
12
13 ( s q r t (+ ( ∗ 5 5 ) ( ∗ 12 1 2 ) ) )
14 ; : 13.0
15
16 ( q u o t i e n t 10 3 )
17 ;: 3
18
19 ( r e m a i n d e r 10 3 )
20 ;: 1
21
22 ( s u b s t r i n g ( s t r i n g append ” c a n d i c e ” ” b e r g e n ” ) 4 1 1 )
23 ; : iceberg
24
25 ( d i s p l a y (+ 3 ( r e a d ) ) )
26 ; : depends on keyboard i n p u t

2
CSE425 - Concepts of Programming Language MMR4, NSU

2 Logical Operations
1 ( p o s i t i v e ? 45)
2 ; : #t
3
4 ( negative ? 0)
5 ; : #f
6
7 ( zero ? 45)
8 ; : #f
9
10 (= 3 4 )
11 ; : #f
12
13 (= 3 3 )
14 ; : #t
15
16 (< 1 . 1 2 )
17 ; : #t
18
19 (>= 3 4 )
20 ; : #f
21
22 ( equal ? ' a 'b) ; e q u a l ? compares two o b j e c t s .
23 ; : #f
24
25 ( equal ? ' a ' a )
26 ; : #t
27
28 ( equal ? 1 1)
29 ; : #t
30
31 ( equal ? ' ( a b ( c d) ) ' ( a b ( c d) ) )
32 ; : #t
33
34 ( null ? ' (a) ) ; n u l l ? r e t u r n s #t o n l y on t h e empty l i s t .
35 ; : #f
36
37 ( null ? ' () )
38 ; : #t
39
40 ( not #t )
41 ; : #f
42
43 ( not #f )
44 ; : #t
45
46 ( s t r i n g >? ” abc ” ” d e f ” ) ; Compare two s t r i n g s .
47 ; : #f
48
49 ( s t r i n g >? ” d e f ” ” abc ” )
50 ; : #t

3
CSE425 - Concepts of Programming Language MMR4, NSU

3 Defining Values and Expressions


1 ( d e f i n e s i x 6)
2 six
3 ;: 6
4
5 ( define pi 3.14159)
6 ( d e f i n e radius 10)
7 (∗ pi (∗ radius radius ) )
8 ( d e f i n e c i r c l e (∗ 2 pi radius ) )
9 circle
10 ; : 62.8318
11
12 ( d e f i n e ( square x ) (∗ x x ) )
13 ( square 21)
14 ; : 441
15
16 ( d e f i n e ( square x ) (∗ x x ) )
17 ( d e f i n e ( sum o f s q u a r e s x y )
18 (+ ( s q u a r e x ) ( s q u a r e y ) ) )
19 ( define ( f a)
20 ( sum o f s q u a r e s (+ a 1 ) ( ∗ a 2 ) ) )
21 ( f 5)
22 ; : 136
23
24 ( d e f i n e x 7)
25 ( and (> x 5 ) (< x 1 0 ) )
26 ; : #t
27
28 ( d e f i n e (>= x y )
29 ( o r (> x y ) (= x y ) ) )
30 (>= 4 5 )
31 ; : #f
32
33 ( d e f i n e (>= x y )
34 ( not (< x y ) ) )
35 (>= 4 5 )
36 ; : #f
37
38 ( d e f i n e ( abs x )
39 ( i f (< x 0 )
40 ( x)
41 x) )
42
43 ( d e f i n e ( abs x )
44 ( cond (( < x 0 ) ( x) )
45 ( else x) ) )
46
47 ( d e f i n e ( abs x )
48 ( cond (( > x 0 ) x )
49 ((= x 0 ) 0 )
50 (( < x 0 ) ( x ) ) ) )
51

4
CSE425 - Concepts of Programming Language MMR4, NSU

52 ( d e f i n e ( a p l u s abs b a b )
53 ( ( i f (> b 0 ) + ) a b ) )
54 ( a p l u s abs b 10 20)
55 ; : 30
56
57 ( define ( f a c t o r i a l n)
58 ( i f ( zero ? n)
59 1
60 (∗ n ( f a c t o r i a l ( n 1) ) ) ) )
61 ( f a c t o r i a l 4)
62 ; : 24
63
64 ( define ( fib n)
65 ( cond ((= n 0 ) 0 )
66 ((= n 1 ) 1 )
67 ( e l s e (+ ( f i b ( n 1) )
68 ( fib ( n 2) ) ) ) ) )
69 ( f i b 6)
70 ;: 8
71
72 ; : A g e n e r a l form o f i f
73 ; : ( i f <c o n d i t i o n > <c o n s e q u e n t > <a l t e r n a t i v e >)
74
75 ; : A g e n e r a l form o f cond
76 ; : ( cond ( t e s t 1 c o n s e q u e n t 1)
77 ;: ( t e s t 2 c o n s e q u e n t 2)
78 ;: ...
79 ;: ( t e s t n consequent n )
80 ;: ( e l s e consequent d e f a u l t )
81 ;: )
82
83 ; : Defining l o c a l values using l e t
84 ; : f ( x , y ) = x (1+xy ) ˆ2 + y (1 y ) + (1+xy ) (1 y )
85 ; : a = 1+xy
86 ;: b = 1 y
87 ; : f ( x , y ) = xa ˆ2 + yb + ab
88 ( define ( f x y)
89 ( l e t ( ( a (+ 1 ( ∗ x y ) ) )
90 (b ( 1 y) ) )
91 (+ ( ∗ x ( s q u a r e a ) )
92 (∗ y b)
93 (∗ a b) ) ) )
94
95
96 ; : S y n t a c t i c Sugar
97 ( d e f i n e ( square a ) (∗ a a ) )
98 ( d e f i n e s q u a r e ( lambda ( a ) ( ∗ a a ) ) )
99 ( d i s p l a y ( square 6) )

5
CSE425 - Concepts of Programming Language MMR4, NSU

4 Evaluating Series
1 ; : f ( a , b )= 1+ 2+ 3+ . . . . . + 10 ; : a=1 b=10
2 ; : computes t h e sum o f t h e i n t e g e r s from a through b
3 ( d e f i n e ( sum i n t e g e r s a b )
4 ( i f (> a b )
5 0
6 (+ a ( sum i n t e g e r s (+ a 1 ) b ) )
7 )
8 )
9 ( sum i n t e g e r s 1 1 0 )
10
11 ; : f ( a , b )= 1ˆ3+ 2ˆ3+ 3ˆ3+ . . . . . + 10ˆ3 ; : a=1 b=10
12 ; : computes t h e sum o f t h e c u b e s o f t h e i n t e g e r s i n t h e g i v e n r a n g e
13 ( d e f i n e ( cube x ) ( ∗ x x x ) )
14 ( d e f i n e ( sum c u b e s a b )
15 ( i f (> a b )
16 0
17 (+ ( cube a ) ( sum c u b e s (+ a 1 ) b ) ) )
18 )
19 )
20 ( sum c u b e s 1 1 0 )
21
22 ; : f ( a , b )= 1 / ( 1 . 3 ) + 1 / ( 5 . 7 )+ 1 / ( 9 . 1 1 ) + . . . . . . . ; : a=1 b=10
23 ; : computes t h e sum o f a s e q u e n c e o f terms i n t h e s e r i e s u n t i l p i /8
24 ( d e f i n e ( pi sum a b )
25 ( i f (> a b )
26 0
27 (+ ( / 1 . 0 ( ∗ a (+ a 2 ) ) ) ( pi sum (+ a 4 ) b ) )
28 )
29 )
30 ( pi sum 1 1 0 )
31
32 ;: f (a , b) = f (a) + . . . . . . + f (b)
33 ;: A common u n d e r l y i n g p a t t e r n
34 ;: <term> and <next> can be d e f i n e d i n l i n e
35 ;: ( d e f i n e (<name> a b )
36 ;: ( i f (> a b )
37 ;: 0
38 ;: (+ (<term> a ) (<name> (<next> a ) b ) ) ) )
39
40 ; : <term> and <next> can be d e f i n e d s e p a r a t e l y
41 ( d e f i n e ( i n c r n1 ) (+ n1 1 ) )
42 ( d e f i n e ( i n c r b y t w o n2 ) (+ n2 2 ) )
43 ( d e f i n e ( c u b e s m1) ( ∗ m1 m1 m1) )
44 ( d e f i n e ( s q u a r e s m2) ( ∗ m2 m2) )
45 ( d e f i n e ( sum term a next b )
46 ( i f (> a b )
47 0
48 (+ ( term a ) ( sum term ( nex t a ) ne xt b ) ) ) )
49 ( sum c u b e s 1 i n c r 1 0 ) ; : 3025
50 ( sum s q u a r e s 1 i n c r b y t w o 1 0 ) ; : 165

6
CSE425 - Concepts of Programming Language MMR4, NSU

5 List Operations
1 ; : Scenario A
2 ( d e f i n e a 1)
3 ( d e f i n e b 2)
4
5 ; : d i s t i n g u i s h between symbols and t h e i r v a l u e s
6 ( l i s t a b)
7 ; : (1 2)
8
9 ( l i s t 'a 'b)
10 ; : (a b)
11
12 ( l i s t 'a b) ; ' d e n o t e s a symbol e l e m e n t
13 ; : ( a 2)
14
15
16 ( car ' ( a b c ) ) ; ' d e n o t e s symbol e l e m e n t l i s t ; car r e t u r n s 1 s t element
17 ;: a
18
19 ( cdr ' ( a b c ) ) ; ' d e n o t e s symbol e l e m e n t l i s t ; cdr r e t u r n s the r e s t .
20 ; : (b c )
21
22 ( cdr ' (b c ) ) ; ' d e n o t e s symbol e l e m e n t l i s t
23 ;: (c)
24
25 ( cdr ' ( c ) ) ; ' d e n o t e s symbol e l e m e n t l i s t
26 ; : ()
27
28 ( cdr ' ( ) ) ; ' ( ) a l i s t with no e l e m e n t
29 Error : exception ( cdr ' ( ) ) ; ' ( ) a l i s t with no e l e m e n t
30
31 ( cons ' a ' (b c ) )
32 ; : (a b c)
33
34 ( c o n s ( c a r ' ( a1 b1 c1 ) ) ( c d r ' ( a2 b2 c2 ) ) )
35 ; : ( a1 b2 c2 )
36
37 ( cons 1
38 ( cons 'b
39 ( cons ' (3 4)
40 ( cons 5 ' (6) ) ) ) ) ; ' d e n o t e s symbol e l e m e n t l i s t
41 ; : ( l i s t <a1> <a2> . . . <an>) i s e q u i v a l e n t t o
42 ; : ( c o n s <a1> ( c o n s <a2> ( c o n s . . . ( c o n s <an> n i l ) . . . ) ) )
43
44 ; : Scenario B
45 ( d e f i n e one through f o u r ( l i s t 1 2 3 4 ) )
46
47 one through f o u r
48 ; : (1 2 3 4)
49
50 ( c a r one through f o u r )
51 ;: 1

7
CSE425 - Concepts of Programming Language MMR4, NSU

52
53 ( c d r one through f o u r )
54 ; : (2 3 4)
55
56 ( c a r ( c d r one through f o u r ) )
57 ;: 2
58
59 ( c o n s 10 one through f o u r )
60 ; : (10 1 2 3 4)
61
62 ( c o n s 5 one through f o u r )
63 ; : (5 1 2 3 4)
64
65 ; : Scenario C
66 ( d e f i n e x ( l i s t 1 2 3) )
67 ( d e f i n e y ( l i s t 4 5 6) )
68
69 ( append x y )
70 ; : (1 2 3 4 5 6)
71
72 ( cons x y )
73 ; : ((1 2 3) 4 5 6)
74
75 ( l i s t x y)
76 ; : ((1 2 3) (4 5 6) )
77
78 ; : Scenario D
79 ; : D i s p l a y i n g t h e nth e l e m e n t o f t h e l i s t
80 ( d e f i n e ( l i s t r e f items n)
81 ( i f (= n 1 )
82 ( car items )
83 ( l i s t r e f ( cdr items ) ( n 1) ) ) )
84 ( d e f i n e s q u a r e s ( l i s t 1 4 9 16 2 5 ) )
85 ( l i s t r e f squares 4)
86 ; : 16
87
88 ; : D i s p l a y i n g t o t a l number o f e l e m e n t s i n t h e l i s t
89 ( d e f i n e ( length items )
90 ( i f ( n u l l ? items )
91 0
92 (+ 1 ( l e n g t h ( c d r i t e m s ) ) ) ) )
93 ( d e f i n e odds ( l i s t 1 3 5 7 ) )
94 ( l e n g t h odds )
95 ;: 4
96
97 ; : Applying some o p e r a t i o n s t o each e l e m e n t s i n t h e l i s t
98 ( d e f i n e ( scale l i s t items f a c t o r )
99 ( i f ( n u l l ? items )
100 ' () ; ' ( ) i s a l i s t with no e l e m e n t
101 ( cons (∗ ( car items ) f a c t o r )
102 ( s c a l e l i s t ( cdr items ) f a c t o r ) ) ) )
103 ( s c a l e l i s t ( l i s t 1 2 3 4 5) 10)
104 ; : ( 1 0 20 30 40 5 0 )
105

8
CSE425 - Concepts of Programming Language MMR4, NSU

106 ; : Applying t h e d e s i r e d o p e r a t i o n with a s e p a r a t e d e f i n i t i o n


107 ( d e f i n e ( a b s o l u t e num)
108 ( i f (< num 0 ) ( ∗ num 1) num) )
109
110 ( d e f i n e ( make p r o c i t e m s )
111 ( i f ( n u l l ? items )
112 ' () ; ' ( ) i s a l i s t with no e l e m e n t
113 ( cons ( proc ( car items ) )
114 (map p r o c ( c d r i t e m s ) ) ) ) )
115 ( make a b s o l u t e ( l i s t 10 2 . 5 11.6 1 7 ) )
116 ; : (10 2.5 11.6 17)
117
118 ; : Scenario E
119 ; : D e f i n i n g L i s t o p e r a t i o n s with lambda
120 ( d e f i n e sum
121 ( lambda ( l )
122 ( i f ( null ? l )
123 0
124 (+ ( c a r l ) ( sum ( c d r l ) ) ) ) ) )
125
126 ( d e f i n e product
127 ( lambda ( l )
128 ( i f ( null ? l )
129 1
130 ( ∗ ( c a r l ) ( sum ( c d r l ) ) ) ) ) )
131
132 ( define length
133 ( lambda ( l )
134 ( i f ( null ? l )
135 0
136 (+ 1 ( l e n g t h ( c d r l ) ) ) ) ) )

You might also like