I continue to study expl3
and I do not understand some behavior of user defined function, which returns an integer value (as I thought).
I want to use the integer number returned by \count_number_of_elements:n
for further manipulation.
My function \count_number_of_elements:n
work well and output correct number if I use it directly. But I need to assign its output to an integer variable. How can I did it?
MWE
A more or less real example: "Longest Collatz sequence" from Project Euler problem (but I want to avoid spoilers here)
\documentclass[12pt]{article}
\usepackage{xparse}
\ExplSyntaxOn
\int_new:N \l_initial_number_int
\cs_new:Nn \count_number_of_elements:n
{
\int_new:N \l_number_int
\int_set:Nn \l_number_int {1}
\int_set:Nn \l_initial_number_int {#1}
\int_do_while:nn
{\l_initial_number_int != 1}
{
\int_incr:N \l_number_int
\int_if_even:nTF {\l_initial_number_int}
{\int_set:Nn \l_initial_number_int {\l_initial_number_int/2} }
{\int_set:Nn \l_initial_number_int {3*\l_initial_number_int + 1} }
}
\int_use:N \l_number_int
}
\NewDocumentCommand{\MaxNumber}{m}
{
% Code for defining starting number, produces the longest chain. According to my idea, this code should use integer number produced by \count_number_of_elements:n
% say I need get output of \count_number_of_elements:n
\int_set:Nn \l_tmpa_int {\count_number_of_elements:n {#1}}
}
\ExplSyntaxOff
\begin{document}
\end{document}
\int_eval:n
is already added to the second argument so\int_add:Nn \l_tmpa_int { 2 + 3 }
works (you don't need to use\int_eval:n
). – Manuel May 30 '17 at 18:36#1
as the contents of\my_function:n
and it would be expandable :) Add a real example case. But it might not be possible to give a general answer unless you clarify what you want to do. – Manuel May 30 '17 at 18:48\int_set:Nn
in your\my_function:n
prevents expansion. An assignment is not expandable, so\int_add:Nn \l_tmpa_int {\my_function:n{#1}}
in\Number
will fail – user31729 May 30 '17 at 18:48\int_use:N \l_number_int
from\count_number_of_elements:n
and edit\MaxNumber
to be\count_number_of_elements:n {#1} \int_set_eq:NN \l_tmpa_int \l_number_int
and you are done. – Manuel May 30 '17 at 20:00