N'th: Difference between revisions

Content added Content deleted
Line 2,931: Line 2,931:
1000th 1001st 1002nd 1003rd 1004th 1005th 1006th 1007th 1008th 1009th 1010th 1011th 1012th 1013th 1014th 1015th 1016th 1017th 1018th 1019th 1020th 1021st 1022nd 1023rd 1024th 1025th
1000th 1001st 1002nd 1003rd 1004th 1005th 1006th 1007th 1008th 1009th 1010th 1011th 1012th 1013th 1014th 1015th 1016th 1017th 1018th 1019th 1020th 1021st 1022nd 1023rd 1024th 1025th
</pre>
</pre>

=={{header|Picat}}==
nth/3 is a built-in predicate in Picat, so let's call the function nth2/1 and onward.

* nth2/1: {{trans|Prolog}}
* nth3/1: Function based with explicit conditions in head.
* nth4/1: {{trans|Python}}

<lang Picat>go =>
Ranges = [ 0..25, 250..265, 1000..1025],
foreach(Range in Ranges) println([nth2(I) : I in Range])
end,
nl.

%
% Translation of Prolog version
%
nth2(N) = N.to_string() ++ Th =>
( tween(N) -> Th = "th"
; 1 = N mod 10 -> Th = "st"
; 2 = N mod 10 -> Th = "nd"
; 3 = N mod 10 -> Th = "rd"
; Th = "th" ).
tween(N) => Tween = N mod 100, between(11, 13, Tween).

%
% Using explicit conditions
%
nth3(N) = cc(N,"th"), tween(N) => true.
nth3(N) = cc(N,"st"), N mod 10 = 1 => true.
nth3(N) = cc(N,"nd"), N mod 10 = 2 => true.
nth3(N) = cc(N,"rd"), N mod 10 = 3 => true.
nth3(N) = cc(N,"th") => true.
% helper function
cc(N,Th) = N.to_string() ++ Th.

%
% Translation of the Python version
%
nth4(N) = Nth =>
Suffix = ["th","st","nd","rd","th","th","th","th","th","th"],
Nth = N.to_string() ++ cond((N mod 100 <= 10; N mod 100 > 20), Suffix[1 + N mod 10], "th").</lang>

{{out}}
<pre>[0th,1st,2nd,3rd,4th,5th,6th,7th,8th,9th,10th,11th,12th,13th,14th,15th,16th,17th,18th,19th,20th,21st,22nd,23rd,24th,25th]
[250th,251st,252nd,253rd,254th,255th,256th,257th,258th,259th,260th,261st,262nd,263rd,264th,265th]
[1000th,1001st,1002nd,1003rd,1004th,1005th,1006th,1007th,1008th,1009th,1010th,1011th,1012th,1013th,1014th,1015th,1016th,1017th,1018th,1019th,1020th,1021st,1022nd,1023rd,1024th,1025th]</pre>


=={{header|PicoLisp}}==
=={{header|PicoLisp}}==