CSC 201 Note - 1
CSC 201 Note - 1
A. Functional Programming
Definition: A paradigm where computation is treated as the evaluation of mathematical
functions, avoiding changing state and mutable data.
Characteristics:
Emphasizes pure functions (functions that return the same output for the same input and
have no side effects).
Relies heavily on immutability (data that doesn’t change after creation).
Encourages function composition and higher-order functions (functions that can take
other functions as parameters or return them as results).
Examples of Functional Languages: Haskell, Lisp, and Erlang. JavaScript and Python
support functional programming concepts as well.
Use Cases: Often used in data processing, machine learning, and applications that benefit
from immutability (e.g., parallel processing).
B. Declarative Programming
Definition: A paradigm where the programmer specifies what the program should
accomplish rather than detailing how to achieve it.
Characteristics:
Focuses on expressing logic without describing the control flow.
Less code is typically required since specific implementation details are abstracted away.
Common in query languages and configuration management.
Examples of Declarative Languages: SQL for database queries, HTML for webpage
structure, and CSS for styling.
Use Cases: Frequently used in data manipulation and retrieval, configuration management,
and situations where outcomes are more important than procedural steps.
C. Logic Programming
Definition: A paradigm based on formal logic, where the programmer defines a set of rules,
facts, and relationships, and the program derives conclusions based on these.
Characteristics:
Uses facts and rules as a basis for reasoning.
Logic programs often rely on backtracking (trying different paths until a solution is found)
and unification (matching logic terms).
Use Cases: Well-suited for solving complex problems like constraint satisfaction
(scheduling, puzzles) and natural language processing.
Problem-Solving Steps:
1. Understand the Problem: Clearly define the problem and identify requirements,
inputs, and outputs.
2. Plan the Solution: Break down the problem into smaller parts and outline a strategy.
This may involve pseudocode, flowcharts, or logic diagrams.
3. Implement the Solution: Write the code according to your plan. Keep it modular,
starting with a basic version and iterating from there.
4. Test and Debug: Check if your solution works with different inputs, and debug any
issues that arise.
5. Optimize and Improve: Once the solution works, consider ways to make it more
efficient or simpler.
Extra Activities
Write a simple Python script to automate a task (e.g., reading a text file and counting
word occurrences). Alternatively, write a JavaScript program to manipulate an HTML
page (e.g., changing colors or adding elements dynamically).
Provide students with a small problem (e.g., calculating the sum of an array or finding
the maximum number) and have them work through the problem-solving steps in
pairs.
Practice Questions
1. Write a Python script that takes a list of numbers and prints the squares of each
number.
2. Write a JavaScript function that changes the text color of a web page element when
a button is clicked.
3. Problem-Solving Questions:
1. You are given an array of numbers. Write an outline for a function that finds the
average of these numbers. Describe the problem-solving steps before writing the
code.
2. Suppose you need to create a schedule for several events. Which programming
paradigm (functional, declarative, or logic) might be most useful, and why?