Comparative Programming Languages - PowerPoint PPT Presentation

About This Presentation
Title:

Comparative Programming Languages

Description:

A mid-1970s dialect of LISP, designed to be a cleaner, more modern, and simpler ... QUOTE can be abbreviated with the apostrophe prefix operator ... – PowerPoint PPT presentation

Number of Views:17
Avg rating:3.0/5.0
Slides: 53
Provided by: tjh5
Learn more at: https://www.tjhsst.edu
Category:

less

Transcript and Presenter's Notes

Title: Comparative Programming Languages


1
Comparative Programming Languages
  • Language Comparison Scheme, Smalltalk, Python,
    Ruby, Perl, Prolog, ML, C/STL, Java

2
Introduction to Scheme
  • A mid-1970s dialect of LISP, designed to be a
    cleaner, more modern, and simpler version than
    the contemporary dialects of LISP
  • Uses only static scoping
  • Functions are first-class entities
  • They can be the values of expressions and
    elements of lists
  • They can be assigned to variables and passed as
    parameters

CS 363 Spring 2005 GMU
2
3
Scheme
  • (define (gcd u v)
  • (if ( v 0)
  • u
  • (gcd v (remainder u v))
  • )
  • )
  • Once defined in the interpreter
  • (gcd 25 10)
  • 5

CS 363 Spring 2005 GMU
3
4
(No Transcript)
5
Scheme atoms
  • Constants
  • numbers, strings, T True,
  • Identifier names
  • Function/operator names
  • pre-defined user defined

CS 363 Spring 2005 GMU
5
6
Scheme Expression vs. C
  • In Scheme
  • ( 3 ( 4 5 ))
  • (and ( a b)(not ( a 0)))
  • (gcd 10 35)
  • In C
  • 3 4 5
  • (a b) (a ! 0)
  • gcd(10,35)

CS 363 Spring 2005 GMU
6
7
Smalltalk Expressions vs. C
  • In Smalltalk
  • 3 4 5
  • ( 35, 4 mess. sent to 3, then 5 mess.)
  • (a b) (a 0)
  • 10 gcd 35
  • gcd 35 mess. sent to 10
  • In C
  • 3 4 5
  • (a b) (a ! 0)
  • gcd(10,35)

CS 363 Spring 2005 GMU
7
8
Ruby/Python Expressions vs. C(very similar)
  • In Ruby/Python
  • 3 4 5
  • (a b) (a ! 0)
  • gcd(10,35)
  • In C
  • 3 4 5
  • (a b) (a ! 0)
  • gcd(10,35)

CS 363 Spring 2005 GMU
8
9
Ruby Expressions vs. CClass version
  • In Ruby
  • 3.plus(4).times(5)
  • ( 35)
  • (a b) (a ! 0)
  • 10.gcd(35)
  • In C
  • 3 4 5
  • (a b) (a ! 0)
  • gcd(10,35)

CS 363 Spring 2005 GMU
9
10
Evaluation Rules for Scheme Expressions
  1. Constant atoms (numbers, strings) evaluate to
    themselves
  2. Identifiers are looked up in the current
    environment and replaced by the value found there
    (using dynamically maintained symbol table)
  3. A list is evaluated by recursively evaluating
    each element in the list as an expression the
    first expression must evaluate to a function.
    The function is applied to the evaluated values
    of the rest of the list.

CS 363 Spring 2005 GMU
10
11
(No Transcript)
12
(No Transcript)
13
Smalltalk Conditionals
  • If statement
  • (v 0) ifTrue u.
  • v gcd u \\ v)
  • (a 0) ifTrue 0.
  • 1/a
  • (or use ifFalse for the
  • else portion above)
  • Cond statement
  • None in Smalltalk(?)
  • http//c2.com/cgi/wiki?CaseStatementsConsideredHar
    mful
  • Case statements considered
  • harmful??

CS 363 Spring 2005 GMU
13
14
Ruby Conditionals
  • If statement
  • if v 0
  • return u
  • else
  • return
  • gcd(v, u v)
  • end
  • if a 0
  • return 0
  • else return 1/a
  • end
  • Use elsif for else if
  • Cond statement
  • case a
  • when 0
  • 0
  • when 1
  • 1
  • else
  • 1/a
  • end

CS 363 Spring 2005 GMU
14
15
Python Conditionals
  • If statement
  • if v 0
  • return u
  • else
  • return gcd(v, u v)
  • if a 0
  • return 0
  • else
  • return 1/a
  • Blocks are indented.
  • Use elif for else if
  • Cond statement
  • Use
  • if
  • elif
  • elif
  • ...

