[Home]  [Edit this page]  [Recent Changes]  [Special Pages]  [Help
PrologCpp

(Prolog) Prolog and C++ syntax comparison

This page translates C++ structures and syntaxes to their Prolog equivalents

Overview

The Prolog equivalent of the C++ switch

Using the cut, !, you can make statements similar to a C++ switch statement.

(see also: Prolog switch)

% If the user enters a number from 1 to an including 3
% the right clauses are called.
% If you would remove the cuts,
% then the input '2' would results in calling both action(2) and action(_)
%
% Note that you can remove 'nondeterm' from the action's predicate,
% but I left it in, so you do can comment out the cuts
%
% Compiled under Visual Prolog 5.2
PREDICATES
  nondeterm action(integer) % The switch switches on an integer
	
CLAUSES
  action(1) :-              % Equivalent to 'case 1:' 
    !,
    nl,
    write("You typed one").
  action(2) :-              % Equivalent to 'case 2:' 
    !,
    nl,
    write("You typed two").
  action(3) :-              % Equivalent to 'case 3:' 
    !,
    nl,
    write("You typed three").
  action(_) :-              % Equivalent to 'default:' 
    !,
    nl,
    write("You typed an unknown number").
	
GOAL
  write("Type a number from 1 to 3: "),
  readInt(Num),
  action(Num),
  nl.


Note that the C++ 'falling through cases' has no direct Prolog equivalent, as the structure differs.

To give an example, the C++ code below CAN be translated to Prolog:

//C++ code
int x = 0; 
switch (x)
{
  case 0:  /* Something */ ; /* No break! Fall through to next case */
  case 1:  /* Something */ ; /* No break! Fall through to next case */
  default: /* Something */ ; 
}


% Compiled under Visual Prolog 5.2
PREDICATES
  nondeterm action(integer) % The switch switches on an integer
	
CLAUSES
  action(X) :-              
    X <= 0,
    % !, % Needed to remove the cuts
    nl,
    write("You typed zero").
  action(X) :-              
    X <= 1,
    % !, % Needed to remove the cuts
    nl,
    write("You typed one").
  action(_) :-              
    % !, % Needed to remove the cuts
    nl,
    write("You typed an other number").
	
GOAL
  write("Type a number 0,1 or different: "),
  readInt(Num),
  action(Num),
  nl.


The Prolog equivalent of the C++ std::map

% The Prolog predicate of a C++ std::map<int,int>
% Compiled under Visual Prolog 5.2
PREDICATES 
  % Declare the Prolog equivalent of a std::map<int,int> 
  % A std::map has a single key to a single value,
  % therefore the predicate is deterministic
  % input = key, output = value
  determ mapValues(Integer,Integer) - (i,o). % -(i,o) is non-standard flowcontrol, you can remove it.
CLAUSES 
  % Map the keys to their values
  mapValues(0,50).
  mapValues(1,40).
  mapValues(2,30).
  mapValues(3,20).
  mapValues(4,10).
  % mapValues(4,20). % NOT POSSIBLE! That would make the predicate 'mapValues' nondeterm!
  
GOAL 
  X = 2,                      % The value you want to look-up
  mapValues(X,Y),             % Get the value of the key X
  write("X: "), write(X), nl, % Write the key to screen
  write("Y: "), write(Y), nl. % Write the value to screen


last edited (February 7, 2007) by bilderbikkel, Number of views: 1693, Current Rev: 7 (Diff)

[Edit this page]  [Page history]  [What links here]  [Discuss this topic]  [Printer Friendly]  

Members

Username:

Password:


Register
Forgot Password?




Programmers Heaven - for .NET, Java, C/C++ and WEB Developers!
© 1996-2008 Community Networks Ltd. All rights reserved. Reproduction in whole or in part, in any form or medium without express written permission is prohibited. Violators of this policy may be subject to legal action. Please read Terms Of Use and Privacy Statement for more information. Development by Tore Nestenius at .NET Consultant - Synchron Data.