keyword - FFL: Where vs. Let

徘徊边缘 提交于 2020-01-15 06:37:10

问题


I was trying to understand the following code:

    def() ->commands
        if(deferred_passive_abilities != [],
           let [{ability: class passive_ability, creature: class creature}] items = [];
           let found = false;
           map(deferred_passive_abilities,
             if(cmd = null, add(items, [value]), [cmd, set(found, true)])
             where cmd = value.ability.static_effect(me, value.creature));

           if(found,
              set(deferred_passive_abilities, items);
              evaluate_deferred_passive_abilities(),
              set(deferred_passive_abilities, []))
        )

Haskell appears to have both let and where, but I didn't learn much by a superficial reading of their haskell docs. They also have a let...in, which I didn't understand but it would be good to know if FFL has that.

So, what is the significance of using let versus where? Was it necessary to use let here? (Also, possibly another question: why does it need those semicolons?)


回答1:


Using let introduces a variable that can be modified. Note how found and items are modified. By contrast, where always introduces immutable symbols.

Semi-colons are used in FFL to create a command pipeline. Normally in FFL, an entire formula is evaluated, resulting in a command or list of commands, and then the commands are executed.

When a semi-colon is present, everything before the semi-colon is treated as an entirely separate formula to everything after the semi-colon. The first formula is evaluated and executed and then the second formula is evaluated and executed.

Semi-colons effectively allow a much more procedural programming style in FFL, without semi-colons it is a purely functional language.




回答2:


Never knew of let in FFL before this, must be very rare.

Regardless of the insights, the semicolon has to be absolutely necessary, in order to force execution before using the bound variable. In other words, until used the semicolon, the variable does not exist. Does not have a bound value.

This is a big difference to where, which doesn't need of semicolons.

Given the semicolon is not a construction for complete beginners, I could somewhat recommend beginners about variables to stick in where until understanding the trickery of the semicolons.



来源:https://stackoverflow.com/questions/50457299/keyword-ffl-where-vs-let

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!