Questions on closure environment capture
[Thread Prev] | [Thread Next]
[Date Prev] | [Date Next]
- Subject: Questions on closure environment capture
- From: vimacs <vimacs.hacks@xxxxxxxxx>
- Reply-to: myrddin-dev@xxxxxxxxxxxxxx
- Date: Thu, 3 May 2018 12:14:31 +0800
- To: myrddin-dev@xxxxxxxxxxxxxx
Hi,
I'm trying to use the closure in Myrddin, but I'm confused about the
implementation of it and the so-called environment capture.
Here are some code in my program.
============ code begins ================
use std
const f_ = {t
-> {x; -> x+t}
}
const main = {
var i, j
var f: (x:int -> int)[8]
var g
for i = 0; i < 4; i++
f[i] = {x
-> x+i
}
;;
for i = 5; i < 8; i++
f[i] = f_(i)
;;
i = 100
g = {x; -> x+i}
for j = 0; j < 8; j++
std.put("i = {}\n", i)
std.put("{}\n", f[j](4))
;;
std.put("{}\n", g(4))
}
================= code ends ==================
In f[0] to f[4], the result is {x; ->x+3}, I think that's because the
`i` in the function binds to the last `i` defined. So I tried to use
something like `(lambda t. (lambda x. x+t)) i` so that I can make
multiple f(x)=x+i functions with different `i`. However, when the
program tried to call f[5](4), I got a segfault.
So what is the semantic and implementation of closure and environment
capture? Is there an easy way to implement the functions I said above?
Thanks,
Iru
| Re: Questions on closure environment capture | Quentin Carbonneaux <quentin@xxxxxx> |
- Next by Date: Re: Questions on closure environment capture
- Next by thread: Re: Questions on closure environment capture
- Index(es):