Ternary logic: Difference between revisions

no edit summary
(→‎{{header|Vlang}}: Rename "Vlang" in "V (Vlang)")
No edit summary
Line 6,290:
True | False Maybe True
</pre>
 
=={{header|SparForte}}==
As a structured script.
<syntaxhighlight lang="ada">#!/usr/local/bin/spar
pragma annotate( summary, "ternary_logic" )
@( description, "In logic, a three-valued logic (also trivalent, " )
@( description, "ternary, or trinary logic, sometimes abbreviated " )
@( description, "3VL) is any of several many-valued logic systems " )
@( description, "in which there are three truth values indicating " )
@( description, "true, false and some indeterminate third value. " )
@( see_also, "http://rosettacode.org/wiki/Ternary_logic" );
pragma annotate( author, "Ken O. Burtch" );
pragma license( unrestricted );
 
pragma restriction( no_external_commands );
 
procedure ternary_logic is
 
type ternary is (no, maybe, yes);
 
function ternary_and( left : ternary; right : ternary ) return ternary is
begin
if left < right then
return left;
else
return right;
end if;
end ternary_and;
 
function ternary_or( left : ternary; right : ternary ) return ternary is
begin
if left > right then
return left;
else
return right;
end if;
end ternary_or;
 
function ternary_not( right : ternary ) return ternary is
begin
case right is
when yes => return no;
when maybe => return maybe;
when no => return yes;
when others => put_line( "Unexpected value" );
end case;
end ternary_not;
 
function ternary_image( ternary_value : ternary ) return string is
begin
case ternary_value is
when yes => return "Yes";
when no => return "No";
when maybe => return "Maybe";
when others => put_line( "Unexpected value" );
end case;
end ternary_image;
 
begin
? "Ternary Not:"
@ "not no => " & ternary_image( ternary_not( no ) )
@ "not maybe => " & ternary_image( ternary_not( maybe ) )
@ "not yes => " & ternary_image( ternary_not( yes ) );
new_line;
 
? "Ternary And:"
@ "no and no => " & ternary_image( ternary_and( no, no ) )
@ "no and maybe => " & ternary_image( ternary_and( no, maybe ) )
@ "no and yes => " & ternary_image( ternary_and( no, yes ) )
@ "maybe and no => " & ternary_image( ternary_and( maybe, no ) )
@ "maybe and maybe => " & ternary_image( ternary_and( maybe, maybe ) )
@ "maybe and yes => " & ternary_image( ternary_and( maybe, yes ) )
@ "yes and no => " & ternary_image( ternary_and( yes, no ) )
@ "yes and maybe => " & ternary_image( ternary_and( yes, maybe ) )
@ "yes and yes => " & ternary_image( ternary_and( yes, yes ) );
new_line;
 
? "Ternary Or:"
@ "no or no => " & ternary_image( ternary_or( no, no ) )
@ "no or maybe => " & ternary_image( ternary_or( no, maybe ) )
@ "no or yes => " & ternary_image( ternary_or( no, yes ) )
@ "maybe or no => " & ternary_image( ternary_or( maybe, no ) )
@ "maybe or maybe => " & ternary_image( ternary_or( maybe, maybe ) )
@ "maybe or yes => " & ternary_image( ternary_or( maybe, yes ) )
@ "yes or no => " & ternary_image( ternary_or( yes, no ) )
@ "yes or maybe => " & ternary_image( ternary_or( yes, maybe ) )
@ "yes or yes => " & ternary_image( ternary_or( yes, yes ) );
new_line;
end ternary_logic;</syntaxhighlight>
{{out}}
<pre>
$ spar ternary_logic.sp
Ternary Not:
not no => Yes
not maybe => Maybe
not yes => No
 
Ternary And:
no and no => No
no and maybe => No
no and yes => No
maybe and no => No
maybe and maybe => Maybe
maybe and yes => Maybe
yes and no => No
yes and maybe => Maybe
yes and yes => Yes
 
Ternary Or:
no or no => No
no or maybe => Maybe
no or yes => Yes
maybe or no => Maybe
maybe or maybe => Maybe
maybe or yes => Yes
yes or no => Yes
yes or maybe => Yes
yes or yes => Yes</pre>
 
=={{header|Tcl}}==
76

edits