Vì không có ai khác đã đề cập đến nó, tương đương với Level[expr, 1]
xây dựng Yoda là sử dụng Apply để thay thế người đứng đầu của một biểu thức với List
:
In[1]:= expr = a + b - c + d + 4 e - 3 f;
In[2]:= List @@ expr
Level[expr, 1] == %
Out[2]= {a, b, -c, d, 4 e, -3 f}
Out[3]= True
In[4]:= expr2 = a^2 + 5 bc/ef - Sqrt[g - h] - Cos[i]/Sin[j + k];
In[5]:= List @@ expr2
Level[expr2, 1] == %
Out[5]= {a^2, (5 bc)/ef, -Sqrt[g - h], -Cos[i] Csc[j + k]}
Out[6]= True
Hai phương pháp cơ bản làm điều tương tự và có giống hệt nhau timings (sử dụng phiên bản của tôi về một average timing function)
In[1]:= SetOptions[TimeAv, Method -> {"MinNum", 80000}, "BlockSize" -> 20000];
In[7]:= List @@ expr // TimeAv
Total wall time is 0.244517, total cpu time is 0.13
and total time spent evaluating the expression is 0.13
The expression was evaluated 80000 times, in blocks of 20000 runs. This yields
a mean timing of 1.625*10^-6 with a blocked standard deviation of 2.16506*10^-7.
Out[7]= {1.625*10^-6, {a, b, -c, d, 4 e, -3 f}}
In[8]:= Level[expr, 1] // TimeAv
Total wall time is 0.336927, total cpu time is 0.16
and total time spent evaluating the expression is 0.16
The expression was evaluated 80000 times, in blocks of 20000 runs. This yields
a mean timing of 2.*10^-6 with a blocked standard deviation of 3.53553*10^-7.
Out[8]= {2.*10^-6, {a, b, -c, d, 4 e, -3 f}}
Một khả năng khác cho quy tắc là 'Thêm [a_, b___]:> {a, b}' – Simon
Nếu một ưu tiên 'Thay thế', nó có thể được kết hợp với' Áp dụng' cho một tùy chọn khác: 'Thay thế [a + b - c + d + 4 * e - 3 * f, a_Plus:> Danh sách @@ a] 'Hoặc, có thể:' Thay thế [a + b - c + d + 4 * e - 3 * f, Thêm -> Danh sách, 1 , Thủ trưởng -> Đúng] ' –