Comparative Programming Languages Language Comparison Scheme Smalltalk Python

  • Slides: 60
Download presentation
Comparative Programming Languages Language Comparison: Scheme, Smalltalk, Python, Ruby, Perl, Prolog, ML, C++/STL, Java

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

Function Evaluation – Lisp/Scheme • Evaluation process (for normal functions): 1. Parameters are evaluated,

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

Data Structures in Scheme: Box Notation for Lists CS 363 Spring 2005 GMU 3

Data Structures in Scheme: Box Notation for Lists CS 363 Spring 2005 GMU 3

Data Structures in Scheme CS 363 Spring 2005 GMU 4

Data Structures in Scheme CS 363 Spring 2005 GMU 4

Basic List Manipulation • (car L) – returns the first element of L •

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

Basic List Manipulation - Python • L[0] – returns the first element of L

Basic List Manipulation - Python • L[0] – returns the first element of L • L[1: ] – returns L minus the first element x, y = [1, 2, 3], [['a', 'b'], ['c', 'd']] x[0] = 1 y[0] = ['a', 'b'] x[1: ] = [2, 3] y[1: ] = [['c', 'd']] CS 363 Spring 2005 GMU 6

Basic List Manipulation Smalltalk • L first – returns the first element of L

Basic List Manipulation Smalltalk • L first – returns the first element of L • L all. But. First – returns L minus the first element (Squeak), in gst use L copy. From: 2 #(1 2 3) first -> 1 #(($a $b)($c $d))) first -> ($a $b) #(1 2 3) copy. From: 2 -> (2 3) #(($a $b)($c $d))) copy. From: 2 ->(($c $d)) CS 363 Spring 2005 GMU 7

Basic List Manipulation Ruby • L. first – returns the first element of L

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 -> 1 [['a', 'b'] ['c', 'd']]. first ->[“a”, ”b”] #(1 2 3) copy. From: 2 -> (2 3) #(($a $b)($c $d))) copy. From: 2 ->(($c $d)) CS 363 Spring 2005 GMU 8

Basic List Manipulation • (list e 1 … en) – return the list created

