diff --git a/CHANGES.md b/CHANGES.md index cb98d170e9..36cca84bbf 100644 --- a/CHANGES.md +++ b/CHANGES.md @@ -4,6 +4,10 @@ Improve Monomial ordering. +### Bugs Fixed + +1. [\#2003](https://github.com/Mathics3/mathics-core/issues/2003) `Riffle[{a}, x]` gives `{a}`: a non-list separator is inserted only between elements + ## 10.0.1 April 18, 2026 diff --git a/mathics/builtin/import_export/importexport.py b/mathics/builtin/import_export/importexport.py index 3425fdb440..cb7e6f8825 100644 --- a/mathics/builtin/import_export/importexport.py +++ b/mathics/builtin/import_export/importexport.py @@ -778,15 +778,15 @@ class ExportString(Builtin): >> ExportString[{{1,2,3,4},{3},{2},{4}}, "CSV"] = 1,2,3,4 - . 3, - . 2, - . 4, + . 3 + . 2 + . 4 >> ExportString[{1,2,3,4}, "CSV"] - = 1, - . 2, - . 3, - . 4, + = 1 + . 2 + . 3 + . 4 >> ExportString[Integrate[f[x],{x,0,2}], "SVG"]//Head = String """ diff --git a/mathics/builtin/list/rearrange.py b/mathics/builtin/list/rearrange.py index 57522302a2..106be4b6f5 100644 --- a/mathics/builtin/list/rearrange.py +++ b/mathics/builtin/list/rearrange.py @@ -1176,7 +1176,13 @@ def eval(self, list, sep, evaluation: Evaluation): if sep.has_form(SymbolList, None): result = riffle_lists(list.elements, sep.elements) else: - result = riffle_lists(list.elements, [sep]) + # A non-list separator goes only between elements, never + # after the last one: Riffle[{a}, x] is {a}. + result = [] + for i, element in enumerate(list.elements): + if i > 0: + result.append(sep) + result.append(element) return list.restructure("List", result, evaluation, deps=(list, sep)) diff --git a/test/builtin/list/test_list.py b/test/builtin/list/test_list.py index 1dc3e2ce74..745f7eeb89 100644 --- a/test/builtin/list/test_list.py +++ b/test/builtin/list/test_list.py @@ -129,6 +129,17 @@ "{{{1, 1, 1}, {1, 1, 2}, {1, 2, 1}, {1, 2, 2}}, {{2, 1, 1}, {2, 1, 2}, {2, 2, 1}, {2, 2, 2}}}", None, ), + # Riffle + ( + "Riffle[{a}, x]", + None, + "{a}", + "non-list separator goes only between elements", + ), + ("Riffle[{a, b}, x]", None, "{a, x, b}", "Issue #2003"), + ("Riffle[{}, x]", None, "{}", "Degenerate Riffle on empty list"), + ("Riffle[{a}, {x}]", None, "{a, x}", "equal lengths: separator list appended"), + ("Riffle[{a, b}, {x, y}]", None, "{a, x, b, y}", None), # Union and Intersection ( "Union[{1, -1, 2}, {-2, 3}, SameTest -> (Abs[#1] == Abs[#2] &)]",