Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • SEARCH
  • Home
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 6836119
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 26, 20262026-05-26T23:19:02+00:00 2026-05-26T23:19:02+00:00

Can anyone please provide me with an example that can help me to understand

  • 0

Can anyone please provide me with an example that can help me to understand Procedural, functional, Logic and Object Oriented programming models side by side by using nearly same example-problem.

Please give me example code-snippets of the somewhat same problem using Procedural, Functional, Logic and OO programming languages.

  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-05-26T23:19:02+00:00Added an answer on May 26, 2026 at 11:19 pm

    Let’s try simpler example – just calculating n-th Fibonacci number.

    First, procedural (in Pascal):

    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.
    

    This example shows features of procedural languages:

    • There are some subroutines (function in this case)
    • Variables are assigned value probably multiple times ( := operator)
    • There are cycles (for operator in this case)
    • Language is imperative, i.e. we are telling computer what to do in what order

    Second, object oriented (in Python):

    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)
    

    Actually the problem is not worth creating a class, so I added caching of already calculated results.

    This example shows:

    • class and its instantiation (creating instance)
    • class has own section of memory, own state (self and its members)
    • Language is imperative, i.e. we are telling computer what to do in what order

    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.

    Third, in functional style (Haskell):

    import Text.Printf
    
    fib :: Int -> Int
    fib 0 = 0
    fib 1 = 1
    fib n = fib (n-1) + fib (n-2)
    
    main = printf "%d\n" (fib 6)
    

    Following features of a functional programming paradigm are demonstrated:

    • there is no state, no variables – just functions defined
    • there are no cycles – only recursion
    • pattern matching: we separately defined “fib 0”, “fib 1” and “fib n” for rest of numbers, no constructs like if were needed
    • declarative style – we don’t define the order of steps to calculate main 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.
    • 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.

    But the main feature of functional languages is that functions are first class objects.
    This can be demonstrated by other implementation of fib:

    fib n = fibs!!n
    fibs = 0 : 1 : zipWith (+) fibs (tail fibs)
    

    Here we are passing fibs function as parameter to zipWith function.
    This example also demonstrates lazy evaluation: “infinite” list is computed only to extent it is needed for other functions.

    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.

    Prolog:

    fib(1, 1).
    fib(2, 1).
    
    
    fib(X, Y):-
            X > 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.
    

    Following features of logic programming style can be seen:

    • Language is declarative. As in functional style, we define things and not telling in what order to do them.
    • 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!
    • There is no input values and return value – instead of this we define a relation between “input” and “output”.

    This program could also be used to find out that Fibonacci number 8 is at 6th position in the sequence:

    ?- between(0,inf,X), fib(X,8).
    X = 6 .
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

Question: Can anyone please provide a full code example that shows how one does
I am trying to find a good example that can help me to understand
Can anyone please post a Python example that demonstrates the use of a request/response
Can anyone please provide a link to download the .NET 3.5 SDK? I checked
can anyone please suggest a good code example of vb.net/c# code to put the
Can anyone please provide me in layman's terms the difference between developing a JRuby
Can anyone please provide me an explanation as to why some Linux expert suggest
Please I have a problem understanding references in perl . Can anyone provide a
Can anyone please provide me a sample build file demostrating the use of the
What is the key difference between object and component? Can anyone provide examples in

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.