8
$\begingroup$

Why does ToRules return a Sequence expression? Why doesn't it return directly what {ToRules[...]} does now? In what application is having Sequence beneficial here?

Now we have to do x /. {ToRules[...]} instead of x /. ToRules[...]. I would have thought that this is the standard and most common application.

Examples:

ToRules[x == 1 || x == 2]
(* Sequence[{x -> 1}, {x -> 2}] *)

ToRules[x == 1]
(* {x -> 1} *)

ToRules[False]
(* Sequence[] *)

ToRules[True]
(* {} *)
$\endgroup$
3
  • $\begingroup$ Reduce`ReduceToRules returns a list. $\endgroup$
    – Greg Hurst
    Commented Jan 21, 2015 at 20:33
  • 5
    $\begingroup$ Sometimes it's nice to do something like ToRules /@ solutions. That's the only time I can recall liking the Sequence return value. $\endgroup$
    – Greg Hurst
    Commented Jan 21, 2015 at 20:43
  • 3
    $\begingroup$ Another value of Sequence is to insert several rules into a list without increasing the dimension of the list (avoiding the need for Flatten): mySeq = Sequence[c -> d, e -> f]; {a -> b, mySeq, c -> d} yields {a -> b, c -> d, e -> f, c -> d}. This can be quite useful. $\endgroup$ Commented Jan 22, 2015 at 0:46

2 Answers 2

3
$\begingroup$

It is very old function. It was introduced in V1.0 in 1988. It was used in the following way:

enter image description here

So Sequence header was very useful. Ref (Section 3.4.2).

$\endgroup$
1
$\begingroup$

I suspect this is a way of letting you know that ToRules doesn't have an appropriate head to use for the conversion. Or put another way when a head isn't supplied Sequence is used by default. Consider:

Delete[{1, 2}, 0]

{1, 2} /. _[x__] :> x
Sequence[1, 2]

Sequence[1, 2]

ToRules converts And to List:

ToRules[a == 1 && b == 2 && c == 3]
{a -> 1, b -> 2, c -> 3}

Perhaps it doesn't make sense to equate List with Or as well?

$\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.