CS 363 Spring 2005 GMU
15
16
Example of COND
  • (DEFINE (compare x y)
  • (COND
  • ((gt x y) (DISPLAY x is greater than y))
  • ((lt x y) (DISPLAY y is greater than x))
  • (ELSE (DISPLAY x and y are equal))
  • )
  • )

CS 363 Spring 2005 GMU
16
17
Example of COND
  • compare y
  • (self gt y) ifTrue
  • Transcript show x is greater than y
  • printString.
  • Transcript show cr.
  • ifFalse
  • (self lt y) ifTrue
  • Transcript show y is greater than x
  • printString. Transcript show cr.
  • ifFalse
  • Transcript show x and y are equal
  • printString. Transcript show cr..

CS 363 Spring 2005 GMU
17
18
Example of COND Ruby class vers.
  • class Integer
  • def compare(y)
  • if self gt y
  • print x is greater than y\n
  • elsif self lt y
  • print y is greater than x\n
  • else
  • print x and y are equal\n
  • end
  • Syntax 4 compare(5)

CS 363 Spring 2005 GMU
18
19
Example of COND Ruby non-class vers.
  • def compare(x, y)
  • if x gt y
  • print x is greater than y\n
  • elsif x lt y
  • print y is greater than x\n
  • else
  • print x and y are equal\n
  • end
  • Syntax compare(4,5)

CS 363 Spring 2005 GMU
19
20
Example of COND Python
  • def compare(x, y)
  • if x gt y
  • print x is greater than y
  • elif x lt y
  • print y is greater than x
  • else
  • print x and y are equal\n
  • Syntax compare(4,5)
  • (Also blocks are indented)

CS 363 Spring 2005 GMU
20
21
Predicate Functions
  • 1. EQ? takes two symbolic parameters it returns
    T if both parameters are atoms and the two are
    the same
  • e.g., (EQ? 'A 'A) yields T
  • (EQ? 'A '(A B)) yields ()
  • Note that if EQ? is called with list parameters,
    the result is not reliable
  • EQ? does not work for numeric atoms (use )

CS 363 Spring 2005 GMU
21
22
Predicate Functions
  • 2. LIST? takes one parameter it returns T if
    the parameter is a list otherwise()
  • 3. NULL? takes one parameter it returns T if
    the parameter is the empty list otherwise()
  • Note that NULL? returns T if the
    parameter is()
  • 4. Numeric Predicate Functions
  • , ltgt, gt, lt, gt, lt, EVEN?, ODD?, ZERO?,
    NEGATIVE?

CS 363 Spring 2005 GMU
22
23
Predicate Functions - Python
  • 1. returns True if both parameters are the
    same
  • e.g., 'A' 'A' yields True
  • 'A' 'A B' False
  • 2. ! returns True if both parameters are not the
    same.
  • e.g., 'A' ! 'A' yields False
  • 'A' ! 'A B' True

CS 363 Spring 2005 GMU
23
24
Predicate Functions - Python
  • 3. type() returns the type of an object.
  • type(1,2,3) list returns True
  • 4. len() returns True if the parameter is the
    empty list otherwise the length of the list
  • 5. Numeric Predicate Functions
  • , !, gt, lt, gt, lt

CS 363 Spring 2005 GMU
24
25
let function
  • Allows values to be given temporary names within
    an expression
  • (let ((a 2 ) (b 3)) ( a b))
  • 5
  • Semantics Evaluate all expressions, then bind
    the values to the names evaluate the body

CS 363 Spring 2005 GMU
25
26
List Comprehensions - Python
  • provide a concise way to create lists without
    resorting to use of map(), filter() and/or
    lambda.
  • gtgtgt freshfruit ' banana', ' loganberry ',
    'passion fruit '
  • gtgtgt weapon.strip() for weapon in freshfruit
  • 'banana', 'loganberry', 'passion fruit'

CS 363 Spring 2005 GMU
26
27
List Comprehensions - Python
  • gtgtgt vec 2, 4, 6
  • gtgtgt 3x for x in vec
  • 6, 12, 18
  • gtgtgt 3x for x in vec if x gt 3
  • 12, 18
  • gtgtgt 3x for x in vec if x lt 2
  • gtgtgt x,x2 for x in vec
  • 2, 4, 4, 16, 6, 36

CS 363 Spring 2005 GMU
27
28
for statements - Python
  • gtgtgt Measure some strings
  • ... a 'cat', 'window', 'defenestrate'
  • gtgtgt for x in a
  • ... print x, len(x)
  • cat 3
  • window 6
  • defenestrate 12

