我需要定義一個遞回 ML 函陣列合,它接受兩個關系并回傳這兩個關系的組合。我需要在我的組合定義中使用 thhisIsTheImageOf 和 createRelationFromImage 函式。
這是定義組合所需的代碼
fun thisIsTheImageOf(e,[])=[] |thisIsTheImageOf(e,(a,b)::xs) = if e=a then b::thisIsTheImageOf(e,xs) else thisIsTheImageOf(e,xs);
這是 thisIsTheImageOf 函式的資料型別、值和測驗輸入
datatype city =Vancouver|LosAngeles|Mexico|Minneapolis|Omaha |KansasCity|Denver|StLouis|Memphis|Chicago |NewOrleans|Cinncinati|Pittsburgh|Montreal|NewYork; datatype rivers =Missouri|Platte|NPlatte|SPlatte|Arkansas|Canadian |Kansas|Mississippi|Tennessee|Ohio|Allegheny|Monongahela; val isOnRiver=[(Denver,Platte),(Omaha,Missouri),(Omaha,Platte),(KansasCity,Missouri),(KansasCity,Kansas),(Minneapolis,Mississippi),(StLouis,Mississippi),(StLouis,Mi vssouri),(Memphis,Mississippi),(NewOrleans,Mississippi),(Cinncinati,Ohio),(Pittsburgh,Ohio),(Pittsburgh,Allegheny),(Pittsburgh,Monongahela)]; val flowsInto=[(Platte,Missouri),(Kansas,Missouri),(Missouri,Mississippi),(Allegheny,Ohio),(Monongahela,Ohio),(Tennessee,Ohio),(NPlatte,Platte),(SPlatte,Platte),(Ohio,Mississippi)]; thisIsTheImageOf(Pittsburgh, isOnRiver);thisIsTheImageOf(Mississippi, flowsInto);thisIsTheImageOf(Cinncinati, isOnRiver); fun createRelationFromImage(e,[])=[] createRelationFromImage(e,x::xs)= (e,x)::createRelationFromImage(e,xs);`` Here are tested inputs for the createRelationFromImage function
createRelationFromImage("Cincinnati",["New York", "Boston", "Dallas"]);
這兩個函式是作為一個單獨的函式創建的,但我應該使用這兩個函式來創建組合的遞回函式。
我知道數學上的組合函式,這是我為了幫助我了解我需要做的事情
fun composition( i, r)x=i(r(x));
但是,在嘗試實作這兩個功能時,我堅持要走得更遠。
uj5u.com熱心網友回復:
fun composition([],_ )=[]
| composition((a,b)::rest,relation)=
let
fun thisIsTheImageOf(e,[])=[]
|thisIsTheImageOf(e,(a,b)::xs) =
if e=a
then b::thisIsTheImageOf(e,xs)
else thisIsTheImageOf(e,xs);
fun createRelationFromImage(e,[])=[]
| createRelationFromImage(e,x::xs)= (e,x)::createRelationFromImage(e,xs);
in
createRelationFromImage(a, (thisIsTheImageOf(b, relation)))@ composition(rest, relation)
end;
轉載請註明出處,本文鏈接:https://www.uj5u.com/qiye/455707.html
上一篇:對稱關系遞回SML
