Zsh Mailing List Archive
Messages sorted by: Reverse Date, Date, Thread, Author

# Re: Can you loop in math mode?

• X-seq: zsh-users 24156
• From: Stephane Chazelas <stephane.chazelas@xxxxxxxxx>
• To: Sebastian Gniazdowski <sgniazdowski@xxxxxxxxx>
• Subject: Re: Can you loop in math mode?
• Date: Mon, 19 Aug 2019 07:20:28 +0100
• Cc: Zsh Users <zsh-users@xxxxxxx>
• List-help: <mailto:zsh-users-help@zsh.org>
• List-id: Zsh Users List <zsh-users.zsh.org>
• List-post: <mailto:zsh-users@zsh.org>
• List-unsubscribe: <mailto:zsh-users-unsubscribe@zsh.org>
• Mail-followup-to: Sebastian Gniazdowski <sgniazdowski@xxxxxxxxx>, Zsh Users <zsh-users@xxxxxxx>
• Mailing-list: contact zsh-users-help@xxxxxxx; run by ezmlm
• References: <CAKc7PVDnSqZMpQz=8L5b+SijziN1aUYkm0C-Pv3FMSVy-mg_0Q__8285.33232431568\$1566173873\$gmane\$org@mail.gmail.com>

2019-08-19 02:16:20 +0200, Sebastian Gniazdowski:
[...]
> is some kind of a looping possible in math mode?
[...]

Can you maybe be more specific as to what you're trying to do?

You can use recursion:

\$ n=6 a='(n-->1 ? a*(n+1) : 1)' zsh -c 'echo \$((a))'
720

(6!)

Including via math functions, but within limits.

\$ n=0 a='(++n <= 255 ? a+2 : 1)' zsh -c 'echo \$((a))'
zsh:1: math recursion limit exceeded: (++n <= 255 ? a+2 : 1)

And you can of course have command substitutions inside an
arithmetic expression:

\$ echo \$(( 1 \$(for i ({2..6}) print "*\$i") ))
720

--
Stephane

Messages sorted by: Reverse Date, Date, Thread, Author