Find centralized, trusted content and collaborate around the technologies you use most.
Teams
Q&A for work
Connect and share knowledge within a single location that is structured and easy to search.
我试图用不成对的元素(递归)填充malazylist,从元素k开始。例如:k = 2,列表为[2,3,5,7,9,...]代码:
[2,3,5,7,9,...]
let lgen = let rec gen k = LCons(k, fun () -> gen k (k + 2)) in gen 1;;
但是如何检查元素 k 是否未配对?(我认为这里我需要使用匹配)。
假设你的惰性列表类型是这样的:
type 'a llist = LNil | LCons of 'a * (unit -> 'a llist);;
您可以像这样进行模式匹配:
let rec lfind e lxs = match lxs with | LNil -> false | LCons(x, _) when x > e -> false | LCons(x, xs) -> if e=x then true else lfind e (xs ()) ;;