Docsity

Lecture Slides on Programming Paradigms - Programming Languages | CS 403, Study notes of Programming Languages

Toggle sidebar
bg1
1
Spring 2002 CS 403 Class Notes Page 1
Lecture 32 – Tuesday, April 30
• Today
– Programming paradigms
• Reading Assignments For Next Class
– Chapter 11
Spring 2002 CS 403 Class Notes Page 2
Programming Paradigms
• Imperative/procedural paradigm
– Pretty much what you are used to
• Object-oriented paradigm
– Also have some experience with this, but C++ and even
Java have some procedural legaci es
• Functional paradigm
• Logic paradigm
• Event-driven paradigm
• Concurrency paradigm
Spring 2002 CS 403 Class Notes Page 3
Imperative Paradigm
• Also called the procedural paradigm
• Basic elements:
– Assignment statements
– Procedures
– Standard f low-of-control statements (if, while, etc.)
• Program has definite initiation and termination points
• Pascal, C, etc. are examples of this
Spring 2002 CS 403 Class Notes Page 4
Object-Oriented Paradigm
• Program is a series of communicati ng objects.
• Objects pass messages to one another.
• Objects have methods that are executed in response to a
message.
• Smalltalk is the classical language in this category.
• C++ i s also object-oriented but:
– It has non-object elem ents (built-in types, plain functi ons, etc.)
– We don’t usually us e the “message passing” par adigm to
conceptualize C++ pr ograms.
• Java eliminates the “plain functions” , but is otherwise very
similar to C++ in this regard.
Spring 2002 CS 403 Class Notes Page 5
Functional Paradigm
• Program consists entirely of functional elements
– Function: Mappi ng from domain to range
– f(x) = x * x (for all x)
– f(x) = 0 if x = 1
1 if x = 2
x * x otherwise
• No assignment statements, no way to affect state
directly
• Lots of functional composition and recursion
Spring 2002 CS 403 Class Notes Page 6
Example: LISP
• Language for doing list processing.
• Basi c statements:
– (CAR ‘(A B C D)) returns A
– (CDR ‘(A B C D)) returns (B C D)
– (CONS ‘A ‘(B C D)) returns (A B C D)
– (ATOM ‘A) returns T
– (ATOM ‘(B C D)) returns NIL (or FALSE)
– (EQUAL ‘(A B) ‘(A B)) returns T
– (NULL ‘()) returns T
– (NULL ‘(A B C)) returns NIL
– Arithmetic functions
pf2

Partial preview of the text

Download Lecture Slides on Programming Paradigms - Programming Languages | CS 403 and more Study notes Programming Languages in PDF only on Docsity!

Spring 2002 CS 403 Class Notes Page 1

Lecture 32 – Tuesday, April 30

• Today

– Programming paradigms

• Reading Assignments For Next Class

– Chapter 11

Spring 2002 CS 403 Class Notes (^) Page 2

Programming Paradigms

• Imperative/procedural paradigm

  • Pretty much what you are used to

• Object-oriented paradigm

  • Also have some experience with this, but C++ and even Java have some procedural legacies

• Functional paradigm

• Logic paradigm

• Event-driven paradigm

• Concurrency paradigm

Spring 2002 CS 403 Class Notes (^) Page 3

Imperative Paradigm

• Also called the procedural paradigm

• Basic elements:

  • Assignment statements
  • Procedures
  • Standard flow-of-control statements (if, while, etc.)

• Program has definite initiation and termination points

• Pascal, C, etc. are examples of this

Spring 2002 CS 403 Class Notes Page 4

Object-Oriented Paradigm

  • Program is a series of communicating objects.
  • Objects pass messages to one another.
  • Objects have methods that are executed in response to a message.
  • Smalltalk is the classical language in this category.
  • C++ is also object-oriented but:
    • It has non-object elements (built-in types, plain functions, etc.)
    • We don’t usually use the “message passing” paradigm to conceptualize C++ programs.
  • Java eliminates the “plain functions”, but is otherwise very similar to C++ in this regard.

Spring 2002 CS 403 Class Notes (^) Page 5

Functional Paradigm

• Program consists entirely of functional elements

  • Function: Mapping from domain to range
  • f(x) = x * x (for all x)
  • f(x) = 0 if x = 1 1 if x = 2 x * x otherwise

• No assignment statements, no way to affect state

directly

• Lots of functional composition and recursion

Spring 2002 CS 403 Class Notes (^) Page 6

Example: LISP

  • Language for doing list processing.
  • Basic statements:
    • (CAR ‘(A B C D)) returns A
    • (CDR ‘(A B C D)) returns (B C D)
    • (CONS ‘A ‘(B C D)) returns (A B C D)
    • (ATOM ‘A) returns T
    • (ATOM ‘(B C D)) returns NIL (or FALSE)
    • (EQUAL ‘(A B) ‘(A B)) returns T
    • (NULL ‘()) returns T
    • (NULL ‘(A B C)) returns NIL
    • Arithmetic functions

Spring 2002 CS 403 Class Notes Page 7

Some Examples

• Length:

(DEFUN LENGTH(X)

(COND ((NULL X) NIL)

(T (+ 1 (LENGTH (CDR X))))

• (LENGTH ‘(A B C D)) returns 4

Spring 2002 CS 403 Class Notes (^) Page 8

One More Example

• Member:

(DEFUN MEMBER(X Y)

(COND

((NULL Y) nil) ((EQUAL X (CAR Y)) T) ((T (MEMBER X (CDR Y)))) ) )

• (MEMBER ‘A ‘(B C A D (E F))) returns T

Spring 2002 CS 403 Class Notes (^) Page 9

Logic Paradigm

• Program made up of facts and inference rules

• Program gives the answer

• Prolog is the cannonical language in this area

– Big in the artificial intelligence community

Spring 2002 CS 403 Class Notes Page 10

Prolog Example

Rainy(seattle)

Rainy(rochester)

Cold(rochester)

Snowy(x) => Rainy(x), Cold(x)

Query: Snowy(y) will yield Rochester

Spring 2002 CS 403 Class Notes (^) Page 11

Event-Driven Paradigm

  • Canonical example: Visual Basic
  • Lay out a visual representation of the program
    • Buttons, windows, etc.
  • Attach event handlers to buttons
    • In VB, click on button in design view
    • Write an event handler that is executed if the button is clicked
  • Key distinguishing feature of event-driven paradigm: No main program that drives the entire execution

Spring 2002 CS 403 Class Notes (^) Page 12

Concurrency Paradigm

• Write programs that contain multiple threads of

execution

• Threads are executed in parallel

• Threads communicate through two principal

mechanisms:

  • Shared variables
  • Message passing

• General discussion of concurrency is beyond the

scope of this course