Upload
jitendra-lenka
View
138
Download
0
Tags:
Embed Size (px)
DESCRIPTION
Scheme: A well known language for mathematical computation, scripting and others too.
Citation preview
SCHEME LANGUAGEJitendra Lenka
Sr. Software Engineer,
L&T Tech Services, Mysore
Introduction• First appeared in 1975
• Functional programming language
• Scheme was developed at the MIT AI Lab by Guy L. Steele and Gerald Jay Sussman
• Scheme is influenced by Lisp
• Scheme influenced Java Script, Ruby, Common Lisp
• Clojure, Common Lisp, Dylan, EuLisp, Haskell, Hop, JavaScript, Kernel, Lua, R, Racket, Ruby
• s-expressions, parenthesized lists in which a prefix operator is followed by its arguments.
• It supports both in Windows and Linux platform.
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
Scheme Variant• Chez Scheme
• Chicken Scheme
• Racket Scheme
First Program
>(sqrt 23)
4.79583152331272
>(display “Hello World”)
(newline)
Integer Arithmetic
(+ 100 200)
(+ 10 20 30 40 50 )
Q: What about negative number
Integer Procedures• remainder• quotient• Max• min
Nested Procedure Call
(* (- 7 3) (+ 5 12))
(* (quotient (* 7 41) (- (max 38 14) 35))
Definition
>(define freezing 32)
> (define boiling 212)
(- boiling freezing)
180
(define seconds-in-week (* 7 24 60 60))
Local & Global Binding• Local
>(let ((number 5)) (* number (+ number 1)))
30
Question : What will be the output ?
> (+ 12 number)
Local & Global Binding• Global Binding
>(define number 100)
>number
100
>(let ((number 5)) (* number (+ number 1)))
30
Q: What is output ?
> number
Internal Definition(let ((number 5))
(define successor (+ number 1)) (* number successor))
Simple Procedure
Definition :
> (define (square root) (* root root))
Calling ‘square’ procedure:
> (square 10)
Lambda Expression• Lambda
(define disparity (lambda (a b) (abs (- a b))))
• Without Lambda
(define (disparity a b) (abs (- a b)))
Boolean Expression
>(not #f)
>(not #t)
>(not 5)
>(not not)
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
Compare Boolean expression• >(eq? #t #t)
Equal and Inequality Procedure• Equal
>(= 5 6)
• Inequality
(define (~= first-num second-num)
(not (= first-num second-num)))
Factorial Function
>(define (factorial n)
(if (zero? n)
1
(* (factorial (sub1 n)) n)))
>(factorial 0)
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.
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) )
Pairs continue…• How do you recover the contents of a pair?>(define my-pair (cons 1 2))
>(car my-pair)
1
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)))
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
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
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
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))
THANK YOU