zsh-users
 help / color / mirror / code / Atom feed
From: Kurtis Rader <krader@skepticism.us>
To: Eric Cook <llua@gmx.com>
Cc: Zsh Users <zsh-users@zsh.org>
Subject: Re: Could someone clarify how math functions work?
Date: Thu, 18 Dec 2014 21:16:41 -0800	[thread overview]
Message-ID: <CABx2=D9fAePDZmJsi6zHHoPHHfCYwUqNxWvKeU5+H1bWMGmUZw@mail.gmail.com> (raw)
In-Reply-To: <54939F50.50102@gmx.com>

[-- Attachment #1: Type: text/plain, Size: 1941 bytes --]

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@gmx.com> 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

  reply	other threads:[~2014-12-19  5:17 UTC|newest]

Thread overview: 8+ messages / expand[flat|nested]  mbox.gz  Atom feed  top
2014-12-19  3:45 Eric Cook
2014-12-19  5:16 ` Kurtis Rader [this message]
2014-12-19  5:34 ` Bart Schaefer
2014-12-19  5:48   ` Kurtis Rader
2014-12-19  5:51     ` Kurtis Rader
2014-12-19  6:05     ` Bart Schaefer
2014-12-19 11:15   ` Eric Cook
2014-12-19  9:35 ` Peter Stephenson

Reply instructions:

You may reply publicly to this message via plain-text email
using any one of the following methods:

* Save the following mbox file, import it into your mail client,
  and reply-to-all from there: mbox

  Avoid top-posting and favor interleaved quoting:
  https://en.wikipedia.org/wiki/Posting_style#Interleaved_style

* Reply using the --to, --cc, and --in-reply-to
  switches of git-send-email(1):

  git send-email \
    --in-reply-to='CABx2=D9fAePDZmJsi6zHHoPHHfCYwUqNxWvKeU5+H1bWMGmUZw@mail.gmail.com' \
    --to=krader@skepticism.us \
    --cc=llua@gmx.com \
    --cc=zsh-users@zsh.org \
    /path/to/YOUR_REPLY

  https://kernel.org/pub/software/scm/git/docs/git-send-email.html

* If your mail client supports setting the In-Reply-To header
  via mailto: links, try the mailto: link
Be sure your reply has a Subject: header at the top and a blank line before the message body.
Code repositories for project(s) associated with this public inbox

	https://git.vuxu.org/mirror/zsh/

This is a public inbox, see mirroring instructions
for how to clone and mirror all data and code used for this inbox;
as well as URLs for NNTP newsgroup(s).