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

Slides:



Advertisements
Similar presentations
Functional Programming Languages Session 12
Advertisements

1 Scheme and Functional Programming Aaron Bloomfield CS 415 Fall 2005.
1 Programming Languages (CS 550) Lecture Summary Functional Programming and Operational Semantics for Scheme Jeremy R. Johnson.
Functional Programming. Pure Functional Programming Computation is largely performed by applying functions to values. The value of an expression depends.
CS 355 – PROGRAMMING LANGUAGES Dr. X. Apply-to-all A functional form that takes a single function as a parameter and yields a list of values obtained.
1-1 An Introduction to Scheme March Introduction A mid-1970s dialect of LISP, designed to be a cleaner, more modern, and simpler version than.
1 Functional programming Languages And a brief introduction to Lisp and Scheme.
Functional Programming Languages
Chapter 15 Other Functional Languages. Copyright © 2007 Addison-Wesley. All rights reserved. Functional Languages Scheme and LISP have a simple syntax.
Chapter 15 Functional Programming Languages. Copyright © 2007 Addison-Wesley. All rights reserved. 1–2 Introduction Design of imperative languages is.
ISBN Chapter 15 Functional Programming Languages Mathematical Functions Fundamentals of Functional Programming Languages Introduction to.
Functional programming: LISP Originally developed for symbolic computing First interactive, interpreted language Dynamic typing: values have types, variables.
ISBN Chapter 15 Functional Programming Languages.
ISBN Chapter 15 Functional Programming Languages.
Dr. Muhammed Al-Mulhem ICS An Introduction to Functional Programming.
1 Lisp and Functional Languages Functional forms Referential transparency Function construction Function composition Mapping functions Designing functional.
Chapter 15: Functional Programming Languages
1 Copyright © 1998 by Addison Wesley Longman, Inc. Chapter 13 In a language without exception handling: When an exception occurs, control goes to the operating.
Comparative Programming Languages Functional Languages Comparison: Scheme, Smalltalk, Python, Ruby.
1 Functional Programming In Text: Chapter Chapter 2: Evolution of the Major Programming Languages Outline Functional programming (FP) basics A bit.
By Neng-Fa Zhou1 Evolution of programming languages –Machine language –Assembly language –Sub-routines and loop (Fortran) –Procedures and recursion (Algol,
 The design of the imperative languages is based directly on the von Neumann architecture  Efficiency is the primary concern  Low-level specifications:
ISBN Chapter 15 Functional Programming Languages.
CS 355 – PROGRAMMING LANGUAGES Dr. X. Common LISP A combination of many of the features of the popular dialects of LISP around in the early 1980s A large.
ISBN Chapter 15 Functional Programming Languages.
Comparative Programming Languages Functional Languages Comparison: Scheme, Smalltalk, Python, Ruby, Perl, Prolog, ML, C++/STL, Java.
ISBN Chapter 15 Functional Programming Languages.
CS 326 Programming Languages, Concepts and Implementation Instructor: Mircea Nicolescu Lecture 8.
Chapter Fifteen: Functional Programming Languages Lesson 12.
Comparative Programming Languages Language Comparison: Scheme, Smalltalk, Python, Ruby, Perl, Prolog, ML, C++/STL, Java.
ISBN Chapter 15 Functional Programming Languages.
CS 363 Comparative Programming Languages Functional Languages: Scheme.
ISBN Chapter 15 Functional Programming Languages.
ISBN Chapter 15 Functional Programming Languages.
ISBN Chapter 15 Functional Programming Languages.
1 Chapter 15 © 2002 by Addison Wesley Longman, Inc Introduction - The design of the imperative languages is based directly on the von Neumann architecture.
ISBN Chapter 15 Functional Programming Languages.
Chapter 15 Functional Programming Languages. Copyright © 2012 Addison-Wesley. All rights reserved.1-2 Chapter 15 Topics Introduction Mathematical Functions.
1-1 An Introduction to Functional Programming Sept
Milos Hauskrecht (PDF) Hieu D. Vu (PPT) LISP PROGARMMING LANGUAGE.
1 FP Foundations, Scheme In Text: Chapter Chapter 14: FP Foundations, Scheme Mathematical Functions Def: A mathematical function is a mapping of.
Functional Programming Part 1. Organization of Programming Languages-Cheng Big Picture u What we’ve learned so far: Imperative Programming Languages 
ISBN Chapter 15 Functional Programming Languages.
Comparative Programming Languages Language Comparison: Scheme, Smalltalk, Python, Ruby, Perl, Prolog, ML, C++/STL, Java.
ISBN Chapter 15 Functional Programming Languages.
Functional Programming
Functional Programming Languages
Functional Programming Languages
Functional Programming Languages
Chapter 15 :Functional Programming Languages
Functional Programming Languages
Functional Programming
Functional Programming
Functional Programming Languages
Functional Programming Languages
Evolution of programming languages
Functional Programming
Functional Programming Languages
FP Foundations, Scheme In Text: Chapter 14.
Functional Programming Languages
Functional Programming Languages
Functional Programming Languages
Functional Programming Languages
Functional Programming Languages
15.2 Mathematical Functions
Functional Programming Languages
Functional Programming Languages
Functional Programming Languages
Functional Programming Languages
Presentation transcript:

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

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)) )

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

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)) ) )

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)

CS 363 Spring 2005 GMU6 Selection Sort in Python def myremove(L, A): L.remove(A) return L

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])))

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

CS 363 Spring 2005 GMU9 Selection Sort in Ruby def myremove(lis, a) lis.delete(a) lis end

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

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]. ].

CS 363 Spring 2005 GMU12 Selection Sort in Smalltalk myRemove: item ^self removeAt: (self find: item). !

CS 363 Spring 2005 GMU13 Selection Sort in Smalltalk selection "(FileStream oldFileNamed: 'selection.st') fileIn. Numbers := #( ) 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 GMU14 Selection Sort in Perl sub smallest { my if == 0) { return $element; } elsif ($lis[0] < $element) { return $lis[0]); } else { return $element); }

CS 363 Spring 2005 GMU15 Selection Sort in Perl sub remove { my if == 0) { return (); } elsif ($lis[0] == $element) { } else = $lis[0]); }

CS 363 Spring 2005 GMU16 Selection Sort in Perl sub selectionsort { my $s; if == 0) { return (); } else { $s = = $s); }

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).

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).

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).

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);

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);

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;

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 *)

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); }

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; }

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; }

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); }

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; }

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; }

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

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)))) ))

CS 363 Spring 2005 GMU32 Higher-Order Functions: mapcar Scheme Using mapcar: (mapcar (LAMBDA (num) (* num num num)) ‘( )) returns ( )

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

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 ‘( )) Returns 24

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

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

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

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

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;

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)

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

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

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

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]

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]

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]

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

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