Useful alternative control structures?

后端 未结 28 916
礼貌的吻别
礼貌的吻别 2021-01-30 02:20

Sometimes when I am programming, I find that some particular control structure would be very useful to me, but is not directly available in my programming language. I think my

相关标签:
28条回答
  • 2021-01-30 03:11

    If you look at Haskell, although there is special syntax for various control structures, control flow is often captured by types. The most common kind of such control types are Monads, Arrows and applicative functors. So if you want a special type of control flow, it's usually some kind of higher-order function and either you can write it yourself or find one in Haskells package database (Hackage) wich is quite big.

    Such functions are usually in the Control namespace where you can find modules for parallel execution to errorhandling. Many of the control structures usually found in procedural languages have a function counterpart in Control.Monad, among these are loops and if statements. If-else is a keyworded expression in haskell, if without an else doesn't make sense in an expression, but perfect sense in a monad, so the if statements without an else is captured by the functions when and unless.

    Another common case is doing list operation in a more general context. Functional languages are quite fond of fold, and the Specialized versions like map and filter. If you have a monad then there is a natural extension of fold to it. This is called foldM, and therefor there are also extensions of any specialized version of fold you can think of, like mapM and filterM.

    0 讨论(0)
  • 2021-01-30 03:12
    {
        foo();
    } split_while( condition ) {
        bar();
    }
    

    You can accomplish that pretty easily using a regular while:

    while (true) {
        foo();
        if (!condition) break;
        bar();
    }
    

    I do that pretty frequently now that I got over my irrational distaste for break.

    0 讨论(0)
  • 2021-01-30 03:15
    if (cond)
       //do something
    else (cond)
       //do something
    else (cond)
       //do something
    first
       //do something
    then
       //do something
    else (cond)
       //do something
    else
       //do something
    end
    

    FIRST and THEN blocks runs if any of 3 conditionals are evaluated to true. FIRST block runs before the conditional block and THEN runs after the conditional block has ran.

    ELSE conditional or final write following FIRST and THEN statement are independent from these blocks.

    It can read as :

    if (cond)
       first()
       //do something
       then()
    else (cond)
       first()
       //do something
       then()
    else (cond)
       first()
       //do something
       then()
    else (cond)
       //do something
    else
       //do something
    end
    
    
    function first()
       //do something
    return
    function then()
       //do something
    return
    

    These functions are just a form to read. They wouldn't create scope. It's more like a gosub/return from Basic.

    Usefulness and readability as matter of discussion.

    0 讨论(0)
  • 2021-01-30 03:15
    for int i := 0 [down]to UpperBound() [step 2]
    

    Missing in every C-derived language.

    Please consider before you vote or write a comment:
    This is not redundant to for (int i = 0; i <= UpperBound(); i++), it has different semantics:

    1. UpperBound() is evaluated only once

    2. The case UpperBound() == MAX_INT does not produce an infinite loop

    0 讨论(0)
提交回复
热议问题