zsh-users
 help / color / mirror / code / Atom feed
From: Kurtis Rader <krader@skepticism.us>
To: Bart Schaefer <schaefer@brasslantern.com>
Cc: Zsh Users <zsh-users@zsh.org>
Subject: Re: Could someone clarify how math functions work?
Date: Thu, 18 Dec 2014 21:51:18 -0800	[thread overview]
Message-ID: <CABx2=D-OWdTMEdpC0tASNNYs+LSs8LSOqmeHmyS4-XwZDABRFA@mail.gmail.com> (raw)
In-Reply-To: <CABx2=D90SO9+hA0O9EK-exz60V2vQAxZGbhNZK-_0H=p_6igyA@mail.gmail.com>

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

To clarify the "lather, rinse, repeat" part of my reply:
http://en.wikipedia.org/wiki/Lather,_rinse,_repeat



On Thu, Dec 18, 2014 at 9:48 PM, Kurtis Rader <krader@skepticism.us> wrote:
>
> Your second example is identical to the first so you apparently did what I
> often do: forgot to make a copy of the desired text. That is, your second
> example should be
>
> zsh -c 'add() { for arg; do (( n += arg )); done; print  n: $n };
> functions -M add; print results: $(( add(3,1,2) ))'
>
>
> I would argue that your explanation, while correct, illustrates my point.
> Namely, that the subtleties involving the parsing, execution,
> interpolation, lather, rinse, repeat sequence of any Bourne shell
> derivative is inherently problematic.
>
> On Thu, Dec 18, 2014 at 9:34 PM, Bart Schaefer <schaefer@brasslantern.com>
> wrote:
>>
>> On Dec 18, 10:45pm, Eric Cook wrote:
>> } Subject: Could someone clarify how math functions work?
>> }
>> } 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?
>>
>> The following might illustrate:
>>
>> zsh -c 'add() ( for arg; do (( n += arg )); done; print  n: $n );
>> functions -M add; print results: $(( add(3,2,1) ))'
>>
>> zsh -c 'add() ( for arg; do (( n += arg )); done; print  n: $n );
>> functions -M add; print results: $(( add(3,1,2) ))'
>>
>>
>> When you define add() with parens ( ) around the function body, you
>> are running the function body in a subshell.  The "last arithmetical
>> expression evaluated" IN THE CURRENT SHELL is the processing of the
>> argument list of the call to add(), which is done left-to-right and
>> is therefore "3" in the original example.
>>
>> When you define add() with braces { } you are running the function
>> body in in the current shell, so the last expression is the last
>> assignment in the for-loop body.
>>
>
>
> --
> Kurtis Rader
> Caretaker of the exceptional canines Junior and Hank
>


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

  reply	other threads:[~2014-12-19  5:51 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
2014-12-19  5:34 ` Bart Schaefer
2014-12-19  5:48   ` Kurtis Rader
2014-12-19  5:51     ` Kurtis Rader [this message]
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=D-OWdTMEdpC0tASNNYs+LSs8LSOqmeHmyS4-XwZDABRFA@mail.gmail.com' \
    --to=krader@skepticism.us \
    --cc=schaefer@brasslantern.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).