From mboxrd@z Thu Jan 1 00:00:00 1970 Return-Path: Received: (qmail 20566 invoked by alias); 19 Dec 2014 05:48:18 -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: 19592 Received: (qmail 21762 invoked from network); 19 Dec 2014 05:48:16 -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=o6O8Yq6RF9zTy9lr4nId1aefWepQEW4gT+BMHGGTKvA=; b=PTF7Aw4dXxcLicqwt0Ao/LwedlBfNEBORIswprHBoZoSa11nNn/LzHHVxtO1aZMExE H9VrLta+uFIQU3EHP03uSEGb1swB0kgOvOKLjY/KvQnfLBDoprPMyqiKbVbZOlhXmk+k pya2ZtOGRXeIsfzCrGtqAw13IAIS58J3mpC7WvffPHekXBLOLNylIIRZo4C0ILqha3Wk qAcstNJIbZZkYAgTWh45NGTrb6IxGqEsOiKr9cPEbBpPw2rg1I0VgEXI85Fan4QUSYYD FjGyLLBcxmH8cCcgfGYT2uZ/EhmH26BCWkAWV/FZwvuVRUPdw4FcQKK2IYB1S2CxB6+6 +XAg== X-Gm-Message-State: ALoCoQnpr5jYK/ZB9D6aPZfDVwkp9j8qWpYS9kwOFGRG2qlgKdC2W+BGmMI+jCXLg38bwwsflV9k MIME-Version: 1.0 X-Received: by 10.112.162.226 with SMTP id yd2mr5963256lbb.1.1418968094916; Thu, 18 Dec 2014 21:48:14 -0800 (PST) In-Reply-To: <141218213450.ZM28822@torch.brasslantern.com> References: <54939F50.50102@gmx.com> <141218213450.ZM28822@torch.brasslantern.com> Date: Thu, 18 Dec 2014 21:48:14 -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=089e0112c86c3edbc9050a8b410a --089e0112c86c3edbc9050a8b410a Content-Type: text/plain; charset=UTF-8 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 --089e0112c86c3edbc9050a8b410a--