1 of 20

CS10: The Beauty and Joy of Computing - Fall 2021

Lecture 14: Programming Paradigms, Midterm Review

2 of 20

As We Prepare To Step Into Python

Monday 10/11 was our last day of Snap! in lecture before we move on to Python.

  • Snap! will make an appearance later in lecture and lab for a couple of special topics.
  • You’ll also have a choice of Snap! or Python for your final project.

Python, though quite similar to Snap! in many ways, will make you think differently.

  • Today we’ll talk about the philosophy of programming generally by discussing different “programming paradigms”.
  • You’ll see this topic again in 61A.

3 of 20

Programming Paradigms

4 of 20

Programming Paradigms

Paradigm (Merriam Webster): a typical example or pattern of something; a model. Example: "there is a new paradigm for public art in this country"

Programming Paradigm (Joe Turner, Clemson University): “A programming paradigm is a general approach, orientation, or philosophy of programming that can be used when implementing a program.”

Example, three very different approaches to squaring list:

5 of 20

Word of Warning

There is no universally agreed upon taxonomy of human programming styles.

One possible list:

  • Imperative
  • Functional
  • Array-based
  • Object oriented
  • Declarative

These terms are a bit fluid, and as you’ll see if you read more on wikipedia, there is substantial disagreement about these terms.�

6 of 20

Programming Paradigms

Example, three very different approaches to squaring list:

  • Functional:

  • Array-based:

  • Imperative:

7 of 20

The Imperative Programming Paradigm

An imperative program provides a sequence of steps.

  • Like following a recipe.
  • Assignment is allowed (can set variables).
  • Mutation is allowed (can change variables).

Example (acronym):

8 of 20

The Functional Programming Paradigm

In functional programming, computation is thought of in terms of the evaluation of functions.

  • No state (e.g. variable assignments).
  • No mutation (e.g. changing variable values).
  • No side effects when functions execute.

9 of 20

A Hybrid Approach

These paradigms are not official rules. Just attempts to taxonomize approaches taken by humans.

  • Code below is sorta functional, sorta imperative.
  • Utilizes state for clarity. I tend to program this way. You might not.

Array operation!

10 of 20

Discussion and Debate

Which of these do you like best?

Easy to come back and read it later.

Less to keep track of.

Liked this more when first learning. More natural. More similar to the way we think about problems.

11 of 20

Imperative vs. Functional

Can argue that functional is a subset of imperative.

  • Functional programming is still a series of steps.
  • Just need to avoid state and think of computation as functions.

Programs in the functional paradigm:

  • More often only one obvious way to do something.
    • Programming feels more like solving puzzles.
    • Solutions can seem like magic (especially to imperative programmers).
  • Tend to be shorter.
  • Tend to be easier to debug (no need to track variables / side effects).
  • Tend to parallelize better (can split work on multiple computers).
    • Example: Each computer can do 1/8th of a “map” operation.
  • Are growing in popularity.

12 of 20

The Object Based Programming Paradigm

In object programming, we organize our thinking around objects, each containing its own data, and each with its own procedures that can be invoked.

Sprites in Snap! are objects.

  • Each has a position, size, costume, etc.
  • One sprite can tell other sprites to do something, access other sprite’s data, etc.

13 of 20

The Object Based Programming Paradigm

There is a LOT more to object oriented programming.

  • We’ll see more when we get to Python.
  • Major topic of 61A and especially 61B.
    • All programs in Java (the language used by 61B) are required to be object oriented by the structure of the language.

14 of 20

Declarative Programming

In declarative programming, we express what we want, without specifying how. A program is simply a description of the result we want.

15 of 20

Declarative Programming

In declarative programming, we express what we want, without specifying how. A program is simply a description of the result we want.

Example: coloring a map of Germany using the Prolog language:�

16 of 20

Prolog Example (From Bernardo Pires)

Tell Prolog that colors exist: Tell Prolog that same colors can’t touch:

Tell Prolog all the borders:

Ask Prolog for answer:

17 of 20

Declarative Programming

In declarative programming, we express what we want, without specifying how. A program is simply a description of the result we want.

Another example, coloring a map of Germany using the Prolog language:�

18 of 20

Declarative Programming

Each declarative language has only a limited number of tasks for which you can specify “what”, and not “how”, e.g.

  • Prolog: Logic.
  • SQL: Queries from a database.
  • Pandas: Data manipulation operations like aggregation, filtering, joining, etc.
    • Very common operations in Data 8 and Data 100.
    • Pandas is a “library” for Python: Think of it as a collection of blocks for data manipulation.
    • Let’s see a quick demo.

19 of 20

Universality

Each of the discussed paradigms (imperative, functional, object-based, array based, declarative) are simply philosophies.

  • All paradigms are equally powerful.

Recall that any Turing Complete language can perform any computation that any other language can perform.

  • Simulation of a Turing Machine is possible in any of these paradigms.
  • Therefore they are equally powerful.

20 of 20

Open Q&A / Midterm Review