From mboxrd@z Thu Jan 1 00:00:00 1970 Return-Path: Received: (qmail 27937 invoked by alias); 19 Dec 2014 05:51:22 -0000 Mailing-List: contact zsh-users-help@zsh.org; run by ezmlm Precedence: bulk X-No-Archive: yes List-Id: Zsh Users List List-Post: List-Help: X-Seq: 19594 Received: (qmail 21841 invoked from network); 19 Dec 2014 05:51:20 -0000 X-Spam-Checker-Version: SpamAssassin 3.3.2 (2011-06-06) on f.primenet.com.au X-Spam-Level: X-Spam-Status: No, score=-2.6 required=5.0 tests=BAYES_00,HTML_MESSAGE, RCVD_IN_DNSWL_LOW autolearn=ham version=3.3.2 X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20130820; h=x-gm-message-state:mime-version:in-reply-to:references:date :message-id:subject:from:to:cc:content-type; bh=Z/t6WzXU23PUMZLEed+Zoru+0gIHnnO+cdfjrnOVGGI=; b=JQqPJifCfCsR1cgjd8Z9aMck6Vnxg8k9/j0C1E8Z1XeCwhNVGOGcr9LDkW8P0JmoVR WZ6kKLdnmxnrSBXExMlyPx0y/VRCN0zg8qNCg7Ud4vekrZ7VXiriBAdMo9gM6AJ0hkvK HEnGygr4tS6f2OCRncBEZXzDbD1qvoxRHuQZ96LDB9pcd8n8xxx31NFCDb9YU/cmaETX QOJnRf9OmcALYH36UNiXIrkib4wTzNVYotwX2bplcsMdXMYFKZeAYqrmEwM0pfFzY644 G3sZHeNObMSOi7z/I62Id9Z1zuZ6CxT0RRMRFORbGeqOSvz0E7tfr9V8zeCw0WJU0GEV RVKA== X-Gm-Message-State: ALoCoQm/Mu1o2LSajXZB0fpAfpMxlUfrJnXUd8bUEdSoHoTbLfI4sQEQV1en7AkUb/sjJQz/Amo4 MIME-Version: 1.0 X-Received: by 10.152.29.129 with SMTP id k1mr6090462lah.10.1418968278058; Thu, 18 Dec 2014 21:51:18 -0800 (PST) In-Reply-To: References: <54939F50.50102@gmx.com> <141218213450.ZM28822@torch.brasslantern.com> Date: Thu, 18 Dec 2014 21:51:18 -0800 Message-ID: Subject: Re: Could someone clarify how math functions work? From: Kurtis Rader To: Bart Schaefer Cc: Zsh Users Content-Type: multipart/alternative; boundary=089e0160b7da295e2e050a8b4c83 --089e0160b7da295e2e050a8b4c83 Content-Type: text/plain; charset=UTF-8 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 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 > 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 --089e0160b7da295e2e050a8b4c83--