List 如果我在F中有一个自定义定义的列表类型,那么如何获取该类型列表的开头和结尾
我在F中有一个自定义的列表,例如:List 如果我在F中有一个自定义定义的列表类型,那么如何获取该类型列表的开头和结尾,list,f#,reverse,List,F#,Reverse,我在F中有一个自定义的列表,例如: type 'element mylist = NIL | CONS of 'element * 'element mylist 我想用类似于 let rec helperOld a b = match a with | [] -> b | h::t -> helperOld t (h::b) let revOld L = helperOld L [] 到目前为止,我想做的是 let rec helper a b =
type 'element mylist = NIL | CONS of 'element * 'element mylist
我想用类似于
let rec helperOld a b =
match a with
| [] -> b
| h::t -> helperOld t (h::b)
let revOld L = helperOld L []
到目前为止,我想做的是
let rec helper a b =
match a with
| NIL -> b
| CONS(a, b) -> helper //tail of a, head of a cons b
然而,我很难弄清楚如何得到a的尾巴和头。标准的a.头和a.尾不起作用。如何访问此自定义列表中的这些元素?您的助手函数不需要使用Head或Tail函数,因为它可以通过模式匹配提取这些值:
let rec helper a b =
match a with
| NIL -> b
| CONS(x, xs) -> helper xs (CONS(x, b))
标准的Head和Tail函数不起作用,因为您有一个自定义列表定义。您可以使用模式匹配创建自己的函数,这与您所走的路线类似:
让我的头X=
将xs与
|无->无
|CONSh,->Someh
让myTail xs=
将xs与
|无->无
|CONS_ut,t->Somet
您的helper函数不需要使用Head或Tail函数,因为它可以通过模式匹配提取这些值:
let rec helper a b =
match a with
| NIL -> b
| CONS(x, xs) -> helper xs (CONS(x, b))
标准的Head和Tail函数不起作用,因为您有一个自定义列表定义。您可以使用模式匹配创建自己的函数,这与您所走的路线类似:
让我的头X=
将xs与
|无->无
|CONSh,->Someh
让myTail xs=
将xs与
|无->无
|CONS_ut,t->Somet
更新为帮助器函数的示例答案更新为帮助器函数的示例答案