meljun cortes programming languages other programming paradigms

Upload: meljun-cortes-mbampa

Post on 01-Jun-2018

231 views

Category:

Documents


0 download

TRANSCRIPT

  • 8/8/2019 MELJUN CORTES Programming Languages Other Programming Paradigms

    1/21

    Other Programming Paradigms

    Programming Languages

    * Property of STI 

    Page 1 of 21

    Concurrent

    Programming

    It is a computer programming techniquethat provides for the execution of

    operations concurrently, either with asingle computer or across a number ofsystems.

    Single tasks are split into a number ofsubtasks that can be computed relatively

    independently and then aggregated to forma single coherent solution.

  • 8/8/2019 MELJUN CORTES Programming Languages Other Programming Paradigms

    2/21

    Other Programming Paradigms

    Programming Languages

    * Property of STI 

    Page 2 of 21

    Reader-Writer Problem

     The syntactic structure of tasks is similarto that of package, which also has two

     basic parts: a specification part and a body .  The specification part can contain “entry ”

    statement only while the body contains thecorresponding “accept ” statement.

    Local tasks become active when the parent

    task reaches the “begin ” following the taskdeclaration.

     The means of communication andsynchronization between tasks is achievedusing the concept of collecting between a

    task issuing an entry call and a taskaccepting the call by an accept statement.

  • 8/8/2019 MELJUN CORTES Programming Languages Other Programming Paradigms

    3/21

    Other Programming Paradigms

    Programming Languages

    * Property of STI 

    Page 3 of 21

    Reader-Writer Problem

     with TEXT_IO; use TEXT_IO;

    procedure READER_WRITER_TASK is

    type ELEM is array(1…80) of CHARACTER;task READER_WRITER is

    entry READ(V : out ELEM);

    entry WRITE(E : in ELEM);

    end;

    task body READER_WRITER is

     VARIABLE: ELEM; begin

    accept WRITER(E : IN ELEM) do

     VARIABLE:= E;

    end;

  • 8/8/2019 MELJUN CORTES Programming Languages Other Programming Paradigms

    4/21

    Other Programming Paradigms

    Programming Languages

    * Property of STI 

    Page 4 of 21

    Reader-Writer Problem

      ontinuation…

    loopselect 

    accept READ(V : out ELEM) do

     V := VARIABLE;

    end;

    or 

    accept WRITE (E : in ELEM) do VARIABLE:=E;

    end;

    end select;

    end loop

    end READER_WRITER  begin – -parent task 

    -- Statements that use the READER_WRITERin a meaningful way go here.

    -- Note that this program will not terminate because of the infinite loop.

    end READER_WRITER_TASK 

  • 8/8/2019 MELJUN CORTES Programming Languages Other Programming Paradigms

    5/21

    Other Programming Paradigms

    Programming Languages

    * Property of STI 

    Page 5 of 21

    Delay Statement  This statement suspends the execution of

    the next statement in the applicationprogram for the specified period of time.

    delay  

    Simple Timer

     with TEXT_IO; use TEXT_IO;

     procedure TIMER is

    task MIN_TIMER 

    entry START;

    entry STOP;

    end MIN_TIMER;

    task body MIN_TIMER is

     begin

    loopselect

    accept START;

    delay 60.0;

    or

    accept STOP;

    orterminate;

  • 8/8/2019 MELJUN CORTES Programming Languages Other Programming Paradigms

    6/21

    Other Programming Paradigms

    Programming Languages

    * Property of STI 

    Page 6 of 21

    Delay Statement

      ontinuation…

    end selectend loop

    end MIN_TIMER 

     begin

     put (“Start timer ”);

     MIN_TIMER.START; -- start the delay

    -- sequence of statements that take lessthan one minute to execute.

     MIN_TIMER.STOP;

    -- the statements that follow cannot begin execution until one minute haselapsed.

     putline (“”);

     put (“End of Timer”)end TIMER 

    Task Termination

    termination occurs when the task reachesthe end of its task body and all locally

    declared tasks have terminated theirexecution

  • 8/8/2019 MELJUN CORTES Programming Languages Other Programming Paradigms

    7/21

    Other Programming Paradigms

    Programming Languages

    * Property of STI 

    Page 7 of 21

    Functional

    Programming

    It is a way of writing a program thatdescribes only the operations to be

    performed on the inputs to the program.

    LISP was proposed by John McCarthy1959 for symbolic programming language.

    It was designed to implement modelcomputation.

    In 1983, Common LISP was proposed byDARPA (Defense Advanced ResearchProjects Agency) for a standard design.

     The values of LISP is called “S-expressions”or “symbolic expression”.

    S-expressions can be a symbol, a number,or a list.

  • 8/8/2019 MELJUN CORTES Programming Languages Other Programming Paradigms

    8/21

    Other Programming Paradigms

    Programming Languages

    * Property of STI 

    Page 8 of 21

    Syntactic Elements

    of LISP

     The list of zero element is nil list or simplynil .

    “Symbol” is a LISP term for name in the basic evaluator.

    Nil is the false value and non-Nil value

    represents true.

     Atom is the basic syntactic element of LISPthat can be a number, a character, acontinuous stream of characters of stringsor nil .

    Lists represent program and data at thesame time.

  • 8/8/2019 MELJUN CORTES Programming Languages Other Programming Paradigms

    9/21

    Other Programming Paradigms

    Programming Languages

    * Property of STI 

    Page 9 of 21

    LISP Interpreter

    LISP interpreter completes the followingsteps:

    Prompt user for input (only for interactivemode)

    • Read list 

    • Evaluate list 

    • Print result 

    Continuous loop of the steps above

    In evaluating an s-expression, several values are returned:

    If the s-expression is a number, return the value of the number.

    If the s-expression is an atomic symbol,

    return the value bound to that symbol; if itis not bound, it is an error.

    If the s-expression is a list, evaluate thesecond through the last arguments andapply the function indicated by the firstargument to the results.

  • 8/8/2019 MELJUN CORTES Programming Languages Other Programming Paradigms

    10/21

    Other Programming Paradigms

    Programming Languages

    * Property of STI 

    Page 10 of 21

    Controlling LISP

    Evaluation

     The constructs for controlling evaluation inLISP are

    QUOTE

    andEV L

    .

     To treat expressions as data, QUOTE isneeded to evaluate itself.

    lisp> (quote (a b c))

    (a b c)

    lisp> (quote (+ 1 3))

    (+ 1 3)

    lisp> „ (a b c)

    (a b c)

    lisp> (list „(+ 1 2) „(+ 3))

    ((+ 1 2) (+ 3 4))

     While QUOTE forces non-evaluation, EV Lobviously does the opposite.

    lisp> (eval (+ 1 3))

    4

    lisp> (list eval (+ 1 2) eval (+ 3 4))(3 7)

  • 8/8/2019 MELJUN CORTES Programming Languages Other Programming Paradigms

    11/21

    Other Programming Paradigms

    Programming Languages

    * Property of STI 

    Page 11 of 21

    User-Defined Functions

    General syntax for user-defined function:

    (defun ( )

     )

    Example:

    (defun cube (X)

    (* x x x))

    DEFUN does not evaluate its arguments, but defines a new function.

  • 8/8/2019 MELJUN CORTES Programming Languages Other Programming Paradigms

    12/21

    Other Programming Paradigms

    Programming Languages

    * Property of STI 

    Page 12 of 21

    Program Control LISP also has its statements for program

    control which is different from evaluationcontrol.

    COND

    Conditions and actions are arbitraryexpressions.

    cond ( )

    ( ). . .

    ( ))

    Example of COND used in expression:

    (defun absolute-value (x)

    (cond ( (< x 0) (- x))

    ( (>= x 0) x)))

    (defun absolute-value (x)

    (cond ((< x 0) (- x))

    (t x)))

  • 8/8/2019 MELJUN CORTES Programming Languages Other Programming Paradigms

    13/21

    Other Programming Paradigms

    Programming Languages

    * Property of STI 

    Page 13 of 21

    Program Control

    IF-THEN-ELSE

     This LISP construct works similarly with

    other programming languages.

    Example:

    (defun absolute-value (x)

    (if (< x 0) (- x) x))

    (defun count (n)

    (if (

  • 8/8/2019 MELJUN CORTES Programming Languages Other Programming Paradigms

    14/21

    Other Programming Paradigms

    Programming Languages

    * Property of STI 

    Page 14 of 21

    Operations on Lists

    LISP as a list based programming languageis obviously designed for list operations.

     Three basic list operations: CAR , CDR andCONS.

    Given a list:

    S, (S1,…,Sn), n>0, then (car S) is S1

    From above, CAR is summarized as a listtruncation function that returns the firstelement of any given list.

    CAR extracts the head or first element of anonempty list.

    Example:

    lisp> (car „(a b c))a

  • 8/8/2019 MELJUN CORTES Programming Languages Other Programming Paradigms

    15/21

    Other Programming Paradigms

    Programming Languages

    * Property of STI 

    Page 15 of 21

    Operations on Lists

    Given a list:

    S, (S1,…, Sn), n>0, then (cdr S) is(S2,…Sn)

    CDR returns the elements of a list exceptthe first element.

    Example:

    lisp> (cdr „ (a b c))

    (b c)

    CDR extracts the tail, consisting all but thefirst element.

    CAR and CDR are similar to stringmanipulation functions in C++ and VisualBasic. CAR can be used to list the elementsof a list while CDR can be used to get thelist of elements remaining excluding thefirst element.

  • 8/8/2019 MELJUN CORTES Programming Languages Other Programming Paradigms

    16/21

    Other Programming Paradigms

    Programming Languages

    * Property of STI 

    Page 16 of 21

    Operations on Lists

    CONS operation build lists: creates value

     with head and tail.

    It can also be represented as a “dotted”notation. (a.x)

    Given a list:

    lisp> (cons 1 „(2 3 4))

    (1 3 4)

    this is the same as:

    lisp> (car(1) cdr(2 3 4))

    (1 3 4)

  • 8/8/2019 MELJUN CORTES Programming Languages Other Programming Paradigms

    17/21

    Other Programming Paradigms

    Programming Languages

    * Property of STI 

    Page 17 of 21

     Variables

     Variable is a symbol denoting a quantity ora symbolic representation.

    LISP has a function set with twoarguments:

    it must be a symbol

    it can be an expression that will beevaluated and the result bound to the

    symbol (set „x 0)

     The function setq serves as an easier

     version because it does not try to evaluatethe first argument, and therefore, it doesnot need a quote. (setq x 0)

     Variables exist in a global environment.However, if a value is bound to a symbolthat is used as formal parameter, theoriginal value will be restored when the

    function returns.

  • 8/8/2019 MELJUN CORTES Programming Languages Other Programming Paradigms

    18/21

    Other Programming Paradigms

    Programming Languages

    * Property of STI 

    Page 18 of 21

    Local Variables

     A local variable is declared inside a singlemethod and can be seen only by code

     within that method.

     The LET function allows specifying local variables

    Syntax:

    (let () )

     with () ::=( )

    Example:

    (defun functionx (x y)

    (let (a (b 3)

    (setq a (* x y))

    (- (* a x) b))))

  • 8/8/2019 MELJUN CORTES Programming Languages Other Programming Paradigms

    19/21

    Other Programming Paradigms

    Programming Languages

    * Property of STI 

    Page 19 of 21

    Local Variables

     The following is a factorial function writtenin an iterative manner.

    (defun fact (n)

    (setf n! 1) ;

    Initialization

    (dotimes (I n n!) ; Loop

    (setf n! (* n! (+ I 1))))) ; Body

     The example below shows theimplementation of a function that removesan element from a list.

    (defun without (el li)(cond ((null li) nil); Test if li is

    empty

    ((equalp el (car li)) (cdr li))

    (t (cons (car li) (without el (cdr

    li))))))

  • 8/8/2019 MELJUN CORTES Programming Languages Other Programming Paradigms

    20/21

    Other Programming Paradigms

    Programming Languages

    * Property of STI 

    Page 20 of 21

    LISP Input/Output

    Read is a predefined input function in

    LISP.

    Syntax:

    (read STREAM)

     The Read function acquires one LISPexpression as text from STREAM andreturns it as a LISP object.

    If STREAM is nil, use the value of ‘standard-input’. STREAM or the value of ‘standard-

    input’ may be a buffer, a marker, a function

    or a string.

    Example:

    (read)

    (setf x (read)) ; assigns theinput to x

  • 8/8/2019 MELJUN CORTES Programming Languages Other Programming Paradigms

    21/21

    Other Programming Paradigms

    Programming Languages

    * Property of STI

    LISP Input/Output

     The Print function is a predefined LISP

    output function.

    Syntax:

    (print OBJ STREAM)

     This function outputs the printedrepresentation of OBJECT to STREAM,

     which prints in addition one newline beforethe object and another after it.

    Example:

    (print x)

    (print (list x y z))