我正在處理 Maple 中的一個問題。但是我的問題很籠統,可以根據任何編程語言來回答。
問題:
- 僅使用地圖功能(無回圈)。假設有兩個串列 A 和 B,其中包含 n 和 m 個元素。用“真”替換 A 中的那些元素,這些元素也在 B 中找到,并用“假”替換 A 中的其他元素。
問題:我確實嘗試像這樣解決這個問題:
funk := proc(i::integer,j::integer) # checks if two elements are identical
if (i = j) then
return true;
else
return false;
end if;
end proc:
funktion := proc(int::integer, L :: list) # calls funk on every element of L.
map(funk, L, int);
end proc:
SchnittTrueA := proc(M::list, L::list) # calls funktion on every Element of M and L.
map(funktion,M,L);
end proc:
如果我跑SchnittTrueA(k,l);k :=[1,3] 和 l := [2,3] 我得到這個結果:
[[false, false], [false, true]]
這絕不是我想要該程式做的事情!
我需要這樣的東西 [false, true]
我確實盡我所能,但我不知道如何解決這個問題。
P.S1:我是一個新手程式員,所以請不要對我太苛刻:DPS2:如果您想給我任何提示/答案,只要您可以使用地圖功能,它就不需要是Maple語言。
uj5u.com熱心網友回復:
除了map. 例如。您的用戶定義程序使用if..then等。
但以下不使用回圈。
前兩個是相當有效的,因為對于tlist中的每個元素,A只要true在 list 中找到任何匹配的元素,它們就會生成B。
相比之下,后兩種方法效率低下,因為它們針對t串列A中的每個元素測驗串列中的每個元素B(即使已經找到匹配項)。
restart;
A := [$1..10];
[1, 2, 3, 4, 5, 6, 7, 8, 9, 10]
B := [seq(ithprime(i),i=1..4)];
[2, 3, 5, 7]
map(member,A,B);
[false, true, true, false, true, false, true, false, false, false]
map(t->ormap(evalb@`=`,B,t),A);
[false, true, true, false, true, false, true, false, false, false]
map(t->`or`(op(map(evalb@`=`,B,t))),A);
[false, true, true, false, true, false, true, false, false, false]
map(t1->`if`(map(t2->`if`(t2=t1,true,NULL),B)=[true],
true,false),A);
[false, true, true, false, true, false, true, false, false, false]
在上面顯示的四種方法中,第四種在精神上與您的嘗試最接近。
我將展示一些與上面第四個等價的東西,每個都更接近你最初的嘗試。
restart;
A := [$1..10]:
B := [seq(ithprime(i),i=1..4)]:
這些等同物的使用那些相同A和B示例串列。
func := (t1,t2)->`if`(t2=t1,true,NULL):
funktion := (t1,B)->`if`(map(func,B,t1)=[true],
true,false):
map(funktion, A, B);
[false, true, true, false, true, false, true, false, false, false]
現在使用proc而不是更短的運算子語法。
func := proc(t1,t2)
`if`(t2=t1,true,NULL);
end proc:
funktion := proc(t1,B)
`if`(map(func,B,t1)=[true],
true,false);
end proc:
map(funktion, A, B);
[false, true, true, false, true, false, true, false, false, false]
現在使用長形式的if..then, 而不是較短的if運算子形式。
func := proc(t1,t2)
if t2=t1 then
true;
else
NULL;
end if;
end proc:
funktion := proc(t1,B)
if map(func,B,t1)=[true] then
true;
else
false;
end if;
end proc:
map(funktion, A, B);
[false, true, true, false, true, false, true, false, false, false]
轉載請註明出處,本文鏈接:https://www.uj5u.com/ruanti/327740.html
上一篇:Python可逆函式
