Programming Paradigms

Cards (21)

  • Programming Paradigms
    There are 4 different types of programming paradigms:
    • Procedural Programming
    • Object-Oriented Programming
    • Declarative Programming
    • Functional Programming
  • What is a programming paradigm?
    A style or way of programming
  • Imperative Programming

    Languages which support imperative programming consist of a series of instructions that tell the computer what to do with the input in order to solve the problem.
    Procedural programming is imperative programming with procedure calls.
  • Structured Programming

    Could also be defined as a programming paradigms. It is a kind of procedural (imperative) programming which uses the constructs sequence, selection, iteration and recursion rather than "goto" statements. Modular techniques are used to split a large program into manageable chunks.
  • Declarative Programming

    SQL is a declarative language. SQL statements describe the problem that is to be solved. The language implementation then finds the best way of solving it. It is used to create, amend and query databases.
  • Logic Programming

    A form of declarative programming. It is a paradigm that expresses the logic of a computation without expressing its control flow. Programs consist of logical statements. Prolog is an example of a logic programming language.
  • Programming in Prolog
    Statements are written in the form of facts and rules
    For Example, likes(tom, jenny) /*tom likes jenny*/
    You can query your database of facts
  • Variables in Prolog
    Variables are distinguished by starting with an uppercase letter.
  • Rules in Prolog
    Rules are expressed in the form of :
    IF a is true, THEN b is true
    Consider the following logic:
    Lions eat meat
    Larry is a lion
    Therefore, Larry eats meat
    In prolog:
    eats_meat(X):
    lion(X) /*if its a lion, it eats meat*/
  • Summary of Prolog
    A fact is always unconditionally true, and a rule is true depending on a given condition. The order in which facts and rules are stated is not important, so it is easy to add, change or delete rules.
  • Backtracking
    When prolog selects a route through a maze of facts or rules it may sometimes lead to a dead end. If that happens it will backtrack to the last decision point and try another route until either the goal is achieved or there are no further routes to try. It is an important part of declarative programming.
  • What are the applications of declarative programming?

    This paradigm is well suited to programming expert systems/ The expert system embodies the facts and rules about a particular field of knowledge.
    • medical diagnosis
    • oil exploration
    • tax regulations
    It is also useful for processing natural language
  • Object Oriented Programming (OOP)
    OOP languages were developed to make it possible to abstract details of implementation away from the user. The code is designed to be reusable and is easy to maintain. In OOP the world is viewed as a collection of objects such as:
    • person
    • animal
    • event
    • data structure (queue or a stack)
    A program consists of objects:
    • each object has its own data (attributes) and operation on the data (methods)
    • objects interacting with one another
    • all processing is done by objects
  • What is a class?
    A blueprint for an object. It defines attributes and methods that capture the common characteristics and behaviours of objects.
  • What is encapsulation?
    A fundamental principle of OOP. Attributes and methods are wrapped into a single entity.
  • What is information hiding?
    When the objects attributes are hidden (Private). Attributes are access and changed only through the objects methods. Methods are required to set (setters) and retrieve (getters) an objects attributes. To interact with an object, its methods must be accessible (public).
  • What are types of methods?
    Forward, turn
  • How do you define a class?
    The methods and attributes belonging to a class are specified in a class definition.
    Example:
    class Turtle
    private name
    private xcoord, ycoord, angle, colour
    public procedure new(x, y, myAngle, myColour)
    xcoord = x
    endprocedure
    public procedure forward(steps)
    (statements to calculate new position)
    end procedure
    (other procedures)
    endclass
  • What is inheritance?

    A relationship among classes where a sub-class shares all of the attributes and methods of a parent class. The sub-class may in addition have its own attributes and methods
  • the "is a" rule
    there is a simple rule to determine whether inheritance is appropriate in a program.
    Ask: "is object A an object B"
    E.g.
    is a cat an animal? is a mouse a rodent?
  • What is polymorphism?
    When an inherited class may have methods or attributes that do not exist in the parent class. It may redefine methods that are defined the parent class.
    E.g.
    a parent class Bird may have a method Eat
    • a subclass Parrot may define this as eating seeds
    • a subclass Eagle may define this as eating meat
    • a subclass Chicken may define this as eating both meat and seeds