Balanced brackets: Difference between revisions
Content added Content deleted
m (→{{header|Sidef}}: code simplification) |
(L++) |
||
Line 2,275: | Line 2,275: | ||
Starting with Julia v0.3, a foldl function is available, so that balanced may be written shortly (but not as efficiently) as follows: |
Starting with Julia v0.3, a foldl function is available, so that balanced may be written shortly (but not as efficiently) as follows: |
||
<lang Julia>balanced(str) = foldl((x,y)->x<0? -1: x+y, 0, [(x=='[')-(x==']') for x=str])==0</lang> |
<lang Julia>balanced(str) = foldl((x,y)->x<0? -1: x+y, 0, [(x=='[')-(x==']') for x=str])==0</lang> |
||
=={{header|L++}}== |
|||
<lang lisp>(include "string") |
|||
(defn bool balanced (std::string s) |
|||
(def bal 0) |
|||
(foreach c s |
|||
(if (== c |'['|) (++ bal) |
|||
(if (== c |']'|) (-- bal))) |
|||
(if (< bal 0) (return false))) |
|||
(return (== bal 0))) |
|||
(main |
|||
(decl std::string |tests[]| |{"", "[]", "[][]", "[[][]]", "][", "][][", "[]][[]"}|) |
|||
(prn std::boolalpha) |
|||
(foreach x tests |
|||
(prn x "\t" (balanced x))))</lang> |
|||
=={{header|Lasso}}== |
=={{header|Lasso}}== |