2
$\begingroup$

I am trying to use NestWhile with a custom test as follows:

k = {{Subscript[\[Lambda], s], {s, s}}, {Subscript[\[Lambda],x], {x, x}}};
SubscriptQ[x_] := (ToString[Head[x]] == "Subscript")
NestWhile[First, k, SubscriptQ[#] &]

But it does not work. I want it to keep taking the first element of the list until some Subscript appears. How can I make a test for this?

$\endgroup$
2
  • 6
    $\begingroup$ @user444, I would like to guide you to When should I edit posts? (namely "Tiny, trivial edits are discouraged"). Also, I don't think contractions are considered grammatical mistakes :) $\endgroup$
    – Domen
    Mar 1 at 15:29
  • 3
    $\begingroup$ @user444 - I have to agree with Domen here. Not a single one of your edits seems to improve the questions $\endgroup$
    – Jason B.
    Mar 1 at 15:49

1 Answer 1

5
$\begingroup$

NestWhile will stop when the condition is False. Therefore, you have to negate your condition!

Also, no need for ToString.

SubscriptQ[x_] := Head[x] === Subscript

k = {{Subscript[λ, s], {s, s}}, {Subscript[λ, x], {x, x}}};
NestWhile[First, k, Not@*SubscriptQ]
(* Subscript[λ, s] *)
$\endgroup$

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

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