Note that there are some explanatory texts on larger screens.

plurals
  1. PO
    primarykey
    data
    text
    <p>Let's try simpler example - just calculating n-th Fibonacci number.</p> <p>First, procedural (in Pascal):</p> <pre><code>program Fibonacci; function fib(n: Integer): Integer; var a: Integer = 1; b: Integer = 1; f: Integer; i: Integer; begin if (n = 1) or (n = 2) then fib := 1 else begin for i := 3 to n do begin f := a + b; b := a; a := f; end; fib := f; end; end; begin WriteLn(fib(6)); end. </code></pre> <p>This example shows features of procedural languages: </p> <ul> <li>There are some subroutines (function in this case)</li> <li>Variables are assigned value probably multiple times ( <strong>:=</strong> operator)</li> <li>There are cycles (<strong>for</strong> operator in this case)</li> <li>Language is imperative, i.e. we are telling computer what to do in what order</li> </ul> <p>Second, object oriented (in Python):</p> <pre><code>class Fibonacci: def __init__(self): self.cache = {} def fib(self, n): if self.cache.has_key(n): return self.cache[n] if n == 1 or n == 2: return 1 else: a = 1 b = 1 for i in range(2, n): f = a + b; b = a; a = f; self.cache[n] = f; return f; fibonaccyCounter = Fibonacci() print fibonaccyCounter.fib(6) </code></pre> <p>Actually the problem is not worth creating a class, so I added caching of already calculated results.</p> <p>This example shows:</p> <ul> <li>class and its instantiation (creating instance)</li> <li>class has own section of memory, own state (<strong>self</strong> and its members)</li> <li>Language is imperative, i.e. we are telling computer what to do in what order</li> </ul> <p>Not shown but we can e.g. descend this class from abstract class returning n-th member of some sequence. By subslassing we get class defining Fibonacci sequence, sequence 1,2,3..., sequence 1,4,9,16,... etc.</p> <p>Third, in functional style (Haskell):</p> <pre><code>import Text.Printf fib :: Int -&gt; Int fib 0 = 0 fib 1 = 1 fib n = fib (n-1) + fib (n-2) main = printf "%d\n" (fib 6) </code></pre> <p>Following features of a functional programming paradigm are demonstrated:</p> <ul> <li>there is no state, no variables - just functions defined</li> <li>there are no cycles - only recursion</li> <li>pattern matching: we separately defined "fib 0", "fib 1" and "fib n" for rest of numbers, no constructs like <strong>if</strong> were needed</li> <li>declarative style - we don't define the order of steps to calculate <strong>main</strong> function value: the compiler/interpreter/runtime figures this out by itself, given the function definitions. We tell the computer what we want to get, not what to do.</li> <li>Lazy evaluation. If main was calling only "fib 2" then "fib n" was not called because functions are evaluated only when their result is needed to be passed as parameter to other functions.</li> </ul> <p>But the main feature of functional languages is that functions are first class objects. This can be demonstrated by other implementation of <code>fib</code>:</p> <pre><code>fib n = fibs!!n fibs = 0 : 1 : zipWith (+) fibs (tail fibs) </code></pre> <p>Here we are passing <code>fibs</code> function as parameter to <code>zipWith</code> function. This example also demonstrates lazy evaluation: "infinite" list is computed only to extent it is needed for other functions.</p> <p>By the way, functional does not necessary mean not object oriented. An example of programming language that is both functional and object oriented is Scala.</p> <p>Prolog:</p> <pre><code>fib(1, 1). fib(2, 1). fib(X, Y):- X &gt; 1, X1 is X - 1, X2 is X - 2, fib(X1, Z), fib(X2, W), Y is W + Z. main :- fib(6,X), write(X), nl. </code></pre> <p>Following features of logic programming style can be seen:</p> <ul> <li>Language is declarative. As in functional style, we define things and not telling in what order to do them.</li> <li>But the difference with functional style is that we define predicates, not functions. In this case, predicate fib(X, Y) means "X-th Fibonacci number is Y". Given some known predicates (fib(1, 1) and fib(2, 1) - i.e. first Fibonacci number is 1 and second Fibonacci number is 1) and rules to infer other predicates (Y is X-th Fibonacci number is Y is a sum of X-1th Fibonacci number and X-2th Fibonacci number), Prolog infers predicates in question. Actually there could be more than 1 answer!</li> <li>There is no input values and return value - instead of this we define a relation between "input" and "output". </li> </ul> <p>This program could also be used to find out that Fibonacci number 8 is at 6th position in the sequence:</p> <pre><code>?- between(0,inf,X), fib(X,8). X = 6 . </code></pre>
    singulars
    1. This table or related slice is empty.
    plurals
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. VO
      singulars
      1. This table or related slice is empty.
    2. VO
      singulars
      1. This table or related slice is empty.
    3. VO
      singulars
      1. This table or related slice is empty.
 

Querying!

 
Guidance

SQuiL has stopped working due to an internal error.

If you are curious you may find further information in the browser console, which is accessible through the devtools (F12).

Reload