Zsh Mailing List Archive
Messages sorted by: Reverse Date, Date, Thread, Author

Re: Could someone clarify how math functions work?



I tried many variations on your example. None of them resulted in the
output you and I would have expected based on the documentation. I conclude
that either the documentation is wrong (at best confusing) or the
implementation is broken. This is yet another example for why I use any
language but a classic UNIX shell for any non-trivial behavior. This
problem isn't unique to zsh. It affects most UNIX command shells (e.g.,
ksh, csh, etc.) to some degree. I say this as a 50+ year old  grey-beard
who has mastered and forgotten more languages and command shells (including
non-UNIX varieties) than I can remember.

UNIX style command shells derived from the Bourne shell (which includes
zsh) have behaviors that are both difficult to document clearly and
understand. This is a consequence of the complex interaction between
parsing and evaluating statements. Not to mention changes in behavior based
on various "backward compatibility" options that can be set at run time (as
opposed to compile time). Not to mention those options were likely set far
removed from your shell script (e.g., the ~/.zshrc or ~/.zshenv files).

On Thu, Dec 18, 2014 at 7:45 PM, Eric Cook <llua@xxxxxxx> wrote:
>
> In the `functions -M' section of zshbuiltins there is the sentence:
> "The  result  of the last arithmetical expression evaluated inside the
> shell function (even if it is a form that normally only returns a
> status) gives the result of the mathematical function."
>
> but:
> zsh -c 'add() ( for arg; do (( n += arg )); done; print  n: $n );
> functions -M add; print results: $(( add(1,2,3) ))'
>
> Outputs:
> n: 6
> results: 3
>
> where as:
> zsh -c 'add() { local arg n; for arg; do (( n += arg )); done; print n:
> $n }; functions -M add; print results: $(( add(1,2,3) ))'
>
> Outputs:
> n: 6
> results: 6
>
> Is that expected behavior? If so, could you explain why?
>


-- 
Kurtis Rader
Caretaker of the exceptional canines Junior and Hank


Messages sorted by: Reverse Date, Date, Thread, Author