Recursion 从F中的列表中删除#

Recursion 从F中的列表中删除#,recursion,f#,functional-programming,pattern-matching,Recursion,F#,Functional Programming,Pattern Matching,我需要关于F#上的字符串连接的帮助 给定一个整数n和列表l,我必须删除l中出现的所有n。例如: remove 1 (L(1, L(2, L(3, E)))) should give me L(2, L(3, E)) 我的代码: type ilist = E | L of int * ilist let rec remove n l = match l with | E -> [] | L(h,E) -> if (h=n) then remove h (L(h,E))

我需要关于F#上的字符串连接的帮助

给定一个整数n和列表l,我必须删除l中出现的所有n。例如:

 remove 1 (L(1, L(2, L(3, E)))) should give me L(2, L(3, E))
我的代码:

type ilist = E | L of int * ilist


let rec remove n l =
match l with
| E -> []
| L(h,E) -> if (h=n) then remove h (L(h,E))
            else [h, E]
| L(h,t) -> if (h=n) then remove h t
            else [h :: (remove n t)] 
我得到一个类型错误,表示最后一行应该有类型 int*ilist
但这里有一种类型 "名单",


任何帮助都将不胜感激!谢谢

主要问题很常见。您正在将结果包装在
[]
中,忘记了
h::remove n t
已经是一个列表。所以我的固定版本:

let rec remove n l =
  match l with
    | E -> []
    | L(h,t) -> if (h = n) then remove n t
                           else h :: (remove n t)
请注意,您可以去掉中间的情况,因为可以通过将
t
E
匹配来巧妙地解决这个问题

我还将您的
删除h
更改为
删除n
;尽管他们被保证是平等的,但我认为这表明
n

是递归函数的“常量”参数。

列表串联*