[問題] Closure
想請問,closure, function literal, lambda, functor, anonymous function
它們之間有什麼關聯呢?
我知道 lambda 是一種表示式,可是其它的就模模糊糊了…
另外我在網路上看到 C 的相關資料(不知道是不是標準?網路上說是…)
ref1:
https://sholtz9421.wordpress.com/2012/03/06/closures-in-ansi-cc/
ref2: 他講的好像怪怪的…
http://computer-programming-forum.com/47-c-language/127a1918dfb4b5cf.htm
例:
#include <stdio.h>
#include <Block.h>
typedef void (^fun_t)(int);
fun_t get_fun(int);
int main(int)
{
fun_t fun1 = get_fun(10);
fun_t fun2 = get_fun(20);
fun1(100);
fun2(200);
^void(int i)
{
printf("%d\n", i);
}(150);
Block_release(fun1);
Block_release(fun2);
return 0;
}
fun_t get_fun(int z)
{
__block int x = 50;
return Block_copy(^(int y)
{
printf("%d\n", x + y + z);
});
}
root@localhost:~# clang -std=c89 -Wall -Wextra -Weverything -Werror -pedantic
-Ofast -o test test.c -fblocks -lBlocksRuntime
root@localhost:~# ./test
160
270
150
root@localhost:~#
(如果有用到 get_fun 這種東西的話,就要 -lBlocksRuntime (libBlocksRuntime-dev)
如果只是 ^void(void) { ... }() 就只要 -fblocks)
再來,問題來了:
1、return (xxx) 小括號裡面的 xxx 是不是一個 function literal 呢?
2、這裡 closure close 的是 x 嗎?
可是我試過,x 的生命周期並不會延長啊?(所以用 static)
3、get_fun 算是一個 functor 嗎?
4、^void(void) { ... } 這個是 anonymous function 嗎?
不知道我的問題有沒有很奇怪 QQ
然後最近有逛到 PLT 版,對 Lambda calculus 有點興趣,不知道要怎麼入門呢?(呼叫
suhorng 大啊啊啊~)
--
※ 發信站: 批踢踢實業坊(ptt.cc), 來自: 49.217.66.47
※ 文章網址: https://www.ptt.cc/bbs/C_and_CPP/M.1448916464.A.77C.html
※ 編輯: OPIV (49.217.66.47), 12/01/2015 10:37:22
→
12/01 11:45, , 1F
12/01 11:45, 1F
→
12/01 11:45, , 2F
12/01 11:45, 2F
推
12/01 15:34, , 3F
12/01 15:34, 3F
→
12/01 15:35, , 4F
12/01 15:35, 4F
OK~
現在已經有點懂了,haskell 聽說不錯
後來發現上面有些不對的地方,所以改掉了,例如 static -> __block…
※ 編輯: OPIV (49.217.65.61), 12/02/2015 11:20:20
C_and_CPP 近期熱門文章
PTT數位生活區 即時熱門文章