CS 363 Spring 2005 GMU
28
29
for statements - Python
  • gtgtgt Measure some strings
  • ... a 'cat', 'window', 'defenestrate'
  • gtgtgt for x in a
  • ... print x, len(x)
  • cat 3
  • window 6
  • defenestrate 12

CS 363 Spring 2005 GMU
29
30
for statements - Python
  • gtgtgt for x in a make a slice copy of the
    entire list
  • ... if len(x) gt 6 a.insert(0, x)
  • ...
  • gtgtgt a
  • 'defenestrate', 'cat', 'window', 'defenestrate'

CS 363 Spring 2005 GMU
30
31
range() function - Python
  • gtgtgt range(10)
  • 0, 1, 2, 3, 4, 5, 6, 7, 8, 9
  • gtgtgt range(5, 10)
  • 5, 6, 7, 8, 9
  • gtgtgt range(0, 10, 3)
  • 0, 3, 6, 9
  • gtgtgt range(-10, -100, -30)
  • -10, -40, -70

CS 363 Spring 2005 GMU
31
32
range() function - Python
  • gtgtgt a 'Mary', 'had', 'a', 'little', 'lamb'
  • gtgtgt for i in range(len(a))
  • ... print i, ai
  • 0 Mary
  • 1 had
  • 2 a
  • 3 little
  • 4 lamb

CS 363 Spring 2005 GMU
32
33
More looping - Python
  • gtgtgt knights 'gallahad' 'the pure', 'robin'
    'the brave'
  • gtgtgt for k, v in knights.iteritems()
  • ... print k, v
  • ...
  • gallahad the pure
  • robin the brave

33
CS 363 Spring 2005 GMU
34
More looping - Python
  • gtgtgt for i, v in enumerate('tic', 'tac', 'toe')
  • ... print i, v
  • ...
  • 0 tic
  • 1 tac
  • 2 toe

34
CS 363 Spring 2005 GMU
35
More looping - Python
  • gtgtgt for i in reversed(xrange(1,10,2))
  • ... print i
  • ...
  • 9
  • 7
  • 5
  • 3
  • 1

35
CS 363 Spring 2005 GMU
36
More looping - Python
  • gtgtgt basket 'apple', 'orange', 'apple', 'pear',
    'orange', 'banana'
  • gtgtgt for f in sorted(set(basket))
  • ... print f
  • apple
  • banana
  • orange
  • pear

36
CS 363 Spring 2005 GMU
37
Quote () function - Lisp/Scheme
  • A list that is preceeded by QUOTE or a quote mark
    () is NOT evaluated.
  • QUOTE is required because the Scheme interpreter,
    named EVAL, always evaluates parameters to
    function applications before applying the
    function. QUOTE is used to avoid parameter
    evaluation when it is not appropriate
  • QUOTE can be abbreviated with the apostrophe
    prefix operator
  • Can be used to provide function arguments
  • (myfunct (a b) (c d))

CS 363 Spring 2005 GMU
37
38
Output functions - Lisp/Scheme
  • Output Utility Functions
  • (DISPLAY expression)
  • (NEWLINE)

CS 363 Spring 2005 GMU
38
39
Output functions - Python
  • print
  • print "Hello, World!"
  • print "(33 2) / 5 11.5 ",(33 2) / 5
    11.5
  • print "Halt!"
  • s raw_input("Who Goes there? ")
  • print "You may pass,", s

CS 363 Spring 2005 GMU
39
40
define function - Scheme
  • Form 1 Bind a symbol to a expression
  • (define a 2)
  • (define emptylist ( ))
  • (define pi 3.141593)

CS 363 Spring 2005 GMU
40
41
assignment and def functions - Python
  • Form 1 Bind a symbol to a expression
  • a 2
  • emptylist
  • pi 3.141593

CS 363 Spring 2005 GMU
41
42
define function - Smalltalk
  • Form 1 Bind a symbol to a expression
  • a 2
  • mylist OrderedCollection new 0
  • mylist addAll (1 2 3 a b)
  • pi 3.141593

CS 363 Spring 2005 GMU
42
43
define function - Ruby/Python
  • Form 1 Bind a symbol to a expression
  • a 2
  • emptylist
  • mylist 1,2,3,'a'
  • pi 3.141593

CS 363 Spring 2005 GMU
43
44
(No Transcript)
45
(No Transcript)
46
(No Transcript)
47
(No Transcript)
48
(No Transcript)
49
(No Transcript)
50
(No Transcript)
51
(No Transcript)
52
(No Transcript)
Write a Comment
User Comments (0)
About PowerShow.com