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

4
CSE425 - Concepts of Programming Language MMR4, NSU

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

También podría gustarte