Comparative Programming Languages - PowerPoint PPT Presentation

1 / 60
About This Presentation
Title:

Comparative Programming Languages

Description:

Comparative Programming Languages – PowerPoint PPT presentation

Number of Views:20
Avg rating:3.0/5.0
Slides: 61
Provided by: tjh5
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
Function Evaluation Lisp/Scheme
  • Evaluation process (for normal functions)
  • 1. Parameters are evaluated, in no particular
    order
  • 2. The values of the parameters are substituted
    into the function body
  • 3. The function body is evaluated
  • 4. The value of the last expression in the body
    is the value of the function
  • (Special forms use a different evaluation process)

CS 363 Spring 2005 GMU
2
3
(No Transcript)
4
(No Transcript)
5
Basic List Manipulation
  • (car L) returns the first element of L
  • (cdr L) returns L minus the first element
  • (car (1 2 3)) 1
  • (car ((a b)(c d))) (a b)
  • (cdr (1 2 3)) (2 3)
  • (cdr ((a b)(c d))) ((c d))

CS 363 Spring 2005 GMU
5
6
Basic List Manipulation - Python
  • L0 returns the first element of L
  • L1 returns L minus the first element
  • x, y 1,2,3, 'a','b', 'c','d'
  • x0 1
  • y0 'a', 'b'
  • x1 2, 3
  • y1 'c', 'd'

CS 363 Spring 2005 GMU
6
7
Basic List Manipulation Smalltalk
  • L first returns the first element of L
  • L allButFirst returns L minus the first
    element (Squeak), in gst use L copyFrom2
  • (1 2 3) first -gt 1
  • ((a b)(c d))) first -gt (a b)
  • (1 2 3) copyFrom 2 -gt (2 3)
  • ((a b)(c d))) copyFrom2 -gt((c d))

CS 363 Spring 2005 GMU
7
8
Basic List Manipulation Ruby
  • L.first returns the first element of L
  • L.slice(1..m) returns L minus the first
    element
  • 1, 2, 3.first -gt 1
  • 'a', 'b' 'c', 'd'.first -gta,b
  • (1 2 3) copyFrom 2 -gt (2 3)
  • ((a b)(c d))) copyFrom2 -gt((c d))

CS 363 Spring 2005 GMU
8
9
Basic List Manipulation
  • (list e1 en) return the list created from the
    individual elements
  • (cons e L) returns the list created by adding
    expression e to the beginning of list L
  • (list 2 3 4) (2 3 4)
  • (list (a b) x (c d) ) ((a b)x(c d))
  • (cons 2 (3 4)) (2 3 4)
  • (cons ((a b)) (c)) (((a b)) c)

CS 363 Spring 2005 GMU
9
10
Basic List Manipulation - Python
  • gtgtgt a 'spam', 'eggs', 100, 1234
  • gtgtgt a
  • 'spam', 'eggs', 100, 1234
  • gtgtgt a0
  • 'spam'
  • gtgtgt a3
  • 1234
  • gtgtgt a-2
  • 100
  • gtgtgt a1-1
  • 'eggs', 100

CS 363 Spring 2005 GMU
10
11
Basic List Manipulation - Python
  • gtgtgt a2 'bacon', 22
  • 'spam', 'eggs', 'bacon', 4
  • gtgtgt 3a3 'Boe!'
  • 'spam', 'eggs', 100, 'spam', 'eggs', 100,
    'spam', 'eggs', 100, 'Boe!'
  • List items can be changed
  • gtgtgt a
  • 'spam', 'eggs', 100, 1234
  • gtgtgt a2 a2 23
  • gtgtgt a
  • 'spam', 'eggs', 123, 1234

CS 363 Spring 2005 GMU
11
12
Basic List Manipulation - Python
  • It is possible to nest lists (create lists
    containing other lists), for example
  • gtgtgt q 2, 3
  • gtgtgt p 1, q, 4
  • gtgtgt len(p)
  • 3
  • gtgtgt p1
  • 2, 3
  • gtgtgt p10
  • 2

