SML 中的组合(离散数学和函数式编程)
Composistion in SML (Discreate Math and Functional Programming)
我需要定义一个递归 ML 函数组合,它接受两个关系和 returns 这两个关系的组合。我需要在我的合成定义中使用 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));
然而,在尝试实现这两个功能时,我仍然坚持不懈。
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;
我需要定义一个递归 ML 函数组合,它接受两个关系和 returns 这两个关系的组合。我需要在我的合成定义中使用 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));
然而,在尝试实现这两个功能时,我仍然坚持不懈。
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;