Comparative Programming Languages - PowerPoint PPT Presentation

About This Presentation
Title:

Comparative Programming Languages

Description:

Comparative Programming Languages Language Comparison: Scheme, Smalltalk, Python, Ruby, Perl, Prolog, ML, C++/STL, Java, Haskell Selection Sort in Scheme Let s ... – PowerPoint PPT presentation

Number of Views:78
Avg rating:3.0/5.0
Slides: 49
Provided by: tjhsstEdu
Learn more at: https://www.tjhsst.edu
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, Haskell

2
Selection Sort in Scheme
  • Lets define a few useful functions first
  • (DEFINE (findsmallest lis small)
  • (COND ((NULL? lis) small)
  • ((lt (CAR lis) small)
  • (findsmallest (CDR lis)
    (CAR lis)))
  • (ELSE
  • (findsmallest (CDR lis)
    small))
  • )
  • )

CS 363 Spring 2005 GMU
2
3
(No Transcript)
4
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
5
Selection Sort in Python
  • def smallest(L, A)
  • if len(L)0
  • return A
  • elif L0 lt A
  • return smallest(L1, L0)
  • else
  • return smallest(L1, A)

CS 363 Spring 2005 GMU
5
6
Selection Sort in Python
  • def myremove(L, A)
  • L.remove(A)
  • return L

CS 363 Spring 2005 GMU
6
7
Selection Sort in Python
  • def selection(L)
  • if len(L) 0
  • return
  • else
  • return smallest(L,L0) selection(myremove(L,s
    mallest(L, L0)))

CS 363 Spring 2005 GMU
7
8
Selection Sort in Ruby
  • def smallest(lis, a)
  • if lis.length0 then
  • a
  • elsif lis0 lt a then
  • smallest(lis1..lis.length-1, lis0)
  • else
  • smallest(lis1..lis.length-1, a)
  • end
  • end

CS 363 Spring 2005 GMU
8
9
Selection Sort in Ruby
  • def myremove(lis, a)
  • lis.delete(a)
  • lis
  • end

CS 363 Spring 2005 GMU
9
10
Selection Sort in Ruby
  • def selection(lis)
  • if lis.length 0 then
  • else
  • smallest(lis,lis0) selection(myremove(lis,sm
    allest(lis, lis0)))
  • end
  • end

CS 363 Spring 2005 GMU
10
11
Selection Sort in Smalltalk
  • smallest currentsmallest
  • self size 0 ifTrue currentsmallest.
  • ifFalse
  • self first lt currentsmallest ifTrue
  • self allButFirst smallest self first.
  • ifFalse
  • self allButFirst smallest currentsmallest.
  • .

CS 363 Spring 2005 GMU
11
12
Selection Sort in Smalltalk
  • myRemove item
  • self removeAt (self find item).
  • !

CS 363 Spring 2005 GMU
12
13
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.
  • .

CS 363 Spring 2005 GMU
13
14
Selection Sort in Perl
  • sub smallest
  • my (element, _at_lis) _at__
  • if (scalar(_at_lis) 0)
  • return element
  • elsif (lis0 lt element)
  • return smallest(_at_lis1..lis, lis0)
  • else
  • return smallest(_at_lis1..lis, element)

CS 363 Spring 2005 GMU
14
15
Selection Sort in Perl
  • sub remove
  • my (element, _at_lis) _at__ my _at_templis
  • if (scalar(_at_lis) 0) return ()
  • elsif (lis0 element)
  • return _at_lis1..lis
  • else
  • _at_templis remove(element, _at_lis1..lis)
  • unshift(_at_templis, lis0)
  • return _at_templis

CS 363 Spring 2005 GMU
15
16
Selection Sort in Perl
  • sub selectionsort
  • my _at_templis my _at_templis2 my s
  • if (scalar(_at__) 0)
  • return ()
  • else
  • s smallest(_at__1.._, _0)
  • _at_templis selectionsort(remove(s, _at__))
  • unshift(_at_templis, s)
  • return _at_templis

