28
SCHEME LANGUAGE Jitendra Lenka Sr. Software Engineer, L&T Tech Services, Mysore

Scheme language

Embed Size (px)

DESCRIPTION

Scheme: A well known language for mathematical computation, scripting and others too.

Citation preview

Page 1: Scheme language

SCHEME LANGUAGEJitendra Lenka

Sr. Software Engineer,

L&T Tech Services, Mysore

Page 3: Scheme language

Introduction Continues…• It uses small size interpreter

• Uses in embedded system, compiler design, scripting.

• Google App Inventor for Android uses Scheme, where Kawa is used to compile the Scheme code down to byte-codes for the Java Virtual Machine running on Android devices

• List of Universities teach Scheme :• MIT

• Northeastern University and Worcester Polytechnic Institute

• Rice University, etc

Page 4: Scheme language

Scheme Variant• Chez Scheme

• Chicken Scheme

• Racket Scheme

Page 5: Scheme language

First Program

>(sqrt 23)

4.79583152331272

>(display “Hello World”)

(newline)

Page 6: Scheme language

Integer Arithmetic

(+ 100 200)

(+ 10 20 30 40 50 )

Q: What about negative number

Page 7: Scheme language

Integer Procedures• remainder• quotient• Max• min

Page 8: Scheme language

Nested Procedure Call

(* (- 7 3) (+ 5 12))

(* (quotient (* 7 41) (- (max 38 14) 35))

Page 9: Scheme language

Definition

>(define freezing 32)

> (define boiling 212)

(- boiling freezing)

180

(define seconds-in-week (* 7 24 60 60))

Page 10: Scheme language

Local & Global Binding• Local

>(let ((number 5)) (* number (+ number 1)))

30

Question : What will be the output ?

> (+ 12 number)

Page 11: Scheme language

Local & Global Binding• Global Binding

>(define number 100)

>number

100

>(let ((number 5)) (* number (+ number 1)))

30

Q: What is output ?

> number

Page 12: Scheme language

Internal Definition(let ((number 5))

(define successor (+ number 1)) (* number successor))

Page 13: Scheme language

Simple Procedure

Definition :

> (define (square root) (* root root))

Calling ‘square’ procedure:

> (square 10)

Page 14: Scheme language

Lambda Expression• Lambda

(define disparity (lambda (a b) (abs (- a b))))

• Without Lambda

(define (disparity a b) (abs (- a b)))

Page 15: Scheme language

Boolean Expression

>(not #f)

>(not #t)

>(not 5)

>(not not)

Page 16: Scheme language

Boolean Expression Continues…• So you use the boolean? predicate to distinguish Boolean

values from values of other types?

>(boolean? #t) #t

> (boolean? #f) #t

> (boolean? 0) #f

> (boolean? (lambda (augend) (+ augend 3))) #f

> (boolean? boolean?) #f

> (boolean? (boolean? boolean?)) #t

Page 17: Scheme language

Compare Boolean expression• >(eq? #t #t)

Page 18: Scheme language

Equal and Inequality Procedure• Equal

>(= 5 6)

• Inequality

(define (~= first-num second-num)

(not (= first-num second-num)))

Page 19: Scheme language

Factorial Function

>(define (factorial n)

(if (zero? n)

1

(* (factorial (sub1 n)) n)))

>(factorial 0)

Page 20: Scheme language

Pairs• Pascal, in which procedures can return only simple data

values; but in Scheme a procedure can return anything

• If you have exactly two values to pack together, the best data structure to use is a pair -- a container designed specifically to hold two values. The two values packed into a pair are completely independent of one another; they need not belong to the same data type.

Page 21: Scheme language

Pairs continue…How do you construct a pair?

>(cons 1 2)

(1 . 2)

>(cons #t #f)

(#t . #f)

So a pair can be a component of another pair?

>(cons (cons 1 2) 3) ((1 . 2) . 3)

Q:What Will bé the output ?

(define lp (cons (cons (cons (cons (cons 1 2) 3) 4) 5) 6) )

Page 22: Scheme language

Pairs continue…• How do you recover the contents of a pair?>(define my-pair (cons 1 2))

>(car my-pair)

1

Page 23: Scheme language

ListUsing list we can pack more than two values.

How does Scheme treat lists differently from other pairs in Scheme?

>(cons 1 (cons 2 '()))

(1 2)

use the shorthand notation for lists when you type them in, too?

>'(8 4 6 1)

(8 4 6 1)

>'()

()

Q: What will be the output

> (cons 1 (cons 2 (cons 3 4)))

Page 24: Scheme language

List continues…>(define list-of-six (list 6 5 4 3 2 1))

Empty List: null procedure defines it. which takes one argument and determines whether its operand is the empty list and returns Boolean value.

>(null? '()) #t

> (null? '(3)) #f

>(null? (cons 1 2)) #f

>(null? (cdr '(3))) #t

> (null? (cdr (cons 3 '()))) #t

Page 25: Scheme language

List continues…

> (list 1 2 3 4) (1 2 3 4)

> (list (cons 1 2) (cons 3 4) (cons 5 6)) ((1 . 2) (3 . 4) (5 . 6))

> (list (+ 1 12) (* 60 4)) (13 240)

> (list 28) (28) > (list) ()

List-ref procédure : extract value from a list.

>(list-ref '(0 1 4 9 16 25 36 49 64 81 100 121) 7)

49

Page 26: Scheme language

Vectors• Vectors are heterogeneous structures whose elements are indexed

by integers. A vector typically occupies less space than a list of the same length, and the average time required to access a randomly chosen element is typically less for the vector than for the list.

> (define my-vec (vector 1 2 3))

> my-vec

#(1 2 3)

> (vector-ref my-vec 2)

3

Page 27: Scheme language

Vector continues…• Useful procedures(vector obj)

Create a vector

(vector? Obj )

Returns boolean

(vector-length vector)

Returns the number of elements of the venctor.

Vector->list vector

(vector-list ‘#(12 13 14))

List->vector

(list->vector ‘(1 2 3))

Page 28: Scheme language

THANK YOU