Sml 什么是嵌套模式?
什么是嵌套模式?我不明白为什么下面有嵌套模式:Sml 什么是嵌套模式?,sml,smlnj,Sml,Smlnj,什么是嵌套模式?我不明白为什么下面有嵌套模式: exception BadTriple fun zip3 list_triple = case list_triple of ([],[],[]) => [] | (hd1::tl1,hd2::tl2,hd3::tl3) => (hd1,hd2,hd3)::zip3(tl1,tl2,tl3) | _ => raise BadTriple fun unzip3 lst =
exception BadTriple
fun zip3 list_triple =
case list_triple of
([],[],[]) => []
| (hd1::tl1,hd2::tl2,hd3::tl3) => (hd1,hd2,hd3)::zip3(tl1,tl2,tl3)
| _ => raise BadTriple
fun unzip3 lst =
case lst of
[] => ([],[],[])
| (a,b,c)::tl => let val (l1,l2,l3) = unzip3 tl
in
(a::l1,b::l2,c::l3)
end
另外,我无法理解嵌套模式和嵌套大小写表达式之间有什么不同,我能举一些例子吗?嵌套模式是一种包含其他非平凡模式的模式(其中“非平凡”指的是“不是变量或通配符模式”)
([],[],[])
是一种嵌套模式,因为(p1,p2,p3)
是一种模式(匹配元组),[]
也是一种模式(匹配空列表),在这里应用于三元组的元素。同样地,(hd1::tl1,hd2::tl2,hd3::tl3)
也是嵌套模式,因为(p1,p2,p3)
是模式,hd1::tl1
,hd2::tl2
和hd3::tl3
也是模式(匹配非空列表)
在没有嵌套模式的情况下表达相同内容的一种方法是将内部模式移动到它们自己的case表达式中(尽管我不建议这样做,因为正如您所看到的,这会使代码变得更加复杂):
这是一个嵌套的case表达式,因为我们有一个包含其他case表达式的case表达式。具有嵌套模式的版本不是嵌套的大小写表达式,因为只有一个大小写表达式,彼此之间没有多个。嵌套模式是包含其他非平凡模式的模式(其中“非平凡”指的是“不是变量或通配符模式”)
([],[],[])
是一种嵌套模式,因为(p1,p2,p3)
是一种模式(匹配元组),[]
也是一种模式(匹配空列表),在这里应用于三元组的元素。同样地,(hd1::tl1,hd2::tl2,hd3::tl3)
也是嵌套模式,因为(p1,p2,p3)
是模式,hd1::tl1
,hd2::tl2
和hd3::tl3
也是模式(匹配非空列表)
在没有嵌套模式的情况下表达相同内容的一种方法是将内部模式移动到它们自己的case表达式中(尽管我不建议这样做,因为正如您所看到的,这会使代码变得更加复杂):
这是一个嵌套的case表达式,因为我们有一个包含其他case表达式的case表达式。带有嵌套模式的版本不是嵌套的case表达式,因为只有一个case表达式,彼此之间没有多个case表达式。如果我们将函数分解为curry样式,可能会有所帮助, 没有嵌套模式, 在下面的foo函数中,只有3个(非嵌套)模式
fun foo [] [] [] = []
| foo (hd1::tl1) (hd2::tl2) (hd3::tl3) = (hd1, hd2, hd3)::zip3(tl1, tl2, tl3)
| foo _ _ _ = raise BadTriple
and zip3 (l1, l2, l3) = foo l1 l2 l3
当我们使用as关键字时,很容易看到每个单独的模式,
在每个as的右侧是一个图案
fun zip3 (l1 as [], l2 as [], l3 as []) = []
| zip3 (l1 as hd1::tl1, l2 as hd2::tl2, l3 as hd3::tl3) = (hd1, hd2, hd3)::zip3(tl1, tl2, tl3)
| zip3 _ = raise BadTriple
fun zip3 (list_triple as (l1 as [], l2 as [], l3 as [])) = []
| zip3 (list_triple as (l1 as hd1::tl1, l2 as hd2::tl2, l3 as hd3::tl3)) = (hd1, hd2, hd3)::zip3(tl1, tl2, tl3)
那么,我们为什么要考虑嵌套?< /P> 我们可以为初始参数添加as列表\u triple, 看到我们实际上在一个模式中有模式
fun zip3 (l1 as [], l2 as [], l3 as []) = []
| zip3 (l1 as hd1::tl1, l2 as hd2::tl2, l3 as hd3::tl3) = (hd1, hd2, hd3)::zip3(tl1, tl2, tl3)
| zip3 _ = raise BadTriple
fun zip3 (list_triple as (l1 as [], l2 as [], l3 as [])) = []
| zip3 (list_triple as (l1 as hd1::tl1, l2 as hd2::tl2, l3 as hd3::tl3)) = (hd1, hd2, hd3)::zip3(tl1, tl2, tl3)
如果没有as和未使用的变量,它看起来会更好
fun zip3 ([], [], []) = []
| zip3 (hd1::tl1, hd2::tl2, hd3::tl3) = (hd1, hd2, hd3)::zip3(tl1, tl2, tl3)
| zip3 _ = raise BadTriple
如果我们把功能分解成咖喱风格也许会有帮助, 没有嵌套模式, 在下面的foo函数中,只有3个(非嵌套)模式
fun foo [] [] [] = []
| foo (hd1::tl1) (hd2::tl2) (hd3::tl3) = (hd1, hd2, hd3)::zip3(tl1, tl2, tl3)
| foo _ _ _ = raise BadTriple
and zip3 (l1, l2, l3) = foo l1 l2 l3
当我们使用as关键字时,很容易看到每个单独的模式,
在每个as的右侧是一个图案
fun zip3 (l1 as [], l2 as [], l3 as []) = []
| zip3 (l1 as hd1::tl1, l2 as hd2::tl2, l3 as hd3::tl3) = (hd1, hd2, hd3)::zip3(tl1, tl2, tl3)
| zip3 _ = raise BadTriple
fun zip3 (list_triple as (l1 as [], l2 as [], l3 as [])) = []
| zip3 (list_triple as (l1 as hd1::tl1, l2 as hd2::tl2, l3 as hd3::tl3)) = (hd1, hd2, hd3)::zip3(tl1, tl2, tl3)
那么,我们为什么要考虑嵌套?< /P> 我们可以为初始参数添加as列表\u triple, 看到我们实际上在一个模式中有模式
fun zip3 (l1 as [], l2 as [], l3 as []) = []
| zip3 (l1 as hd1::tl1, l2 as hd2::tl2, l3 as hd3::tl3) = (hd1, hd2, hd3)::zip3(tl1, tl2, tl3)
| zip3 _ = raise BadTriple
fun zip3 (list_triple as (l1 as [], l2 as [], l3 as [])) = []
| zip3 (list_triple as (l1 as hd1::tl1, l2 as hd2::tl2, l3 as hd3::tl3)) = (hd1, hd2, hd3)::zip3(tl1, tl2, tl3)
如果没有as和未使用的变量,它看起来会更好
fun zip3 ([], [], []) = []
| zip3 (hd1::tl1, hd2::tl2, hd3::tl3) = (hd1, hd2, hd3)::zip3(tl1, tl2, tl3)
| zip3 _ = raise BadTriple
什么是嵌套模式
模式中的模式是嵌套模式
我不明白为什么是嵌套模式
- 这里的模式:
(列表1、列表2、列表3)
- 这里的嵌套模式:
,list1->hd1::tl1
,list2->hd2::tl2
list3->hd3::tl3
- 这里的模式:
tuple::t1
- 这里的嵌套模式:
tuple->(a,b,c)
它们是两种不同的东西。上面已经解释了嵌套模式。对于嵌套大小写表达式: 什么是嵌套模式 模式中的模式是嵌套模式
我不明白为什么是嵌套模式
- 这里的模式:
(列表1、列表2、列表3)
- 这里的嵌套模式:
,list1->hd1::tl1
,list2->hd2::tl2
list3->hd3::tl3
- 这里的模式:
tuple::t1
- 这里的嵌套模式:
tuple->(a,b,c)
它们是两种不同的东西。上面已经解释了嵌套模式。对于嵌套大小写表达式: