It's the wasp's elbows

Join a laid-back, close-knit community of mixed interests Get a free account!

  1. Programming with Neko-Chan: Languages and Pseudo Code


    #1057322016-08-04 01:18:58 *Neko-Chan said:

    Languages and Pseudo Code

    Let's talk about languages

    I think everyone and their cat has a rough understanding of what a computer language is. But do they? I will try my best to explain as much as I can about languages without going into their linguistics (most definitely its own thread).

    So what is a computer language? A language is a set of keywords and syntactic grammar used to instruct hardware to run program instructions.

    Typically when people reference computer languages they mean High level languages. High level languages are much more robust and complex and readable than what a computer can actually understand. Just by writing C/C++ does not mean your CPU can understand it. Compilers are special programs that convert high level languages into low level languages that hardware can then interpret.

    Computer languages are crutches for humans because no one likes programming binary. They are an intermediary to the pure math and complexity of hardware.

    As a programmer the thing you need to understand is that languages are just tools and they are not like spoken human language. Typically once you learn one, learning most others becomes simple, for all of the concepts, tools, and invariants are the same. All that changes is the vocabulary, the syntax, and the invariants between languages. All programs however fall into a style of programming, which is what I will discuss within this thread.

    Paradigms

    Paradigms are ways to classify computer languages based on the computational style they operate. While there are many paradigms I will discuss the big 4.

    Procedural

    Procedural languages are the simplest and the first programming languages. They describe exactly how a program will run. It is literally writing in order a list of computer instructions that will be ran in that order.

    Languages a part of the procedural paradigm:

    • COBOL
    • FORTRAN
    • ALGOL
    • BASIC
    • C
    • Python

    Object-Oriented Programming (OOP)

    Following procedural languages OOP languages were created. They describe programs in terms of objects and the relations between them. Objects that have characteristics that describe them and actions they can take. These languages are typically much more verbose than procedural languages but allows for concepts like Abstraction, Encapsulation, Inheritance, and Polymorphism.

    Encapsulation means the internal representation of an object is generally hidden from the view outside the object's definition. This reduces system complexity and makes for more readable code.

    I.e. You have a cellphone and it can make calls, you don't know how it makes calls but you still make calls with the cellphone. The way the cellphone makes calls is encapsulated within it.

    Abstraction is the development of objects in terms of their interfaces and functionality instead of their details. It is tied with encapsulation but essentially means that you can define an object with same essential properties and actions in the original object being represented.

    I.e. A cellphone can be defined without having to explicitly specify that it is an iPhone or a Galaxy S whatever.

    Inheritance is a way to reuse existing objects or establish subtypes from an existing object.

    I.e. You can define an object called "Animal" all animals can breath and walk but a fish can't bark. Dog and Fish would be a subclass of animal. They are extended.

    Polymorphism means one name for many forms. Many objects can have the same method name but differ in definition.

    I.e. You can find the area of a triangle, and you can find the area of a circle. How you do so is different. Defining a function called getArea() would exist in both of these objects but have different instructions on how to do it.

    Encapsulation, Abstraction, Inheritance, and Polymorphism are the 4 principles of OOP and what gives it its value.

    Languages a part of the OOP paradigm:

    • Java
    • Smalltalk
    • C++
    • C#
    • PHP

    Declarative / functional

    A style of building the structure and elements of a program without describing its control flow (Thank you wiki). Declarative attempts to eliminate concepts found in other paradigms such as state and control flow.

    If you have data that can be stored and what you do next depends on that data, then you have state. The problem with state is that often you do not know what your state is and therefore do not know how your program is going to behave or do not have proper definitions on how to handle that state. Control flow is process of deciding how to execute and ordering of instructions based on state.

    The idea is that you describe what the program must do and not how to do it.

    Languages a part of the Declarative paradigm:

    • Haskell
    • SQL
    • Curl

    Logical / Symbolic

    A subset of declarative like functional, this paradigm is about defining logical truths and letting the program infer things about the data. These are often used for finding relations between large data sets and artificial intelligence. This is not my area of expertise so if this interests you, there are many better sources of info on this category.

    Languages a part of Logical/Symbolic paradigm:

    • Lisp
    • Prolog

    In a later thread I will go into the linguistics of a program language so you will have a better idea how programming languages differ and understand some of their core functionality.

    Paradigms of Modern Languages

    These days most high level languages are a part of many paradigms, taking the best parts from each to make that specific language the most versatile. Often these differences will arise up when programming complex things that must use language specific functionality.


    Pseudo Code

    While many languages exist, the concepts and algorithms used within them are often similar if not the same. When reading tutorials or referencing algorithms they are often described in a C or Java syntactic Pseudo code. Pseudo code is exactly what it means: fake code. The Pseudo code by design is not specific to any language and cannot be ran. It often ignores the invariants and differences between languages.

    I will be using it to describe algorithms and control flow in later projects. A good exercise to learn pseudo code is to take something that you understand and describe how it works in terms of pseudo code. Not only does this teach you pseudo code, it teaches you how to reason computationally.

    Here is a comparison of different types of Pseudo Code for the bubble sort Algorithm:

    Actual C Code:

    /* bubble sort */
    void ArraySort(int This[], CMPFUN fun_ptr, uint32 ub) {
        uint32 indx;
        uint32 indx2;
        int temp;
        int temp2;
        int flipped;
        if (ub <= 1)
            return;
        indx = 1;
        do {
            flipped = 0;
            for (indx2 = ub -1; indx2 >= indx; --indx2) {
                temp = This[indx2];
                temp2 = This[indx2 - 1];
                if ((*fun_ptr)(temp2, temp) > 0) {
                    This[indx2 - 1] = temp;
                    This[indx2] = temp2;
                    flipped = 1;
                }
            }
        } while ((++indx < ub) && flipped);
    }
    

    Pseudo Code:

    Set n to number of records to be sorted
    repeat
    flag = false;
    for counter = 1 to n-1 do
        if key[counter] > key[counter+1] then
            swap the records;
            set flag = true;
        end if
    end do
    n = n-1;
    until flag = false or n=1
    

    As you can see the pseudo code is much easier to read and understand. After you know the operations you need to take from pseudo code you can then translate them to the language specific functions and syntax.