Download presentation
Presentation is loading. Please wait.
Published byDina Page Modified over 9 years ago
1
Comparative Programming Languages Language Comparison: Scheme, Smalltalk, Python, Ruby, Perl, Prolog, ML, C++/STL, Java
2
CS 363 Spring 2005 GMU2 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)
3
CS 363 Spring 2005 GMU3 Data Structures in Scheme: Box Notation for Lists first element (car)rest of list (cdr) 1 List manipulation is typically written using ‘car’ and ‘cdr’
4
CS 363 Spring 2005 GMU4 Data Structures in Scheme 1 23 (1,2,3) c da b ((a b) c (d))
5
CS 363 Spring 2005 GMU5 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))
6
CS 363 Spring 2005 GMU6 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']]
7
CS 363 Spring 2005 GMU7 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 copyFrom:2 #(1 2 3) first -> 1 #(($a $b)($c $d))) first -> ($a $b) #(1 2 3) copyFrom: 2 -> (2 3) #(($a $b)($c $d))) copyFrom:2 ->(($c $d))
8
CS 363 Spring 2005 GMU8 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) copyFrom: 2 -> (2 3) #(($a $b)($c $d))) copyFrom:2 ->(($c $d))
9
CS 363 Spring 2005 GMU9 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)
10
CS 363 Spring 2005 GMU10 Basic List Manipulation - Python >>> a = ['spam', 'eggs', 100, 1234] >>> a ['spam', 'eggs', 100, 1234] >>> a[0] 'spam' >>> a[3] 1234 >>> a[-2] 100 >>> a[1:-1] ['eggs', 100]
11
CS 363 Spring 2005 GMU11 Basic List Manipulation - Python >>> a[:2] + ['bacon', 2*2] ['spam', 'eggs', 'bacon', 4] >>> 3*a[:3] + ['Boe!'] ['spam', 'eggs', 100, 'spam', 'eggs', 100, 'spam', 'eggs', 100, 'Boe!'] List items can be changed: >>> a ['spam', 'eggs', 100, 1234] >>> a[2] = a[2] + 23 >>> a ['spam', 'eggs', 123, 1234]
12
CS 363 Spring 2005 GMU12 Basic List Manipulation - Python It is possible to nest lists (create lists containing other lists), for example: >>> q = [2, 3] >>> p = [1, q, 4] >>> len(p) 3 >>> p[1] [2, 3] >>> p[1][0] 2
13
CS 363 Spring 2005 GMU13 Basic List Manipulation - Python >>> p[1].append('xtra') >>> p [1, [2, 3, 'xtra'], 4] >>> q [2, 3, 'xtra'] Note that in the last example, p[1] and q really refer to the same object!
14
CS 363 Spring 2005 GMU14 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)
15
CS 363 Spring 2005 GMU15 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))) ))
16
CS 363 Spring 2005 GMU16 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:])
17
CS 363 Spring 2005 GMU17 Example Functions - Ruby 1. member - takes an atom and a simple list; returns t rue 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
18
CS 363 Spring 2005 GMU18 Example Functions - Smalltalk 1. member - takes an atom and a simple list; returns t rue 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
19
CS 363 Spring 2005 GMU19 Example Functions - Perl 1. member - takes an atom and a simple list; returns t rue 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]); }
20
CS 363 Spring 2005 GMU20 Example Functions - Prolog 1. member - takes an atom and a simple list; returns t rue 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]).
21
CS 363 Spring 2005 GMU21 Example Functions - ML 1. member - takes an atom and a simple list; returns t rue 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 *)
22
CS 363 Spring 2005 GMU22 Example Functions – C++, STL 1. member - takes an atom and a simple list; returns t rue if the atom is in the list; [] otherwise bool member(int atm, list lis) { if (lis.size() == 0) return false; else if (*lis.begin() == atm) return true; else { lis.pop_front(); return member(atm,lis); }
23
CS 363 Spring 2005 GMU23 Example Functions – Java 1. member - takes an atom and a simple list; returns t rue 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())); }
24
CS 363 Spring 2005 GMU24 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 '()) ))
25
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 lis1[0]== lis2[0]: return equalsimp(lis1[1:],lis2[1:]) else: return []
26
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.length==0 lis2.length == 0 elsif lis2.length==0 [] elsif lis1[0]== lis2[0]: equalsimp(lis1[1..lis1.length- 1],lis2[1..lis2.length-1]) else [] end
27
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:[ lis2=0 ifTrue: [ ^#().] ifFalse: [ self first = lis2 first ifTrue:[ self allButFirst equalsimp: lis2 allButFirst.] ifFalse: [^#().] ] end
28
CS 363 Spring 2005 GMU28 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 '()) ))
29
CS 363 Spring 2005 GMU29 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
30
CS 363 Spring 2005 GMU30 Example Functions - Python def count(L): if len(L)== 0: return 0 else: return 1 + count(L[1:])
31
CS 363 Spring 2005 GMU31 Example Functions - Ruby def count(lis) if lis.length== 0 then 0 else 1 + count(lis[1..lis.length-1]) end
32
CS 363 Spring 2005 GMU32 Example Functions - Smalltalk count self size = 0 ifTrue: [ ^0.] ifFalse: [ ^(1 + self allButFirst count). ]
33
CS 363 Spring 2005 GMU33 Example Functions - Perl sub count { my @ls; @ls = @_; if (scalar(@ls) == 1) { 1; } else { count(@ls[1..$#ls]) + 1; }
34
CS 363 Spring 2005 GMU34 Example Functions - Prolog count([],Total, Total). count([_|Rest], Counter,TotalCount) :- NewCount is Counter + 1, count(Rest, NewCount,TotalCount).
35
CS 363 Spring 2005 GMU35 Example Functions - ML fun count([]) = 0 |count(first::rest) = 1 + count(rest);
36
CS 363 Spring 2005 GMU36 Example Functions – C++, STL int count(list lis) { if (lis.size() == 0) return 0; else { lis.pop_front(); return 1 + count(lis); }
37
CS 363 Spring 2005 GMU37 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())); }
38
CS 363 Spring 2005 GMU38 Scheme Functions Now define (define (count1 L) ?? ) so that (count1 ‘(a (b c d) e)) = 5
39
CS 363 Spring 2005 GMU39 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
40
CS 363 Spring 2005 GMU40 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']
41
CS 363 Spring 2005 GMU41 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', 'c', 'd']
42
CS 363 Spring 2005 GMU42 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')
43
CS 363 Spring 2005 GMU43 Example Functions - Prolog append([],List, List). append([First|Rest],List2, [First|List3]) :- append(Rest, List2, List3).
44
CS 363 Spring 2005 GMU44 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))) =
45
CS 363 Spring 2005 GMU45 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)))) =
46
CS 363 Spring 2005 GMU46 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))) =
47
CS 363 Spring 2005 GMU47 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)) =
48
CS 363 Spring 2005 GMU48 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)
49
CS 363 Spring 2005 GMU49 append: C++/STL list append(list lis, list m) { if (lis.size() == 0) return m; else { int first = *lis.begin(); lis.pop_front(); list appendedList = append(lis, m); appendedList.push_front(first); return appendedList; }
50
CS 363 Spring 2005 GMU50 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; }
51
CS 363 Spring 2005 GMU51 Reverse - Scheme Write a function that takes a list of elements and reverses it: (reverse ‘(1 2 3 4)) = (4 3 2 1)
52
CS 363 Spring 2005 GMU52 Reverse - Scheme (define (reverse L) (if (null? L) ‘() (append (reverse (cdr L))(list (car L))) )
53
CS 363 Spring 2005 GMU53 Reverse - Python def reverse(L): if len(L)==0: return [] else: myappend (reverse (L[1:]), [L[0]])
54
CS 363 Spring 2005 GMU54 Reverse - Ruby def reverse(lis) if lis.length==0 then [] else myappend( reverse (lis[1..lis.length-1]), [lis[0]]) end
55
CS 363 Spring 2005 GMU55 Reverse - Smalltalk reverse self size = 0 ifTrue: [^ #()asOrderedCollection).] ifFalse: [ (self allButFirst reverse) myappend: (OrderedCollection with: self first).]
56
CS 363 Spring 2005 GMU56 Reverse - Perl sub myReverse { my @templis; if (scalar(@_) == 0) { return (); } else { @templis = myReverse(@_[1..$#_]); push(@templis, $_[0]); return @templis; }
57
CS 363 Spring 2005 GMU57 Reverse - Prolog myreverse([],[]). myreverse([First|[]],[First]). myreverse([First|Rest], NewList) :- myreverse(Rest, ReversedList), append(ReversedList,[First], NewList).
58
CS 363 Spring 2005 GMU58 Reverse - ML fun reverse(L) = if L = nil then nil else reverse(tl(L)) @ [hd(L)]; fun reverse2(nil) = nil | reverse2(first::rest) = reverse2(rest) @ [first]
59
CS 363 Spring 2005 GMU59 Reverse - C++/STL list reverse(list lis) { if (lis.size() == 0) return lis; else { int first = *lis.begin(); lis.pop_front(); list reversed; reversed = reverse(lis); reversed.push_back(first); return reversed; }
60
CS 363 Spring 2005 GMU60 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; }
Similar presentations
© 2025 SlidePlayer.com Inc.
All rights reserved.