N'th: Difference between revisions
Content deleted Content added
No edit summary |
→{{header|Picat}}: Split into subsections |
||
Line 2,933:
=={{header|Picat}}==
<code>nth/3</code> is a built-in predicate in Picat, so let's call the
===Prolog style===
<lang Picat>nth2(N) = N.to_string() ++ Th =>▼
* 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.▼
▲nth2(N) = N.to_string() ++ Th =>
( tween(N) -> Th = "th"
; 1 = N mod 10 -> Th = "st"
Line 2,954 ⟶ 2,942:
; 3 = N mod 10 -> Th = "rd"
; Th = "th" ).
tween(N) => Tween = N mod 100, between(11, 13, Tween).</lang>
<lang Picat>nth3(N) = cc(N,"th"), tween(N) => true.▼
▲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.
Line 2,965 ⟶ 2,951:
nth3(N) = cc(N,"th") => true.
% helper function
cc(N,Th) = N.to_string() ++ Th.</lang>
===List of suffixes===
<lang Picat>nth4(N) = Nth => ▼
▲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>
===Test===
▲<lang Picat>go =>
▲ Ranges = [ 0..25, 250..265, 1000..1025],
▲ foreach(Range in Ranges) println([nth2(I) : I in Range])
▲ end,
▲ nl.</lang>
{{out}}
|