Jump to content

Balanced brackets: Difference between revisions

no edit summary
No edit summary
No edit summary
Line 4,561:
 
=={{header|Ya}}==
<lang Ya>@Balanced[]s // each source must be started by specifying its file name; std extension .Ya could be ommitted and auto added by compiler
<lang Ya>
@Balanced[]s
 
// All statements are prefixed by sigil ';' and no ';'s after statements are used
;Use @Std/StdIO
;Use @Std/StdIO // because of using function Print
 
// function Shuffle returns `No which is a synonym of `Void
// all types are prefixed by '`'
// args are written without ()
// `Char[] is a simplest array of chars, which does not hold a memory used by arry items
;`No Shuffle `Char[] brackets
// note: there is tab char at starts of many lines; this tabs specify what in C++ is {}, just like in Python
;For; ++brackets
;For; ++brackets // all statements are analogous to C++ but written starting by capital letter: For If Switch Return
;`Int+ at = Random brackets/Length // `Int+ is C++'s unsigned int
;brackets[-1], brackets[at] = brackets[at], brackets[-1] // swap using a tuple
;`Char[=] MakeNew `Int+ len
;`Char[^] MakeNew `Int+ len // `Char[^] and `Char[=] are arrays that owns their items, like it's ususally in other langs;
;len & 1 ==0 ! // len must be even
// yet in assignment of `Char[^] to `Char[=] the allocated memory is moved from old to new owner, and old string becomes empty
;`Char[=] r(len)
;len & 1 ==0 ! // it's a call to postfix function '!' which is an assert: len must be even
;`Char[=] r(len) // allocate new string of length len
;For `Char[] eye = r; eye; ++eye
;*eye = '[' ;*++eye = '[' // fill r by "[][][]..."
;*++eye = '['
;Shuffle r
;Return r
;Bool AreBalanced `Char[] brackets
;`Int+ extra = 0;
;For; brackets; ++brackets
;Switch *brackets
'[' // it's a C++'s 'case': no either 'case' nor ':' are used; but the code for a case are in ala {} specifyed by tabs at line starts
'['
;++extra
']'
Line 4,591 ⟶ 4,596:
`Int Main(`Char[][])
;For `Int n=10; n; --n
// below note that new var is created inside args of func call
;`Char[=] brackets = MakeNew 10
;Print ;"%s : %s\n" ;`Char[=] brackets = MakeNew 10 ;(AreBalanced brackets) ? "Ok" : "bad"
;Return 0</lang>
</lang>
 
=={{header|zkl}}==
Cookies help us deliver our services. By using our services, you agree to our use of cookies.