zsh-users
 help / color / mirror / code / Atom feed
* Could someone clarify how math functions work?
@ 2014-12-19  3:45 Eric Cook
  2014-12-19  5:16 ` Kurtis Rader
                   ` (2 more replies)
  0 siblings, 3 replies; 8+ messages in thread
From: Eric Cook @ 2014-12-19  3:45 UTC (permalink / raw)
  To: zsh-users

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?


^ permalink raw reply	[flat|nested] 8+ messages in thread

* Re: Could someone clarify how math functions work?
  2014-12-19  3:45 Could someone clarify how math functions work? Eric Cook
@ 2014-12-19  5:16 ` Kurtis Rader
  2014-12-19  5:34 ` Bart Schaefer
  2014-12-19  9:35 ` Peter Stephenson
  2 siblings, 0 replies; 8+ messages in thread
From: Kurtis Rader @ 2014-12-19  5:16 UTC (permalink / raw)
  To: Eric Cook; +Cc: Zsh Users

[-- 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

^ permalink raw reply	[flat|nested] 8+ messages in thread

* Re: Could someone clarify how math functions work?
  2014-12-19  3:45 Could someone clarify how math functions work? 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 11:15   ` Eric Cook
  2014-12-19  9:35 ` Peter Stephenson
  2 siblings, 2 replies; 8+ messages in thread
From: Bart Schaefer @ 2014-12-19  5:34 UTC (permalink / raw)
  To: zsh-users

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.


^ permalink raw reply	[flat|nested] 8+ messages in thread

* Re: Could someone clarify how math functions work?
  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
  1 sibling, 2 replies; 8+ messages in thread
From: Kurtis Rader @ 2014-12-19  5:48 UTC (permalink / raw)
  To: Bart Schaefer; +Cc: Zsh Users

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

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

^ permalink raw reply	[flat|nested] 8+ messages in thread

* Re: Could someone clarify how math functions work?
  2014-12-19  5:48   ` Kurtis Rader
@ 2014-12-19  5:51     ` Kurtis Rader
  2014-12-19  6:05     ` Bart Schaefer
  1 sibling, 0 replies; 8+ messages in thread
From: Kurtis Rader @ 2014-12-19  5:51 UTC (permalink / raw)
  To: Bart Schaefer; +Cc: Zsh Users

[-- 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

^ permalink raw reply	[flat|nested] 8+ messages in thread

* Re: Could someone clarify how math functions work?
  2014-12-19  5:48   ` Kurtis Rader
  2014-12-19  5:51     ` Kurtis Rader
@ 2014-12-19  6:05     ` Bart Schaefer
  1 sibling, 0 replies; 8+ messages in thread
From: Bart Schaefer @ 2014-12-19  6:05 UTC (permalink / raw)
  To: Zsh Users

On Dec 18,  9:48pm, 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) ))'

That IS what my second example is; look again at the text you excerpted
(not copied again here).  By the way, though I personally don't care
much any more after too many years of arguing, we try to discourage
"top posting" on these lists.

} I would argue that your explanation, while correct, illustrates my point.

No disagreement, but what is the point of making that point, again?
Jut to tell Eric to go program in something else?

-- 
Barton E. Schaefer


^ permalink raw reply	[flat|nested] 8+ messages in thread

* Re: Could someone clarify how math functions work?
  2014-12-19  3:45 Could someone clarify how math functions work? Eric Cook
  2014-12-19  5:16 ` Kurtis Rader
  2014-12-19  5:34 ` Bart Schaefer
@ 2014-12-19  9:35 ` Peter Stephenson
  2 siblings, 0 replies; 8+ messages in thread
From: Peter Stephenson @ 2014-12-19  9:35 UTC (permalink / raw)
  To: zsh-users

On Thu, 18 Dec 2014 22:45:20 -0500
Eric Cook <llua@gmx.com> wrote:
> 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

This is all sorted out --- the ( ... ) runs in a subshell and the value
the math function has access to is the last value in the main shell ---
but I'm scratching my head at how we explain this sort of thing better.
It's just the kind of problem Ray has been banging his head against and
why Kurtis, correctly in my opinion, suggests something other than a
shell for a lot of programming tasks.

I'm wondering, however, if we can do a bit better by strengthening the
warnings against variant syntax --- whose sole role appears to be to
confuse the user --- and provide some kind of good practice guide for
complex syntax such as functions, loops, etc.  But would anyone read it?
One of the problems with the documentation is there's too much of it,
measured by volume of text rather than by desired effect.  Sigh.

pws


^ permalink raw reply	[flat|nested] 8+ messages in thread

* Re: Could someone clarify how math functions work?
  2014-12-19  5:34 ` Bart Schaefer
  2014-12-19  5:48   ` Kurtis Rader
@ 2014-12-19 11:15   ` Eric Cook
  1 sibling, 0 replies; 8+ messages in thread
From: Eric Cook @ 2014-12-19 11:15 UTC (permalink / raw)
  To: zsh-users

On 12/19/2014 12:34 AM, Bart Schaefer wrote:
>
> 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.
Ah, It was indeed obvious to me that it was due to the subshell function
definition. but i wasn't aware of processing the positional parameters
being an arithmetic evaluation, which left me lost as to why the last
argument was the returned value.

Thank you


^ permalink raw reply	[flat|nested] 8+ messages in thread

end of thread, other threads:[~2014-12-19 11:15 UTC | newest]

Thread overview: 8+ messages (download: mbox.gz / follow: Atom feed)
-- links below jump to the message on this page --
2014-12-19  3:45 Could someone clarify how math functions work? 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
2014-12-19  6:05     ` Bart Schaefer
2014-12-19 11:15   ` Eric Cook
2014-12-19  9:35 ` Peter Stephenson

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).