Comparison Between C and C++ and Lisp and Prolog
Comparison Between C and C++ and Lisp and Prolog
Abstract:
this article introduces the difference between Imperative , functional ,OOP and logic
programming languages by using example language for each type.
Imperative
functional
a programming paradigm that uses "objects" – data structures consisting of data fields and
methods together with their interactions – to design applications and computer programs.
Programming techniques may include features such as information hiding, data abstraction,
encapsulation, modularity, polymorphism, and inheritance. I will use C++ programming
language as an example for it
logic programming
Logic programming is, in its broadest sense, the use of mathematical logic for computer
programming. I will use Prolog programming language as an example for it
١
Data types between C, C++, Lisp, Prolog
C
A C language programmer has to tell the system before-hand, the type of numbers or
characters he is using in his program. These are data types. There are many data types in C
language. A C programmer has to use appropriate data type as per his requirement.
In C language a user can define an identifier that represents an existing data type. The user
defined datatype identifier can later be used to declare variables. The general syntax is
here type represents existing data type and ‘identifier’ refers to the ‘row’ name given to the
data type.
Example:
Variables in C have not only the data type but also storage class that provides
information about their location and visibility. The storage class divides the portion of
٢
the program within which the variables are recognized.
Auto: It is a local variable known only to the function in which it is declared. Auto is
the default storage class.
Static: Local variable which exists and retains its value even after the control is
transferred to the calling function.
C++
TYPE SIZE (Byres) Range
Char 1 signed: -128 to 127
unsigned: 0 to 255
short int 2 signed: -32768 to 32767
unsigned: 0 to 65535
Int 4 signed: -2147483648 to
2147483647
unsigned: 0 to 4294967295
long int 4 signed: -2147483648 to
2147483647
unsigned: 0 to 4294967295
Bool 1 true or false
Float 4 +/- 3.4e +/- 38 (~7 digits)
Double 8 +/- 1.7e +/- 308 (~15 digits
long double 8 +/- 1.7e +/- 308 (~15 digits)
wchar_t 2 or 4 1 wide character
Lisp
The printed representation of an object is the format of the output generated by the Lisp
printer (the function prin1) for that object. The read syntax of an object is the format
of the input accepted by the Lisp reader (the function read) for that object.
• Comments
A comment is text that is written in a program only for the sake of humans that read
the program, and that has no effect on the meaning of the program.
٣
• Programming Types
• Editing Types
Prolog
Prolog's single data type is the term. Terms are either: atoms, numbers, and
variables
Or compound terms (structures). The figure below presents a classification of the
data types in Prolog:
٤
Program structure in C\C++\Lisp\Prolog
C
#include <stdio.h>
int main()
{
/* My first program */
printf("Hello, World! \n");
return 0;
}
C++
// my first program in C++
#include <iostream>
using namespace std;
int main ()
{
cout << "Hello World!";
return 0;
}
Lisp
(+ 2 2) ; adds 2 and 2, yielding 4.
٥
;; The variables a and b have lexical scope, unless the symbols have
been
;; marked as special variables (for instance by a prior DEFVAR).
(let ((a 6)
(b 4))
(+ a b)) ; returns 10
Prolog
A Prolog program consists of a database of facts, rules, and queries. This program is,
in essence, a knowledge base.
1-Arrays
Declaration of arrays:
type variable-name[50];
for example: float height[50];
Initialization of arrays:
type array_name[size]={list of values};
int number[3]={0,0,0};
Multi dimensional Arrays:
two dimension arrays
data_type array_name[row_size][column_size];
int m[10][20];
a 3 dimensional array
date_type array_name[s1][s2][s3]…..[sn];
int survey[3][5][12];
float table[5][4][5][3];
٦
2-Struct
struct lib_books
{
char title[20];
char author[15];
int pages;
float price;
};
3-Union:
union item
{
int m;
float p;
char c;
}
code;
C++
1-Arrays
Similar to C array
2-Struct
struct structure_name {
member_type1 member_name1;
member_type2 member_name2;
member_type3 member_name3;
.
.
} object_names;
EX:
struct product {
int weight;
float price;
} ;
product apple;
product banana, melon;
٧
3-Object oriebted in C++:
(a) Classes
A class is an expanded concept of a data structure: instead of holding only data,
it can hold both data and functions.
Classes are generally declared using the keyword class, with the following
format:
class class_name {
access_specifier_1:
member1;
access_specifier_2:
member2;
...
} object_names;
• private members of a class are accessible only from within other members of
the same class or from their friends.
• protected members are accessible from members of their same class and
from their friends, but also from members of their derived classes.
• public members are accessible from anywhere where the object is visible.
EX
// classes example
#include <iostream>
using namespace std;
class CRectangle {
int x, y;
public:
void set_values (int,int);
int area () {return (x*y);}
};
int main () {
CRectangle rect;
rect.set_values (3,4);
cout << "area: " << rect.area();
return 0;
}
٨
Lisp
1-array
Declaration of arrays:
(typep (make-array ... :element-type 'A ...)
'(array A)))
Initialization of arrays:
(make-array '(4 2 3)
:initial-contents
'(((a b c) (1 2 3))
((d e f) (3 1 2))
((g h i) (2 3 1))
((j k l) (0 0 0))))
2-Struct
different structures may print out in different ways; the definition of a structure type
may specify a print procedure to use for objects of that type (see the :print-
function option to defstruct). The default notation for structures is
#S(structure-name
slot-name-1 slot-value-1
slot-name-2 slot-value-2
...)
Prolog
Lists in Prolog
A list of terms can be represented betweenbrackets:[a, b, c, d] Its head is a and its tail is
[b, c, d].
The tail of [a] is [ ], the empty list.
Lists may contain lists:
[3.3, [a, 8, [ ]], [x], [p,q]] is a list of four items.
Special form to direct pattern matching:
• The term [X|Y] matches any list with at least one element:
X matches the head of the list, and Y matches the tail.
The term [X,Y,77|T] matches any list with at least three elements whose third element
is the number 77:X matches the first element,Y matches the second element, and T
matches rest of the list after the third item.
Using these pattern matching facilities, values can be specified as the intersection of
constraints on terms instead of by direct assignment.
Use variable names that are suggestive:
[ Head | Tail ] or [ H | T ]
٩
Branching in C\C++\Lisp\Prolog
if Statement:
if (condition)
statement;
EX: if (i=1)
printf("%d",i);
The syntax in the statement ‘a’ represents a complex if statement which combines
different conditions using the and operator in this case if all the conditions are true
only then the whole statement is considered to be true. Even if one condition is false
the whole if statement is considered to be false.
The statement ‘b’ uses the logical operator or (//) to group different expression to be
checked. In this case if any one of the expression if found to be true the whole
expression considered to be true, we can also uses the mixed expressions using logical
operators and and or together.
Nested if Statement
if (condition1)
if (condition2)
statement-1;
else
statement-2;
else
statement-3;
if (condition1)
statement – 1;
else if (condition2)
statement2;
else if (condition3)
statement3;
else if (condition)
١٠
statement n;
else
default statement;
statement-x;
Switch (expression)
{
Case case-label-1;
Case case-label-2;
Case case-label-n;
………………
Case default
}
a>
goto label;
…………
…………
…………
Label;
Statement;
b>
label;
…………
…………
…………
goto label;
EX:
#include <stdio.h> //include stdio.h header file to your program
main () //start of main
{
int n, sum = 0, i = 0 // variable declaration
C++
١١
LISP
cond
By using the convention that a zero value is false and a non-zero value is true, LISP
allows conditional branching and boolean logic.
The cond function takes a series of condition-result pairs. This construct is similar to
a Switch-Case block in C.
e.g.
( defun abs-val(x)
(cond ( (< x 0) (-x) )
( (>= x 0) x )
)
)
if
The if function takes an expression to be examined as true or false, and returns one of
its two other parameters, depending upon the result.
e.g.
Boolean operators
The and and or functions act as boolean operators. Their left to right checking gives
rise to a side-effect which is often used as a conditional branching technique. and
stops checking when it encounters one item which is false, while or stops checking
when it encounters one true item.
Prolog
if(Condition, TrueClause, FalseClause) :-
Condition, !, TrueClause;
!, FalseClause)
EX:
if(X, Y) :-
\+ var(X),
var(Y),
X is 1, % if this fails, prolog tries the next clause of if/2
١٢
Y is 0.
if(X, Y) :-
\+ var(X),
var(Y),
Y is 0.
?- if(0, A).
A = 1.
?- if(1, A).
A = 0.
Looping in C\c++\Lisp\Prolog
in looping process in general would include the following four steps
1. Setting and initialization of a counter
2. Exertion of the statements in the loop
3. Test for a specified conditions for the execution of the loop
4. Incrementing the counter
EX:
Continue statement:
Continue;
١٣
For Loop:
The for loop provides a more concise loop control structure. The general form of the
for loop is:
for (initialization; test condition; increment)
{
body of the loop
}
EX:
C++
Similar to C
Lisp
LISP has no loop constructs, so recursion is the only way to process data. A recursive
function is a function which calls itself. This technique recognizes that an operation
on some data may be best expressed as the aggregate of the same operation performed
on each item of data of which it is comprised. Obviously this technique is best used
with data structures which have the same form at both higher and lower levels,
differing only in scale.
This focus on recursion is the reason for LISP's popularity with AI researchers, who
often attempt to model large-scale behavior in terms of smaller-scale decisions. For
instance, recursion is often used in LISP to search state spaces.
Many of the lists used in LISP programs would be better referred to as trees. Lists are
simply the mechanism used to represent those trees.
The car and cdr functions are generally used to recourse (or 'walk') through the
elements in a tree, while cons is often used to gradually build tree structures to form
the result of a recursive operation. By also using the null function to test for an empty
١٤
list, we can walk through the tree structure, dealing with successively smaller pieces
of the tree.
car returns the first element of a list. e.g. ( car '(a b c) ) evaluates to a.
cdr returns the list with the first element removed. e.g. ( cdr '(a b c) ) evaluates to (b
c).
cons are an associated function which is used to build tree structures, often to form
the result of a recursive operation. Note that it does not simply concatenate lists, but
undoes the effects of a hypothetical use of car and cdr. e.g. ( cons '(a b) '(c d e) )
evaluates to ( (a b) (c d e)) rather than (a b c d e).
Note that the use of these functions can lead to a great deal of inefficient copying.
Prolog
command_loop:-
repeat,
write('Enter command (end to exit): '),
read(X),
write(X), nl,
X = end.
The last goal will fail unless end is entered. The repeat/0 always succeeds on
backtracking and causes the intermediate goals to be re-executed.
?- command_loop.
We will write a new predicate called do/1, which executes only the commands we
allow. Many other languages have 'do case' control structures that perform this kind of
function. Multiple clauses in a Prolog predicate behave similarly to a 'do case.'
Here is do/1. Notice that it allows us to define synonyms for commands, that is, the
player can enter either goto(X) or go(X) to cause the goto/1 predicate to be executed.
do(goto(X)):-goto(X),!.
do(go(X)):-goto(X),!.
do(inventory):-inventory,!.
do(look):-look,!.
NOTE: The cut serves two purposes. First, it says once we have found a 'do' clause to
execute, don't bother looking for anymore. Second, it prevents the backtracking
initiated at the end of command_loop from entering the other command predicates.
١٥
Here are some more do/1's. If do(end) did not always succeed, we would never get to
the' X = end' test and would fail forever. The last do/1 allows us to tell the user there
was something wrong with the command.
do(take(X)) :- take(X), !.
do(end).
do(_) :-
write('Invalid command').
We can now rewrite command_loop/0 to use the new do/1 and incorporate puzzle/1 in
the command loop. We will also replace the old simple test for end with a new
predicate, end_condition/1, that will determine if the game is over.
command_loop:-
write('Welcome to Nani Search'), nl,
repeat,
write('>nani> '),
read(X),
puzzle(X),
do(X), nl,
end_condition(X).
Two conditions might end the game. The first is if the player types 'end.' The second
is if the player has successfully taken the Nani.
end_condition(end).
end_condition(_) :-
have(nani),
write('Congratulations').
?- command_loop.
For list algorithms, the basis usually deals withan empty list, certainly the smallest list.
(Some algorithms for lists do not handle theempty list; so begin with a singleton list, [H]).
For the recursion step, we define the algorithmfor the arbitrary list, [H|T], assuming that it
works correctly for its tail T, a smaller list.
١٦
Sublist basis
Sublist recursion
List [H|T] is a sublist of the list [H|U] if list T is a sublist of list U starting at the first
position.
sublist([H|T], [H|U]) :- initialsublist(T,U). % 2
initialsublist([ ], L). % 3
initialsublist([H|T],[H|U]) :- initialsublist(T,U). % 4
Or the list S is a sublist of the list [H|T] if it is a
sublist of T.
These two cases correspond to the situation where the sublist begins at the start of the
list or the sublist begins later in the list, the only two possibilities.
Sample Executions
sublist([b,c,d], [a,b,c,d,e,f]) % 5
because sublist([b,c,d], [b,c,d,e,f]) % 2
because initialsublist([c,d], [c,d,e,f]) % 4
because initialsublist ([d], [d,e,f]) % 4
because initialsublist ([ ], [e,f]) % 3
sublist([b,d], [b,c,d]) fails % 2
because initialsublist([d], [c,d]) fails
and % 5
because sublist([b,d], [c,d]) fails % 5
because sublist([b,d], [d]) fails % 5
Resources
1-http://en.wikipedia.org/wiki/Logic_programming
2-http://www.exforsys.com/tutorials/c-language/decision-making-
looping-in-c.html
3-http://www.amzi.com/AdventureInProlog/a14cntrl.htm
4-http://www.murrayc.com/learning/AI/lisp.shtml
5-http://www.cplusplus.com/doc/tutorial/variables/
6-http://www.zvon.org/other/elisp/Output/SEC20.html
7-http://www.experiencefestival.com/a/prolog%20-
%20data%20types/id/1866789
8-http://www.csupomona.edu/~jrfisher/www/prolog_tutorial/2_7.html
١٧
١٨