CS 363 Spring 2005 GMU
12
13
Basic List Manipulation - Python
  • gtgtgt p1.append('xtra')
  • gtgtgt p
  • 1, 2, 3, 'xtra', 4
  • gtgtgt q
  • 2, 3, 'xtra'
  • Note that in the last example, p1 and q really
    refer to the same object!

CS 363 Spring 2005 GMU
13
14
Basic List Manipulation - Python
  • gtgtgt x 3,4,5
  • gtgtgt y 'a','b','c'
  • gtgtgt y0 x like (cons (car y) x)
  • gtgtgt y1 like (cdr y)

CS 363 Spring 2005 GMU
14
15
Example Functions - Scheme
  • 1. member - takes an atom and a simple list
    returns T if the atom is in the list ()
    otherwise
  • (DEFINE (member atm lis)
  • (COND
  • ((NULL? lis) '())
  • ((EQ? atm (CAR lis)) T)
  • ((ELSE (member atm (CDR lis)))
  • ))

CS 363 Spring 2005 GMU
15
16
Example Functions - Python
  • 1. member - takes an atom and a simple list
    returns True if the atom is in the list
    otherwise
  • def member(atm, lis)
  • if len.lis 0
  • return
  • elif atm lis0
  • True
  • else
  • member(atm, lis1)

CS 363 Spring 2005 GMU
16
17
Example Functions - Ruby
  • 1. member - takes an atom and a simple list
    returns true if the atom is in the list
    otherwise
  • def member(atm, lis)
  • if len.lis 0 then
  • elsif atm lis0 then
  • true
  • else
  • member(atm, lis1..lis.length-1)
  • end
  • end

CS 363 Spring 2005 GMU
17
18
Example Functions - Smalltalk
  • 1. member - takes an atom and a simple list
    returns true if the atom is in the list
    otherwise
  • member atm
  • self size 0 ifTrue ().
  • ifFalse
  • atm self first ifTrue
  • true.
  • ifFalse
  • self allButFirst member atm

CS 363 Spring 2005 GMU
18
19
Example Functions - Perl
  • 1. member - takes an atom and a simple list
    returns true if the atom is in the list
    otherwise
  • sub member
  • my atm my _at_ls (atm, _at_ls) _at__
  • if (scalar(_at_ls) 0)
  • return 0
  • elsif (ls0 eq atm)
  • return 1
  • else
  • member(atm,_at_ls1..ls)

CS 363 Spring 2005 GMU
19
20
Example Functions - Prolog
  • 1. member - takes an atom and a simple list
    returns true if the atom is in the list
    otherwise
  • member(X,X_).
  • member(X,_Rest) -
  • member(X,Rest).
  • consult('member.pl').
  • ?- member(5,3,2,1,4,5,6).

CS 363 Spring 2005 GMU
20
21
Example Functions - ML
  • 1. member - takes an atom and a simple list
    returns true if the atom is in the list
    otherwise
  • fun member(_,) false
  • member(atm,firstrest)
  • if atmfirst then true
  • else member(atm,rest)
  • ( use - use "member.sml"
  • val member fn ''a ''a list -gt bool
  • val it () unit
  • - member(3,3,4,5)
  • val it true bool
  • )

CS 363 Spring 2005 GMU
21
22
Example Functions C, STL
  • 1. member - takes an atom and a simple list
    returns true if the atom is in the list
    otherwise
  • bool member(int atm, listltintgt lis)
  • if (lis.size() 0)
  • return false
  • else if (lis.begin() atm)
  • return true
  • else
  • lis.pop_front()
  • return member(atm,lis)

CS 363 Spring 2005 GMU
22
23
Example Functions Java
  • 1. member - takes an atom and a simple list
    returns true if the atom is in the list
    otherwise
  • boolean member(int atm, List lis)
  • if (lis.isEmpty()) return false
  • else if
  • (((Integer) lis.get(0)).intValue() atm)
  • return true
  • else
  • return
  • member(atm, lis.subList(1, lis.size()))

CS 363 Spring 2005 GMU
23
24
Example Functions - Scheme
  • 2. equalsimp - takes two simple lists as
    parameters returns T if the two simple lists
    are equal () otherwise
  • (DEFINE (equalsimp lis1 lis2)
  • (COND
  • ((NULL? lis1) (NULL? lis2))
  • ((NULL? lis2) '())
  • ((EQ? (CAR lis1) (CAR lis2))
  • (equalsimp(CDR lis1)(CDR lis2)))
  • (ELSE '())
  • ))

CS 363 Spring 2005 GMU
24
25
Example Functions - Python
  • 2. equalsimp - takes two simple lists as
    parameters returns true if the two simple lists
    are equal () otherwise
  • def equalsimp(lis1, lis2)
  • if len(lis1)0
  • return len(lis2) 0
  • elif len(lis2)0
  • return
  • elif lis10 lis20
  • return equalsimp(lis11,lis21)
  • else return

25
26
Example Functions - Ruby
  • 2. equalsimp - takes two simple lists as
    parameters returns true if the two simple lists
    are equal () otherwise
  • def equalsimp(lis1, lis2)
  • if lis1.length0
  • lis2.length 0
  • elsif lis2.length0
  • elsif lis10 lis20
  • equalsimp(lis11..lis1.length-1,lis21..lis2
    .length-1)
  • else
  • end
  • end

26
27
Example Functions - Smalltalk
  • 2. equalsimp - takes two simple lists as
    parameters returns true if the two simple lists
    are equal () otherwise
  • equalsimp lis2
  • self size 0 ifTrue lis2 size 0.
  • ifFalse
  • lis20 ifTrue ().
  • ifFalse
  • self first lis2 first ifTrue
  • self allButFirst equalsimp
  • lis2 allButFirst.
  • ifFalse ().
  • end
  • end

27
28
Example Functions - Scheme
  • 3. equal - takes two general lists as parameters
    returns T if the two lists are equal
    ()otherwise
  • (DEFINE (equal lis1 lis2)
  • (COND
  • ((NOT (LIST? lis1))(EQ? lis1 lis2))
  • ((NOT (LIST? lis2)) '())
  • ((NULL? lis1) (NULL? lis2))
  • ((NULL? lis2) '())
  • ((equal (CAR lis1) (CAR lis2))
  • (equal (CDR lis1) (CDR lis2)))
  • (ELSE '())
  • ))

CS 363 Spring 2005 GMU
28
29
Example Functions - Scheme
  • (define (count L)
  • (if (null? L) 0
  • ( 1 (count (cdr L)))
  • )
  • )
  • (count ( a b c d))
  • ( 1 (count (b c d)))
  • ( 1 ( 1(count (c d))))
  • ( 1 ( 1 ( 1 (count (d)))))
  • ( 1 ( 1 ( 1 ( 1 (count ())))))
  • ( 1 ( 1 ( 1 ( 1 0)))) 4

CS 363 Spring 2005 GMU
29
30
Example Functions - Python
  • def count(L)
  • if len(L) 0
  • return 0
  • else
  • return 1 count(L1)

CS 363 Spring 2005 GMU
30
31
Example Functions - Ruby
  • def count(lis)
  • if lis.length 0 then
  • 0
  • else
  • 1 count(lis1..lis.length-1)
  • end
  • end

CS 363 Spring 2005 GMU
31
32
Example Functions - Smalltalk
  • count
  • self size 0 ifTrue 0.
  • ifFalse
  • (1 self allButFirst count).

CS 363 Spring 2005 GMU
32
33
Example Functions - Perl
  • sub count
  • my _at_ls _at_ls _at__
  • if (scalar(_at_ls) 1)
  • 1
  • else
  • count(_at_ls1..ls) 1

CS 363 Spring 2005 GMU
33
34
Example Functions - Prolog
  • count(,Total , Total).
  • count(_Rest, Counter,TotalCount) -
  • NewCount is Counter 1,
  • count(Rest, NewCount,TotalCount).

CS 363 Spring 2005 GMU
34
35
Example Functions - ML
  • fun count() 0
  • count(firstrest)
  • 1 count(rest)

CS 363 Spring 2005 GMU
35
36
Example Functions C, STL
  • int count(listltintgt lis)
  • if (lis.size() 0)
  • return 0
  • else
  • lis.pop_front()
  • return 1 count(lis)

CS 363 Spring 2005 GMU
36
37
Example Functions Java
  • int count(List lis)
  • if (lis.isEmpty())
  • // or lis.size() 0
  • return 0
  • else
  • return 1
  • count(lis.subList(1, lis.size()))

CS 363 Spring 2005 GMU
37
38
Scheme Functions
  • Now define
  • (define (count1 L) ??
  • )
  • so that (count1 (a (b c d) e)) 5

CS 363 Spring 2005 GMU
38
39
Scheme Functions
  • This function counts the individual elements
  • (define (count1 L)
  • (cond ( (null? L) 0 )
  • ( (list? (car L))
  • ( (count1 (car L))(count1 (cdr L))))
  • (else ( 1 (count (cdr L))))
  • )
  • )
  • so that (count1 (a (b c d) e)) 5

CS 363 Spring 2005 GMU
39
40
Example Functions - Python
  • def myappend(L, M)
  • if len(L)0
  • return M
  • else
  • L0 myappend(L1,M))
  • myappend('a','b', 'c','d'
  • 'a', 'b', 'c', 'd'

CS 363 Spring 2005 GMU
40
41
Example Functions - Ruby
  • def myappend(lis, m)
  • if lis.length0 then
  • M
  • else
  • lis0
  • myappend(lis1..lis.length-1,M)
  • end
  • end
  • myappend('a','b', 'c','d'
  • 'a', 'b', 'c', 'd'

CS 363 Spring 2005 GMU
41
42
Example Functions - Smalltalk
  • myappend m)
  • self size 0 ifTrue m.
  • ifFalse
  • (OrderedList with self first),
  • self allButFirst myappend m.
  • (a b) myappend ('c' 'd')
  • ('a' 'b' 'c' 'd')

CS 363 Spring 2005 GMU
42
43
Example Functions - Prolog
  • append(,List, List).
  • append(FirstRest,List2,
  • FirstList3) -
  • append(Rest, List2, List3).

CS 363 Spring 2005 GMU
43
44
How does append do its job?
  • (define (append L M)
  • (if (null? L) M
  • (cons (car L)(append(cdr L) M))))
  • (append (a b) (c d))
  • (cons a (append (b) (c d)))

CS 363 Spring 2005 GMU
44
45
How does append do its job?
  • (define (append L M)
  • (if (null? L) M
  • (cons (car L)(append(cdr L) M))))
  • (append (a b) (c d))
  • (cons a (append (b) (c d)))
  • (cons a (cons b (append () (c d))))

CS 363 Spring 2005 GMU
45
46
How does append do its job?
  • (define (append L M)
  • (if (null? L) M
  • (cons (car L)(append(cdr L) M))))
  • (append (a b) (c d))
  • (cons a (append (b) (c d)))
  • (cons a (cons b (append () (c d))))
  • (cons a (cons b (c d)))

CS 363 Spring 2005 GMU
46
47
How does append do its job?
  • (define (append L M)
  • (if (null? L) M
  • (cons (car L)(append(cdr L) M))))
  • (append (a b) (c d))
  • (cons a (append (b) (c d)))
  • (cons a (cons b (append () (c d))))
  • (cons a (cons b (c d)))
  • (cons a (b c d))

CS 363 Spring 2005 GMU
47
48
How does append do its job?
  • (define (append L M)
  • (if (null? L) M
  • (cons (car L)(append(cdr L) M))))
  • (append (a b) (c d))
  • (cons a (append (b) (c d)))
  • (cons a (cons b (append () (c d))))
  • (cons a (cons b (c d)))
  • (cons a (b c d))
  • (a b c d)

CS 363 Spring 2005 GMU
48
49
append C/STL
  • listltintgt append(listltintgt lis, listltintgtm)
  • if (lis.size() 0)
  • return m
  • else
  • int first lis.begin()
  • lis.pop_front()
  • listltintgt appendedList
  • append(lis, m)
  • appendedList.push_front(first)
  • return appendedList

CS 363 Spring 2005 GMU
49
50
append Java
  • List append(List lis, List m)
  • if (lis.isEmpty()) return m
  • else
  • Integer first (Integer) lis.get(0)
  • List temp new ArrayList()
  • temp.addAll(
  • append(lis.subList(1,lis.size()), m))
  • temp.add(0, first)
  • return temp

CS 363 Spring 2005 GMU
50
51
Reverse - Scheme
  • Write a function that takes a list of elements
    and reverses it
  • (reverse (1 2 3 4)) (4 3 2 1)

CS 363 Spring 2005 GMU
51
52
Reverse - Scheme
  • (define (reverse L)
  • (if (null? L) ()
  • (append (reverse (cdr L))(list (car L)))
  • )
  • )

CS 363 Spring 2005 GMU
52
53
Reverse - Python
  • def reverse(L)
  • if len(L)0
  • return
  • else
  • myappend (reverse (L1), L0)

CS 363 Spring 2005 GMU
53
54
Reverse - Ruby
  • def reverse(lis)
  • if lis.length0 then
  • else
  • myappend(
  • reverse (lis1..lis.length-1),
  • lis0)
  • end
  • end

CS 363 Spring 2005 GMU
54
55
Reverse - Smalltalk
  • reverse
  • self size 0 ifTrue ()asOrderedCollectio
    n).
  • ifFalse
  • (self allButFirst reverse) myappend
  • (OrderedCollection with self first).

CS 363 Spring 2005 GMU
55
56
Reverse - Perl
  • sub myReverse
  • my _at_templis
  • if (scalar(_at__) 0)
  • return ()
  • else
  • _at_templis myReverse(_at__1.._)
  • push(_at_templis, _0)
  • return _at_templis

CS 363 Spring 2005 GMU
56
57
Reverse - Prolog
  • myreverse(,).
  • myreverse(First,First).
  • myreverse(FirstRest, NewList) -
  • myreverse(Rest, ReversedList),
  • append(ReversedList,First, NewList).

CS 363 Spring 2005 GMU
57
58
Reverse - ML
  • fun reverse(L)
  • if L nil then nil
  • else reverse(tl(L)) _at_ hd(L)
  • fun reverse2(nil) nil
  • reverse2(firstrest)
  • reverse2(rest) _at_ first

CS 363 Spring 2005 GMU
58
59
Reverse - C/STL
  • listltintgt reverse(listltintgt lis)
  • if (lis.size() 0)
  • return lis
  • else
  • int first lis.begin()
  • lis.pop_front()
  • listltintgt reversed
  • reversed reverse(lis)
  • reversed.push_back(first)
  • return reversed

CS 363 Spring 2005 GMU
59
60
Reverse - Java
  • List reverse(List lis)
  • if (lis.isEmpty()) return lis
  • else
  • Integer first (Integer) lis.get(0)
  • List temp new ArrayList()
  • temp.addAll(reverse(lis.subList(1,lis.size())))
  • //makes a copy of reveresed list,
  • //temp reverse... affects lis
  • temp.add(temp.size(), first)
  • return temp

CS 363 Spring 2005 GMU
60
Write a Comment
User Comments (0)
About PowerShow.com