Comparative Programming Languages Language Comparison Scheme Smalltalk Python




![Selection Sort in Python def smallest(L, A): if len(L)==0: return A elif L[0] < Selection Sort in Python def smallest(L, A): if len(L)==0: return A elif L[0] <](https://slidetodoc.com/presentation_image_h2/0a07acc8557c606af718021c07587583/image-5.jpg)

![Selection Sort in Python def selection(L): if len(L) == 0: return [] else: return Selection Sort in Python def selection(L): if len(L) == 0: return [] else: return](https://slidetodoc.com/presentation_image_h2/0a07acc8557c606af718021c07587583/image-7.jpg)
![Selection Sort in Ruby def smallest(lis, a) if lis. length==0 then a elsif lis[0] Selection Sort in Ruby def smallest(lis, a) if lis. length==0 then a elsif lis[0]](https://slidetodoc.com/presentation_image_h2/0a07acc8557c606af718021c07587583/image-8.jpg)

![Selection Sort in Ruby def selection(lis) if lis. length == 0 then [] else Selection Sort in Ruby def selection(lis) if lis. length == 0 then [] else](https://slidetodoc.com/presentation_image_h2/0a07acc8557c606af718021c07587583/image-10.jpg)
![Selection Sort in Smalltalk smallest: currentsmallest self size = 0 if. True: [^currentsmallest. ] Selection Sort in Smalltalk smallest: currentsmallest self size = 0 if. True: [^currentsmallest. ]](https://slidetodoc.com/presentation_image_h2/0a07acc8557c606af718021c07587583/image-11.jpg)





![Selection Sort in Prolog smallest([], Smallest). smallest([First|Rest], Curr. Smallest, Smallest) : First < Curr. Selection Sort in Prolog smallest([], Smallest). smallest([First|Rest], Curr. Smallest, Smallest) : First < Curr.](https://slidetodoc.com/presentation_image_h2/0a07acc8557c606af718021c07587583/image-17.jpg)
![Selection Sort in Prolog remove([], _, []). remove([First|Rest], First, Rest). remove([First|Rest], Element, [First|New. List]) Selection Sort in Prolog remove([], _, []). remove([First|Rest], First, Rest). remove([First|Rest], Element, [First|New. List])](https://slidetodoc.com/presentation_image_h2/0a07acc8557c606af718021c07587583/image-18.jpg)
![Selection Sort in Prolog selectionsort([], []). selectionsort([First|Rest], [Smallest|Sorted. List]) : smallest(Rest, First, Smallest), remove([First|Rest], Selection Sort in Prolog selectionsort([], []). selectionsort([First|Rest], [Smallest|Sorted. List]) : smallest(Rest, First, Smallest), remove([First|Rest],](https://slidetodoc.com/presentation_image_h2/0a07acc8557c606af718021c07587583/image-19.jpg)
![Selection Sort in ML fun smallest([], a) = a | smallest(first: : rest, a) Selection Sort in ML fun smallest([], a) = a | smallest(first: : rest, a)](https://slidetodoc.com/presentation_image_h2/0a07acc8557c606af718021c07587583/image-20.jpg)
![Selection Sort in ML fun remove([], _) = [] | remove(first: : rest, item) Selection Sort in ML fun remove([], _) = [] | remove(first: : rest, item)](https://slidetodoc.com/presentation_image_h2/0a07acc8557c606af718021c07587583/image-21.jpg)
![Selection Sort in ML fun selectionsort([]) = [] | selectionsort(first: : rest) = let Selection Sort in ML fun selectionsort([]) = [] | selectionsort(first: : rest) = let](https://slidetodoc.com/presentation_image_h2/0a07acc8557c606af718021c07587583/image-22.jpg)























![Haskell Examples • Quicksort: sort [] = [] sort (a: x) = sort [b Haskell Examples • Quicksort: sort [] = [] sort (a: x) = sort [b](https://slidetodoc.com/presentation_image_h2/0a07acc8557c606af718021c07587583/image-46.jpg)


- Slides: 48
Comparative Programming Languages Language Comparison: Scheme, Smalltalk, Python, Ruby, Perl, Prolog, ML, C++/STL, Java, Haskell
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)) ) ) CS 363 Spring 2005 GMU 2
Selection Sort in Scheme (DEFINE (remove lis item) (COND ((NULL? lis) ‘() ) ((= (CAR lis) item) (CDR lis)) (ELSE (CONS (CAR lis) (remove (CDR lis) item))) ) ) CS 363 Spring 2005 GMU 3
Selection Sort in Scheme (DEFINE (selectionsort lis) (IF (NULL? lis) lis (LET ((s (findsmallest (CDR lis) (CAR lis)))) (CONS s (selectionsort (remove lis s)) ) CS 363 Spring 2005 GMU 4
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) CS 363 Spring 2005 GMU 5
Selection Sort in Python def myremove(L, A): L. remove(A) return L CS 363 Spring 2005 GMU 6
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]))) CS 363 Spring 2005 GMU 7
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 CS 363 Spring 2005 GMU 8
Selection Sort in Ruby def myremove(lis, a) lis. delete(a) lis end CS 363 Spring 2005 GMU 9
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 CS 363 Spring 2005 GMU 10
Selection Sort in Smalltalk smallest: currentsmallest self size = 0 if. True: [^currentsmallest. ] if. False: [ self first < currentsmallest if. True: [ ^self all. But. First smallest: self first. ] if. False: [ ^self all. But. First smallest: currentsmallest]. ]. CS 363 Spring 2005 GMU 11
Selection Sort in Smalltalk my. Remove: item ^self remove. At: (self find: item). ! CS 363 Spring 2005 GMU 12
Selection Sort in Smalltalk selection "(File. Stream old. File. Named: 'selection. st') file. In. Numbers : = #(4 3 16 1 14 25 2) as. Ordered. Collection. numbers selection" | currentsmallest | self size = 0 if. True: [ ^#() as. Ordered. Collection. ] if. False: [ currentsmallest : = self smallest: self first. ^(Ordered. Collection with: currentsmallest), (self my. Remove: (self smallest: self first)) selection. ]. CS 363 Spring 2005 GMU 13
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); } } CS 363 Spring 2005 GMU 14
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; } CS 363 Spring 2005 GMU 15 }
Selection Sort in Perl sub selectionsort { my @templis; my @templis 2; my $s; if (scalar(@_) == 0) { return (); } else { $s = smallest(@_[1. . $#_], $_[0]); @templis = selectionsort(remove($s, @_)); unshift(@templis, $s); return @templis; } CS 363 Spring 2005 GMU } 16
Selection Sort in Prolog smallest([], Smallest). smallest([First|Rest], Curr. Smallest, Smallest) : First < Curr. Smallest, smallest(Rest, First, Smallest). smallest([_|Rest], Curr. Smallest, Smallest) : smallest(Rest, Curr. Smallest, Smallest). CS 363 Spring 2005 GMU 17
Selection Sort in Prolog remove([], _, []). remove([First|Rest], First, Rest). remove([First|Rest], Element, [First|New. List]) : remove(Rest, Element, New. List). CS 363 Spring 2005 GMU 18
Selection Sort in Prolog selectionsort([], []). selectionsort([First|Rest], [Smallest|Sorted. List]) : smallest(Rest, First, Smallest), remove([First|Rest], Smallest, New. List), selectionsort(New. List, Sorted. List). CS 363 Spring 2005 GMU 19
Selection Sort in ML fun smallest([], a) = a | smallest(first: : rest, a) = if first < a then smallest(rest, first) else smallest(rest, a); CS 363 Spring 2005 GMU 20
Selection Sort in ML fun remove([], _) = [] | remove(first: : rest, item) = if first = item then rest else first: : remove(rest, item); CS 363 Spring 2005 GMU 21
Selection Sort in ML fun selectionsort([]) = [] | selectionsort(first: : rest) = let val s = smallest(rest, first); in s: : selectionsort(remove(first: : rest, s)) end; CS 363 Spring 2005 GMU 22
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 CS 363 Spring 2005 GMU *) 23
Selection Sort in C++/STL int smallest(list<int> 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); } } CS 363 Spring 2005 GMU 24
Selection Sort in C++/STL list<int> remove(list<int> 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<int> removed. List = remove(lis, item); removed. List. push_front(first); return removed. List; } CS 363 Spring 2005 GMU } 25
Selection Sort in C++/STL list<int> selectionsort(list<int> lis) { if (lis. size() == 0) return lis; else { int first = *lis. begin(); list<int> rest = lis; rest. pop_front(); int s = smallest(rest, first); list<int> sorted. List = selectionsort(remove(lis, s)); sorted. List. push_front(s); return sorted. List; } } CS 363 Spring 2005 GMU 26
Selection Sort in Java int smallest(List lis, int small) { if (lis. Empty()) return small; else if (((Integer) lis. get(0)). int. Value() < small) return smallest(lis. sub. List(1, lis. size()), ((Integer) lis. get(0)). int. Value()); else return smallest(lis. sub. List(1, lis. size()), small); } CS 363 Spring 2005 GMU 27
Selection Sort in Java List remove(List lis, int item) { if (lis. Empty()) return lis; else if (((Integer) lis. get(0)). int. Value() == item) return lis. sub. List(1, lis. size()); else { Integer first = (Integer) lis. get(0); List temp = new Array. List(); temp. add. All(remove(lis. sub. List(1, lis. size()), item)); temp. add(0, first); return temp; } } CS 363 Spring 2005 GMU 28
Selection Sort in Java List selectionsort(List lis) { if (lis. Empty()) return lis; else { Integer first = (Integer) lis. get(0); int s = smallest(lis. sub. List(1, lis. size()), first. int. Value()); List sorted = new Array. List(); List remove. List = new Array. List(); remove. List. add. All(remove(lis, s)); sorted. add. All(selectionsort(remove. List)); sorted. add(0, new Integer(s)); return sorted; } } CS 363 Spring 2005 GMU 29
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 CS 363 Spring 2005 GMU 30
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)))) )) CS 363 Spring 2005 GMU 31
Higher-Order Functions: mapcar Scheme • Using mapcar: (mapcar (LAMBDA (num) (* num num)) ‘(3 4 2 6)) returns (27 64 8 216) CS 363 Spring 2005 GMU 32
Higher Order Functions: EVAL Scheme • It is possible in Scheme to define a function that builds Scheme code and requests interpretation • This is possible because the interpreter is a user-available function, EVAL CS 363 Spring 2005 GMU 33
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 CS 363 Spring 2005 GMU 34
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 CS 363 Spring 2005 GMU 35
COMMON LISP • A combination of many of the features of the popular dialects of LISP around in the early 1980 s • A large and complex language – the opposite of Scheme CS 363 Spring 2005 GMU 36
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 CS 363 Spring 2005 GMU 37
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 CS 363 Spring 2005 GMU 38
ML • Includes lists and list operations • The val statement binds a name to a value (similar to DEFINE in Scheme) • Function declaration form: function_name (formal_parameters) = function_body_expression; e. g. , fun cube (x : int) = x * x; CS 363 Spring 2005 GMU 39
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) CS 363 Spring 2005 GMU 40
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 CS 363 Spring 2005 GMU 41
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 CS 363 Spring 2005 GMU 42
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 CS 363 Spring 2005 GMU 43
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] CS 363 Spring 2005 GMU 44
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] CS 363 Spring 2005 GMU 45
Haskell Examples • Quicksort: sort [] = [] sort (a: x) = sort [b | b ← x; b <= a] ++ [a] ++ sort [b | b ← x; b > a] CS 363 Spring 2005 GMU 46
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 CS 363 Spring 2005 GMU 47
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 CS 363 Spring 2005 GMU 48