Basic List Manipulation • (list e 1 … 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

Basic List Manipulation - Python >>> a = ['spam', 'eggs', 100, 1234] >>> a[0]

Basic List Manipulation - Python >>> a = ['spam', 'eggs', 100, 1234] >>> a[0] 'spam' >>> a[3] 1234 >>> a[-2] 100 >>> a[1: -1] ['eggs', 100] CS 363 Spring 2005 GMU 10

Basic List Manipulation - Python >>> a[: 2] + ['bacon', 2*2] ['spam', 'eggs', 'bacon',

Basic List Manipulation - Python >>> a[: 2] + ['bacon', 2*2] ['spam', 'eggs', 'bacon', 4] >>> 3*a[: 3] + ['Boe!'] ['spam', 'eggs', 100, 'Boe!'] List items can be changed: >>> a ['spam', 'eggs', 100, 1234] >>> a[2] = a[2] + 23 >>> a ['spam', 'eggs', 1234] CS 363 Spring 2005 GMU 11

Basic List Manipulation - Python It is possible to nest lists (create lists containing

Basic List Manipulation - Python It is possible to nest lists (create lists containing other lists), for example: >>> >>> 3 >>> [2, >>> 2 q = [2, 3] p = [1, q, 4] len(p) p[1] 3] p[1][0] CS 363 Spring 2005 GMU 12

Basic List Manipulation - Python >>> [1, >>> [2, p[1]. append('xtra') p [2, 3,

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

Basic List Manipulation - Python >>> x = [3, 4, 5] >>> y =

Basic List Manipulation - Python >>> x = [3, 4, 5] >>> y = ['a', 'b', 'c'] >>> [y[0]] + x # like (cons (car y) x) >>> y[1: ] # like (cdr y) CS 363 Spring 2005 GMU 14

Example Functions - Scheme 1. member - takes an atom and a simple list;

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

Example Functions - Python 1. member - takes an atom and a simple list;

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 == lis[0]: True else member(atm, lis[1: ]) CS 363 Spring 2005 GMU 16

Example Functions - Ruby 1. member - takes an atom and a simple list;

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 == lis[0] then true else member(atm, lis[1. . lis. length-1]) end CS 363 Spring 2005 GMU 17

Example Functions - Smalltalk 1. member - takes an atom and a simple list;

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 if. True: [ ^#(). ] if. False: [ atm = self first if. True: [ ^true. ] if. False: [ ^self all. But. First member: atm CS 363 Spring 2005 GMU 18

Example Functions - Perl 1. member - takes an atom and a simple list;

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 @ls; ($atm, @ls) = @_; if (scalar(@ls) == 0) { return 0; } elsif ($ls[0] eq $atm) { return 1; } else { member($atm, @ls[1. . $#ls]); } } CS 363 Spring 2005 GMU 19

Example Functions - Prolog 1. member - takes an atom and a simple list;

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

Example Functions - ML 1. member - takes an atom and a simple list;

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, first: : rest) = if atm=first then true else member(atm, rest); (* use - use "member. sml"; val member = fn : ''a * ''a list -> bool val it = () : unit - member(3, [3, 4, 5]); val it = true : bool CS 363 Spring 2005 GMU *) 21

Example Functions – C++, STL 1. member - takes an atom and a simple

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, list<int> 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

Example Functions – Java 1. member - takes an atom and a simple list;

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. Empty()) return false; else if (((Integer) lis. get(0)). int. Value()== atm) return true; else return member(atm, lis. sub. List(1, lis. size())); } CS 363 Spring 2005 GMU 23

Example Functions - Scheme 2. equalsimp - takes two simple lists as parameters; returns

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

Example Functions - Python 2. equalsimp - takes two simple lists as parameters; returns

Example Functions - Python 2. equalsimp - takes two simple lists as parameters; returns true if the two simple lists are equal; () otherwise def equalsimp(lis 1, lis 2): if len(lis 1)==0 return len(lis 2) == 0 elif len(lis 2)==0 return [] elif lis 1[0]== lis 2[0]: return equalsimp(lis 1[1: ], lis 2[1: ]) else: return [] 25

Example Functions - Ruby 2. equalsimp - takes two simple lists as parameters; returns

Example Functions - Ruby 2. equalsimp - takes two simple lists as parameters; returns true if the two simple lists are equal; () otherwise def equalsimp(lis 1, lis 2) if lis 1. length==0 lis 2. length == 0 elsif lis 2. length==0 [] elsif lis 1[0]== lis 2[0]: equalsimp(lis 1[1. . lis 1. length 1], lis 2[1. . lis 2. length-1]) else [] end 26

Example Functions - Smalltalk 2. equalsimp - takes two simple lists as parameters; returns

Example Functions - Smalltalk 2. equalsimp - takes two simple lists as parameters; returns true if the two simple lists are equal; () otherwise equalsimp: lis 2 self size = 0 if. True: [ ^lis 2 size = 0. ] if. False: [ lis 2=0 if. True: [ ^#(). ] if. False: [ self first = lis 2 first if. True: [ self all. But. First equalsimp: lis 2 all. But. First. ] if. False: [^#(). ] ] ] end 27

Example Functions - Scheme 3. equal - takes two general lists as parameters; returns

Example Functions - Scheme 3. equal - takes two general lists as parameters; returns #T if the two lists are equal; ()otherwise (DEFINE (equal lis 1 lis 2) (COND ((NOT (LIST? lis 1))(EQ? lis 1 lis 2)) ((NOT (LIST? lis 2)) '()) ((NULL? lis 1) (NULL? lis 2)) ((NULL? lis 2) '()) ((equal (CAR lis 1) (CAR lis 2)) (equal (CDR lis 1) (CDR lis 2))) (ELSE '()) )) CS 363 Spring 2005 GMU 28

Example Functions - Scheme (define (count L) (if (null? L) 0 (+ 1 (count

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(count ‘(c d)))) (+ 1 (count ‘(d)))))= (+ 1 (count ‘())))))= (+ 1 0))))= 4 CS 363 Spring 2005 GMU 29

Example Functions - Python def count(L): if len(L)== 0: return 0 else: return 1

Example Functions - Python def count(L): if len(L)== 0: return 0 else: return 1 + count(L[1: ]) CS 363 Spring 2005 GMU 30

Example Functions - Ruby def count(lis) if lis. length== 0 then 0 else 1

Example Functions - Ruby def count(lis) if lis. length== 0 then 0 else 1 + count(lis[1. . lis. length-1]) end CS 363 Spring 2005 GMU 31

Example Functions - Smalltalk count self size = 0 if. True: [ ^0. ]

Example Functions - Smalltalk count self size = 0 if. True: [ ^0. ] if. False: [ ^(1 + self all. But. First count). ] CS 363 Spring 2005 GMU 32

Example Functions - Perl sub count { my @ls; @ls = @_; if (scalar(@ls)

Example Functions - Perl sub count { my @ls; @ls = @_; if (scalar(@ls) == 1) { 1; } else { count(@ls[1. . $#ls]) + 1; } } CS 363 Spring 2005 GMU 33

Example Functions - Prolog count([], Total). count([_|Rest], Counter, Total. Count) : New. Count is

Example Functions - Prolog count([], Total). count([_|Rest], Counter, Total. Count) : New. Count is Counter + 1, count(Rest, New. Count, Total. Count). CS 363 Spring 2005 GMU 34

Example Functions - ML fun count([]) = 0 | count(first: : rest) = 1

Example Functions - ML fun count([]) = 0 | count(first: : rest) = 1 + count(rest); CS 363 Spring 2005 GMU 35

Example Functions – C++, STL int count(list<int> lis) { if (lis. size() == 0)

Example Functions – C++, STL int count(list<int> lis) { if (lis. size() == 0) return 0; else { lis. pop_front(); return 1 + count(lis); } } CS 363 Spring 2005 GMU 36

Example Functions – Java int count(List lis) { if (lis. Empty()) // or lis.

Example Functions – Java int count(List lis) { if (lis. Empty()) // or lis. size() == 0 return 0; else return 1 + count(lis. sub. List(1, lis. size())); } CS 363 Spring 2005 GMU 37

Scheme Functions Now define (count 1 L) ? ? ) so that (count 1

Scheme Functions Now define (count 1 L) ? ? ) so that (count 1 ‘(a (b c d) e)) = 5 CS 363 Spring 2005 GMU 38

Scheme Functions This function counts the individual elements: (define (count 1 L) (cond (

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

Example Functions - Python def myappend(L, M) if len(L)==0: return M else: [L[0]] +

Example Functions - Python def myappend(L, M) if len(L)==0: return M else: [L[0]] + myappend(L[1: ], M)) myappend(['a', 'b'], ['c', 'd'] = ['a', 'b', 'c', 'd'] CS 363 Spring 2005 GMU 40

Example Functions - Ruby def myappend(lis, m) if lis. length==0 then M else [lis[0]]

Example Functions - Ruby def myappend(lis, m) if lis. length==0 then M else [lis[0]] + myappend(lis[1. . lis. length-1], M) end myappend(['a', 'b'], ['c', 'd'] = ['a', 'b', CS'c', 'd'] 363 Spring 2005 GMU 41

Example Functions - Smalltalk myappend: m) self size = 0 if. True: [^m. ]

Example Functions - Smalltalk myappend: m) self size = 0 if. True: [^m. ] if. False: [ ^(Ordered. List with: self first), self all. But. First myappend: m. ] #(a b) myappend: ('c' 'd') = ('a' 'b' 'c' 'd') CS 363 Spring 2005 GMU 42

Example Functions - Prolog append([], List). append([First|Rest], List 2, [First|List 3]) : append(Rest, List

Example Functions - Prolog append([], List). append([First|Rest], List 2, [First|List 3]) : append(Rest, List 2, List 3). CS 363 Spring 2005 GMU 43

How does append do its job? (define (append L M) (if (null? L) M

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

How does append do its job? (define (append L M) (if (null? L) M

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

How does append do its job? (define (append L M) (if (null? L) M

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

How does append do its job? (define (append L M) (if (null? L) M

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

How does append do its job? (define (append L M) (if (null? L) M

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

append: C++/STL list<int> append(list<int> lis, list<int>m) { if (lis. size() == 0) return m;

append: C++/STL list<int> append(list<int> lis, list<int>m) { if (lis. size() == 0) return m; else { int first = *lis. begin(); lis. pop_front(); list<int> appended. List = append(lis, m); appended. List. push_front(first); return appended. List; } } CS 363 Spring 2005 GMU 49

append: Java List append(List lis, List m) { if (lis. Empty()) return m; else

append: Java List append(List lis, List m) { if (lis. Empty()) return m; else { Integer first = (Integer) lis. get(0); List temp = new Array. List(); temp. add. All( append(lis. sub. List(1, lis. size()), m)); temp. add(0, first); return temp; } } CS 363 Spring 2005 GMU 50

Reverse - Scheme Write a function that takes a list of elements and reverses

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

Reverse - Scheme (define (reverse L) (if (null? L) ‘() (append (reverse (cdr L))(list

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

Reverse - Python def reverse(L): if len(L)==0: return [] else: myappend (reverse (L[1: ]),

Reverse - Python def reverse(L): if len(L)==0: return [] else: myappend (reverse (L[1: ]), [L[0]]) CS 363 Spring 2005 GMU 53

Reverse - Ruby def reverse(lis) if lis. length==0 then [] else myappend( reverse (lis[1.

Reverse - Ruby def reverse(lis) if lis. length==0 then [] else myappend( reverse (lis[1. . lis. length-1]), [lis[0]]) end CS 363 Spring 2005 GMU 54

Reverse - Smalltalk reverse self size = 0 if. True: [^ #()as. Ordered. Collection).

Reverse - Smalltalk reverse self size = 0 if. True: [^ #()as. Ordered. Collection). ] if. False: [ (self all. But. First reverse) myappend: (Ordered. Collection with: self first). ] CS 363 Spring 2005 GMU 55

Reverse - Perl sub my. Reverse { my @templis; if (scalar(@_) == 0) {

Reverse - Perl sub my. Reverse { my @templis; if (scalar(@_) == 0) { return (); } else { @templis = my. Reverse(@_[1. . $#_]); push(@templis, $_[0]); return @templis; } } CS 363 Spring 2005 GMU 56

Reverse - Prolog myreverse([], []). myreverse([First|[]], [First]). myreverse([First|Rest], New. List) : myreverse(Rest, Reversed. List),

Reverse - Prolog myreverse([], []). myreverse([First|[]], [First]). myreverse([First|Rest], New. List) : myreverse(Rest, Reversed. List), append(Reversed. List, [First], New. List). CS 363 Spring 2005 GMU 57

Reverse - ML fun reverse(L) = if L = nil then nil else reverse(tl(L))

Reverse - ML fun reverse(L) = if L = nil then nil else reverse(tl(L)) @ [hd(L)]; fun reverse 2(nil) = nil | reverse 2(first: : rest) = reverse 2(rest) @ [first] CS 363 Spring 2005 GMU 58

Reverse - C++/STL list<int> reverse(list<int> lis) { if (lis. size() == 0) return lis;

Reverse - C++/STL list<int> reverse(list<int> lis) { if (lis. size() == 0) return lis; else { int first = *lis. begin(); lis. pop_front(); list<int> reversed; reversed = reverse(lis); reversed. push_back(first); return reversed; } CS 363 Spring 2005 GMU } 59

Reverse - Java List reverse(List lis) { if (lis. Empty()) return lis; else {

Reverse - Java List reverse(List lis) { if (lis. Empty()) return lis; else { Integer first = (Integer) lis. get(0); List temp = new Array. List(); temp. add. All(reverse(lis. sub. List(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