Sort three variables: Difference between revisions

Add CLU
(Added solution for Little Man Computer)
(Add CLU)
Line 654:
11.17
11.3</pre>
 
=={{header|CLU}}==
<lang CLU>% Sort three variables.
% The variables must all be of the same type, and the type
% must implement the less-than comparator.
sort_three = proc [T: type] (x,y,z: T) returns (T,T,T)
where T has lt: proctype (T,T) returns (bool)
if y<x then x,y := y,x end
if z<y then y,z := z,y end
if y<x then x,y := y,z end
return(x,y,z)
end sort_three
 
% Test it out on three values, when also given a type and a
% formatter.
example = proc [T: type] (x,y,z: T, fmt: proctype (T) returns (string))
where T has lt: proctype (T,T) returns (bool)
po: stream := stream$primary_output()
% Print the variables
stream$putl(po, "x=" || fmt(x)
|| " y=" || fmt(y)
|| " z=" || fmt(z))
% Sort them
x,y,z := sort_three[T](x,y,z)
% Print them again
stream$putl(po, "x=" || fmt(x)
|| " y=" || fmt(y)
|| " z=" || fmt(z)
|| "\n")
end example
 
% And then we also need formatters, since those are not standardized
% such as '<' is.
fmt_real = proc (n: real) returns (string)
return(f_form(n,2,2))
end fmt_real
 
fmt_str = proc (s: string) returns (string)
return( "'" || s || "'" )
end fmt_str
 
% Test it out on values of each type
start_up = proc ()
example[int] (77444, -12, 0, int$unparse)
example[real] (11.3, -9.7, 11.17, fmt_real)
example[string] ("lions, tigers and", "bears, oh my!",
"(from the \"Wizard of Oz\")", fmt_str)
end start_up</lang>
{{out}}
<pre>x=77444 y=-12 z=0
x=-12 y=0 z=77444
 
x=11.30 y=-9.70 z=11.17
x=-9.70 y=11.17 z=11.30
 
x='lions, tigers and' y='bears, oh my!' z='(from the "Wizard of Oz")'
x='(from the "Wizard of Oz")' y='lions, tigers and' z='lions, tigers and'</pre>
 
=={{header|COBOL}}==
2,094

edits