Sec*_*ret 5 ocaml functional-programming
我的地图功能是:
type 'a stream = Cons of 'a * 'a stream Lazy.t
let rec ones = Cons(1, lazy(ones));;
let rec map (f:'a -> 'b) (s:'a stream) : 'b stream =
match s with
|Cons(h,t) -> Cons(f h, lazy (map f (Lazy.force t)));;
;;
Run Code Online (Sandbox Code Playgroud)
正确?Lazy.强制它就像已经让它记忆了吗?
在ocaml懒惰类型中有一些魔力.我认为当你自己实现它时,更容易理解懒惰,这很容易,虽然不是语法上方便.技巧是
在这里,很清楚Lazy'.force中的记忆是如何以及何时发生的.
module Lazy' : sig
type 'a t
val delay: (unit -> 'a) -> 'a t
val force: 'a t -> 'a
end = struct
type 'a susp =
| NotYet of (unit -> 'a)
| Done of 'a
type 'a t = 'a susp ref
let delay f = ref (NotYet f)
let force f =
match !f with
| Done x -> x
| NotYet f' ->
let a = f'() in
f := Done a;
a
end
Run Code Online (Sandbox Code Playgroud)
输入'a stream ='of a''a stream Lazy'.t ;;
let ones = let rec''(= = Cons(1,Lazy'.delay ones')in ones'();;
让rec map fs =匹配s与| 缺点(h,t) - >缺点(fh,Lazy'.delay(fun() - > map f(Lazy'.force t)));;
| 归档时间: |
|
| 查看次数: |
1288 次 |
| 最近记录: |