Balanced brackets: Difference between revisions
m
→{{header|Picat}}: Moved the approaches into subsections, added {{out}}
m (→{{header|Picat}}: Moved the approaches into subsections, added {{out}}) |
|||
Line 5,506:
=={{header|Picat}}==
===Foreach loop===
<lang picat>
go1,▼
nl.▼
go => true.▼
tests(Tests),
member(Test,Tests),
Line 5,526 ⟶ 5,519:
nl.
go1 => true.
go_dcg ?=>▼
tests(Tests),▼
foreach(Test in Tests)▼
printf("%s: ", Test),▼
if balanced(Test,[]) then▼
println("OK")▼
else▼
println("NOT OK")▼
end▼
% Check if a string of [] is balanced
Line 5,548 ⟶ 5,527:
end,
C == 0.
balanced --> "".▼
balanced --> "[", balanced, "]", balanced.▼
tests(["","[]", "[][]", "[[][]]", "][",
"][][", "[]][[]", "[][][][][][][][][][]",
"[[[[[[[]]]]]]]", "[[[[[[[]]]]]]",
"[][[]][]","[[][]][]", "[][][[]][]"]).</lang>
{{out}}
<pre>: OK
[]: OK
[][]: OK
Line 5,574 ⟶ 5,546:
[][[]][]: OK
[[][]][]: OK
[][][[]][]: OK</pre>
===DCG===
Here is an implementation using DCG (Definite Clause Grammars).
▲<lang Picat>go_dcg ?=>
▲ tests(Tests),
▲ foreach(Test in Tests)
▲ printf("%s: ", Test),
▲ if balanced(Test,[]) then
▲ println("OK")
▲ else
▲ println("NOT OK")
▲ end
▲ nl.
▲balanced --> "".
▲balanced --> "[", balanced, "]", balanced.</lang>
=={{header|PicoLisp}}==
|