Download presentation
Presentation is loading. Please wait.
Published byMervyn Webster Modified over 9 years ago
1
Comparative Programming Languages Language Comparison: Scheme, Smalltalk, Python, Ruby, Perl, Prolog, ML, C++/STL, Java, Haskell
2
CS 363 Spring 2005 GMU2 Selection Sort in Scheme Let’s define a few useful functions first: (DEFINE (findsmallest lis small) (COND ((NULL? lis) small) ((< (CAR lis) small) (findsmallest (CDR lis) (CAR lis))) (ELSE (findsmallest (CDR lis) small)) )
3
CS 363 Spring 2005 GMU3 Selection Sort in Scheme (DEFINE (remove lis item) (COND ((NULL? lis) ‘() ) ((= (CAR lis) item) (CDR lis)) (ELSE (CONS (CAR lis) (remove (CDR lis) item))) ) Cautious programming! Assuming integers
4
CS 363 Spring 2005 GMU4 Selection Sort in Scheme (DEFINE (selectionsort lis) (IF (NULL? lis) lis (LET ((s (findsmallest (CDR lis) (CAR lis)))) (CONS s (selectionsort (remove lis s)) ) )
5
CS 363 Spring 2005 GMU5 Selection Sort in Python def smallest(L, A): if len(L)==0: return A elif L[0] < A: return smallest(L[1:], L[0]) else: return smallest(L[1:], A)
6
CS 363 Spring 2005 GMU6 Selection Sort in Python def myremove(L, A): L.remove(A) return L
7
CS 363 Spring 2005 GMU7 Selection Sort in Python def selection(L): if len(L) == 0: return [] else: return [smallest(L,L[0])] + selection(myremove(L,smallest(L, L[0])))
8
CS 363 Spring 2005 GMU8 Selection Sort in Ruby def smallest(lis, a) if lis.length==0 then a elsif lis[0] < a then smallest(lis[1..lis.length-1], lis[0]) else smallest(lis[1..lis.length-1], a) end
9
CS 363 Spring 2005 GMU9 Selection Sort in Ruby def myremove(lis, a) lis.delete(a) lis end
10
CS 363 Spring 2005 GMU10 Selection Sort in Ruby def selection(lis) if lis.length == 0 then [] else [smallest(lis,lis[0])] + selection(myremove(lis,smallest(lis, lis[0]))) end
11
CS 363 Spring 2005 GMU11 Selection Sort in Smalltalk smallest: currentsmallest self size = 0 ifTrue: [^currentsmallest.] ifFalse: [ self first < currentsmallest ifTrue: [ ^self allButFirst smallest: self first.] ifFalse: [ ^self allButFirst smallest: currentsmallest]. ].
12
CS 363 Spring 2005 GMU12 Selection Sort in Smalltalk myRemove: item ^self removeAt: (self find: item). !
13
CS 363 Spring 2005 GMU13 Selection Sort in Smalltalk selection "(FileStream oldFileNamed: 'selection.st') fileIn. Numbers := #(4 3 16 1 14 25 2) asOrderedCollection. numbers selection" | currentsmallest | self size = 0 ifTrue: [ ^#() asOrderedCollection.] ifFalse: [ currentsmallest := self smallest: self first. ^(OrderedCollection with: currentsmallest), (self myRemove: (self smallest: self first)) selection. ].
14
CS 363 Spring 2005 GMU14 Selection Sort in Perl sub smallest { my ($element, @lis) = @_; if (scalar(@lis) == 0) { return $element; } elsif ($lis[0] < $element) { return smallest(@lis[1..$#lis], $lis[0]); } else { return smallest(@lis[1..$#lis], $element); }
15
CS 363 Spring 2005 GMU15 Selection Sort in Perl sub remove { my ($element, @lis) = @_; my @templis; if (scalar(@lis) == 0) { return (); } elsif ($lis[0] == $element) { return @lis[1..$#lis]; } else { @templis = remove($element, @lis[1..$#lis]); unshift(@templis, $lis[0]); return @templis; }
16
CS 363 Spring 2005 GMU16 Selection Sort in Perl sub selectionsort { my @templis; my @templis2; my $s; if (scalar(@_) == 0) { return (); } else { $s = smallest(@_[1..$#_], $_[0]); @templis = selectionsort(remove($s, @_)); unshift(@templis, $s); return @templis; }
17
CS 363 Spring 2005 GMU17 Selection Sort in Prolog smallest([], Smallest,Smallest). smallest([First|Rest], CurrSmallest, Smallest) :- First < CurrSmallest, smallest(Rest, First, Smallest). smallest([_|Rest], CurrSmallest, Smallest) :- smallest(Rest, CurrSmallest, Smallest).
18
CS 363 Spring 2005 GMU18 Selection Sort in Prolog remove([], _, []). remove([First|Rest], First, Rest). remove([First|Rest], Element, [First|NewList]) :- remove(Rest, Element, NewList).
19
CS 363 Spring 2005 GMU19 Selection Sort in Prolog selectionsort([],[]). selectionsort([First|Rest], [Smallest|SortedList]) :- smallest(Rest, First, Smallest), remove([First|Rest], Smallest, NewList), selectionsort(NewList, SortedList).
20
CS 363 Spring 2005 GMU20 Selection Sort in ML fun smallest([], a) = a | smallest(first::rest, a) = if first < a then smallest(rest, first) else smallest(rest, a);
21
CS 363 Spring 2005 GMU21 Selection Sort in ML fun remove([], _) = [] | remove(first::rest, item) = if first = item then rest else first::remove(rest, item);
22
CS 363 Spring 2005 GMU22 Selection Sort in ML fun selectionsort([]) = [] | selectionsort(first::rest) = let val s = smallest(rest, first); in s::selectionsort(remove(first::rest,s)) end;
23
CS 363 Spring 2005 GMU23 Selection Sort in ML (* - use "selection.sml"; val it = [1,2,3,5] : int list - remove([1,2,3,4,5], 5); val it = [1,2,3,4] : int list - remove([1,2,3,4,5], 1); val it = [2,3,4,5] : int list - smallest([3,14,5,1,18,2], 3); val it = 1 : int *)
24
CS 363 Spring 2005 GMU24 Selection Sort in C++/STL int smallest(list lis, int small) { if (lis.size() == 0) return small; else if (*lis.begin() < small) { int first = *lis.begin(); lis.pop_front(); return smallest(lis, first); } else { lis.pop_front(); return smallest(lis, small); }
25
CS 363 Spring 2005 GMU25 Selection Sort in C++/STL list remove(list lis, int item) { if (lis.size() == 0) return lis; else if (*lis.begin() == item) { lis.pop_front(); return lis; } else { int first = *lis.begin(); lis.pop_front(); list removedList = remove(lis, item); removedList.push_front(first); return removedList; }
26
CS 363 Spring 2005 GMU26 Selection Sort in C++/STL list selectionsort(list lis) { if (lis.size() == 0) return lis; else { int first = *lis.begin(); list rest = lis; rest.pop_front(); int s = smallest(rest, first); list sortedList = selectionsort(remove(lis,s)); sortedList.push_front(s); return sortedList; }
27
CS 363 Spring 2005 GMU27 Selection Sort in Java int smallest(List lis, int small) { if (lis.isEmpty()) return small; else if (((Integer) lis.get(0)).intValue() < small) return smallest(lis.subList(1,lis.size()), ((Integer) lis.get(0)).intValue()); else return smallest(lis.subList(1,lis.size()), small); }
28
CS 363 Spring 2005 GMU28 Selection Sort in Java List remove(List lis, int item) { if (lis.isEmpty()) return lis; else if (((Integer) lis.get(0)).intValue() == item) return lis.subList(1,lis.size()); else { Integer first = (Integer) lis.get(0); List temp = new ArrayList(); temp.addAll(remove(lis.subList(1,lis.size()),item)); temp.add(0,first); return temp; }
29
CS 363 Spring 2005 GMU29 Selection Sort in Java List selectionsort(List lis) { if (lis.isEmpty()) return lis; else { Integer first = (Integer) lis.get(0); int s = smallest(lis.subList(1,lis.size()), first.intValue()); List sorted = new ArrayList(); List removeList = new ArrayList(); removeList.addAll(remove(lis,s)); sorted.addAll(selectionsort(removeList)); sorted.add(0, new Integer(s)); return sorted; }
30
CS 363 Spring 2005 GMU30 Higher order functions - Scheme Def: A higher-order function, or functional form, is one that either takes functions as parameters, yields a function as its result, or both Mapcar Eval
31
CS 363 Spring 2005 GMU31 Higher-Order Functions: mapcar Apply to All - mapcar - Applies the given function to all elements of the given list; result is a list of the results (DEFINE (mapcar fun lis) (COND ((NULL? lis) '()) (ELSE (CONS (fun (CAR lis)) (mapcar fun (CDR lis)))) ))
32
CS 363 Spring 2005 GMU32 Higher-Order Functions: mapcar Scheme Using mapcar: (mapcar (LAMBDA (num) (* num num num)) ‘(3 4 2 6)) returns (27 64 8 216)
33
CS 363 Spring 2005 GMU33 Higher Order Functions: EVAL Scheme It is possible in Scheme to define a function that builds Scheme code and requests its interpretation This is possible because the interpreter is a user-available function, EVAL
34
CS 363 Spring 2005 GMU34 Using EVAL for adding a List of Numbers Suppose we have a list of numbers that must be added: (DEFINE (adder lis) (COND((NULL? lis) 0) (ELSE (+ (CAR lis) (adder(CDR lis )))) )) Using Eval ((DEFINE (adder lis) (COND ((NULL? lis) 0) (ELSE (EVAL (CONS '+ lis))) )) (adder ‘(3 4 5 6 6)) Returns 24
35
CS 363 Spring 2005 GMU35 Other Features of Scheme Scheme includes some imperative features: 1. SET! binds or rebinds a value to a name 2. SET-CAR! replaces the car of a list 3. SET-CDR! replaces the cdr part of a list
36
CS 363 Spring 2005 GMU36 COMMON LISP A combination of many of the features of the popular dialects of LISP around in the early 1980s A large and complex language – the opposite of Scheme
37
CS 363 Spring 2005 GMU37 COMMON LISP Includes: –records –arrays –complex numbers –character strings –powerful I/O capabilities –packages with access control –imperative features like those of Scheme –iterative control statements
38
CS 363 Spring 2005 GMU38 ML A static-scoped functional language with syntax that is closer to Pascal than to LISP Uses type declarations, but also does type inferencing to determine the types of undeclared variables It is strongly typed (whereas Scheme is essentially typeless) and has no type coercions Includes exception handling and a module facility for implementing abstract data types
39
CS 363 Spring 2005 GMU39 ML Includes lists and list operations The val statement binds a name to a value (similar to DEFINE in Scheme) Function declaration form: fun function_name (formal_parameters) = function_body_expression; e.g., fun cube (x : int) = x * x * x;
40
CS 363 Spring 2005 GMU40 Haskell Similar to ML (syntax, static scoped, strongly typed, type inferencing) Different from ML (and most other functional languages) in that it is purely functional (e.g., no variables, no assignment statements, and no side effects of any kind)
41
CS 363 Spring 2005 GMU41 Haskell Most Important Features –Uses lazy evaluation (evaluate no subexpression until the value is needed) –Has list comprehensions, which allow it to deal with infinite lists
42
CS 363 Spring 2005 GMU42 Haskell Examples 1. Fibonacci numbers (illustrates function definitions with different parameter forms) fib 0 = 1 fib 1 = 1 fib (n + 2) = fib (n + 1) + fib n
43
CS 363 Spring 2005 GMU43 Haskell Examples 2. Factorial (illustrates guards) fact n | n == 0 = 1 | n > 0 = n * fact (n - 1) The special word otherwise can appear as a guard
44
CS 363 Spring 2005 GMU44 Haskell Examples 3. List operations –List notation: Put elements in brackets e.g., directions = [“north”, “south”, “east”, “west”] –Length: # e.g., #directions is 4 –Arithmetic series with the.. operator e.g., [2, 4..10] is [2, 4, 6, 8, 10]
45
CS 363 Spring 2005 GMU45 Haskell Examples 3. List operations (cont) –Catenation is with ++ e.g., [1, 3] ++ [5, 7] results in [1, 3, 5, 7] –CONS, CAR, CDR via the colon operator (as in Prolog) e.g., 1:[3, 5, 7] results in [1, 3, 5, 7]
46
CS 363 Spring 2005 GMU46 Haskell Examples Quicksort: sort [] = [] sort (a:x) = sort [b | b ← x; b <= a] ++ [a] ++ sort [b | b ← x; b > a]
47
CS 363 Spring 2005 GMU47 Applications of Functional Languages LISP is used for artificial intelligence –Knowledge representation –Machine learning –Natural language processing –Modeling of speech and vision Scheme is used to teach introductory programming at a significant number of universities
48
CS 363 Spring 2005 GMU48 Comparing Functional and Imperative Languages Imperative Languages: –Efficient execution –Complex semantics –Complex syntax –Concurrency is programmer designed Functional Languages: –Simple semantics –Simple syntax –Inefficient execution –Programs can automatically be made concurrent
Similar presentations
© 2024 SlidePlayer.com. Inc.
All rights reserved.