Jump to content

Conditional structures: Difference between revisions

add bqn
(Realize in F#)
(add bqn)
Line 1,550:
return "even";
</lang>
 
=={{header|BQN}}==
The basic method of control flow in BQN is implemented using first-class functions and Choose (<code>◶</code>). Using Choose, we can implement some basic control structures:
<lang bqn>If ← {𝕏⍟𝕎@}´ # Also Repeat
IfElse ← {c‿T‿F: c◶F‿T@}
While ← {𝕩{𝔽⍟𝔾∘𝔽_𝕣_𝔾∘𝔽⍟𝔾𝕩}𝕨@}´ # While 1‿{... to run forever
DoWhile ← {𝕏@ ⋄ While 𝕨‿𝕩}´
For ← {I‿C‿P‿A: I@ ⋄ While⟨C,P∘A⟩}
 
# Switch/case statements have many variations; these are a few
Match ← {𝕏𝕨}´
Select ← {(⊑𝕩)◶(1↓𝕩)@}
Switch ← {c←⊑𝕩 ⋄ m‿a←<˘⍉∘‿2⥊1↓𝕩 ⋄ (⊑a⊐C)◶m@}
Test ← {fn←{C‿A𝕊e:C◶A‿E}´𝕩⋄Fn@}</lang>
 
The other method of branching is using function predicates, which can be used in any blocks for an if-else like conditional:
<lang bqn>{
a<b ? a+↩1 ; # If
a<c ? c-↩1 ; # Else If
a-↩2 # Else
}</lang>
 
However, they act like any other block header, so the variables defined in each predicate segment do not exist in their else and else if condition. Block Headers in general provide a rudimentary form of control flow (checking for exact matches and wildcards), but these are much more constrained than a general conditional.
 
'''Source:''' [https://mlochbaum.github.io/BQN/doc/control.html Control Flow in BQN]
 
=={{header|Bracmat}}==
236

edits

Cookies help us deliver our services. By using our services, you agree to our use of cookies.