CS 363 Spring 2005 GMU
16
17
Selection Sort in Prolog
  • smallest(, Smallest,Smallest).
  • smallest(FirstRest, CurrSmallest, Smallest) -
  • First lt CurrSmallest, smallest(Rest, First,
    Smallest).
  • smallest(_Rest, CurrSmallest, Smallest) -
  • smallest(Rest, CurrSmallest, Smallest).

CS 363 Spring 2005 GMU
17
18
Selection Sort in Prolog
  • remove(, _, ).
  • remove(FirstRest, First, Rest).
  • remove(FirstRest, Element, FirstNewList) -
  • remove(Rest, Element, NewList).

CS 363 Spring 2005 GMU
18
19
Selection Sort in Prolog
  • selectionsort(,).
  • selectionsort(FirstRest, SmallestSortedList)
    -
  • smallest(Rest, First, Smallest),
  • remove(FirstRest, Smallest, NewList),
  • selectionsort(NewList, SortedList).

CS 363 Spring 2005 GMU
19
20
Selection Sort in ML
  • fun smallest(, a) a
  • smallest(firstrest, a)
  • if first lt a then
  • smallest(rest, first)
  • else smallest(rest, a)

CS 363 Spring 2005 GMU
20
21
Selection Sort in ML
  • fun remove(, _)
  • remove(firstrest, item)
  • if first item then
  • rest
  • else
  • firstremove(rest, item)

CS 363 Spring 2005 GMU
21
22
Selection Sort in ML
  • fun selectionsort()
  • selectionsort(firstrest)
  • let
  • val s smallest(rest, first)
  • in
  • sselectionsort(remove(firstrest,s))
  • end

CS 363 Spring 2005 GMU
22
23
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
24
Selection Sort in C/STL
  • int smallest(listltintgt lis, int small)
  • if (lis.size() 0) return small
  • else if (lis.begin() lt 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
25
Selection Sort in C/STL
  • listltintgt remove(listltintgt 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()
  • listltintgt removedList remove(lis, item)
  • removedList.push_front(first)
  • return removedList

CS 363 Spring 2005 GMU
25
26
Selection Sort in C/STL
  • listltintgt selectionsort(listltintgt lis)
  • if (lis.size() 0) return lis
  • else int first lis.begin()
  • listltintgt rest lis
  • rest.pop_front()
  • int s smallest(rest, first)
  • listltintgt sortedList
  • selectionsort(remove(lis,s))
  • sortedList.push_front(s)
  • return sortedList

CS 363 Spring 2005 GMU
26
27
Selection Sort in Java
  • int smallest(List lis, int small)
  • if (lis.isEmpty()) return small
  • else if (((Integer) lis.get(0)).intValue() lt
    small)
  • return smallest(lis.subList(1,lis.size()),
  • ((Integer) lis.get(0)).intValue())
  • else return smallest(lis.subList(1,lis.size()),
    small)

CS 363 Spring 2005 GMU
27
28
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

CS 363 Spring 2005 GMU
28
29
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

CS 363 Spring 2005 GMU
29
30
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
31
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
32
Higher-Order Functions mapcarScheme
  • Using mapcar
  • (mapcar (LAMBDA (num) ( num num num)) (3 4 2
    6))
  • returns (27 64 8 216)

CS 363 Spring 2005 GMU
32
33
Higher Order Functions EVALScheme
  • 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

CS 363 Spring 2005 GMU
33
34
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
35
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
36
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

CS 363 Spring 2005 GMU
36
37
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
38
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
39
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

CS 363 Spring 2005 GMU
39
40
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
41
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
42
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
43
Haskell Examples
  • 2. Factorial (illustrates guards)
  • fact n
  • n 0 1
  • n gt 0 n fact (n - 1)
  • The special word otherwise can appear as a
    guard

CS 363 Spring 2005 GMU
43
44
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
45
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., 13, 5, 7 results in
  • 1, 3, 5, 7

CS 363 Spring 2005 GMU
45
46
Haskell Examples
  • Quicksort
  • sort
  • sort (ax) sort b b ? x b lt a
  • a
  • sort b b ? x b gt a

CS 363 Spring 2005 GMU
46
47
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
48
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
Write a Comment
User Comments (0)
About PowerShow.com