1
$\begingroup$

Having a function resolving into a list:

IntegerDigits[19835193751, 2^8, 8]
> {0,0,0,4,158,69,9,151}

Its result can be indexed fine like this:

IntegerDigits[19835193751, 2^8, 8][[5]]
> 158

But when symbolics are involved, all hell breaks loose:

IntegerDigits[q, 2^8, 8][[1]]
> q

IntegerDigits[q, 2^8, 8][[2]]
> 256

IntegerDigits[q, 2^8, 8][[3]]
> 8

IntegerDigits[q, 2^8, 8][[0]]
> IntegerDigits

I understand what happens and why, but how could I explain to the program that I want the results indexed, not an object which holds the function and its arguments?

Added: I suppose it boils down to defining a resolution order for functions - Part should stay deferred until its argument becomes a List. Still figuring out an implementation though.

$\endgroup$
  • 1
    $\begingroup$ Look up Indexed $\endgroup$ – Carl Woll Apr 16 '18 at 23:28
  • $\begingroup$ @CarlWoll good point, but this feature is relatively new, older versions don't have it. $\endgroup$ – ogurets Apr 16 '18 at 23:40
3
$\begingroup$

In M10+ you can use Indexed. In versions before M10, you could do:

indexed[l_List, n_Integer?NonNegative] := l[[n]]

For example:

expr = indexed[IntegerDigits[q, 256, 8], 5]

expr /. q->13943312222

indexed[IntegerDigits[q, 256, 8], 5]

63

| improve this answer | |
$\endgroup$

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.