C闭合&词汇范围 [英] C closures & lexical scoping

查看:63
本文介绍了C闭合&词汇范围的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

哇哇哇哇哇哇哇哇哇哇哇哇哇哇哇哇哇哇哇哇哇哇哇哇哇哇哇哇哇哇哇哇哇哇哇哇哇哇哇哇哇哇哇哇哇哇哇哇哇哇>
说话?


//词汇范围 - 通过嵌套函数

#include< stdio.h>

int main(){

int x = 10;


//词法范围

静态无效测试()$

}

test();


printf(" ;%d",x);

返回0;

}


//关闭

#include< stdio.h>


int triple(int num){

返回num * 3;

}


int square(int num){

返回num * num;

}


int main(){

int x = 10;


int(* func)(int);


func = square;

printf("%d \ n",func(5));


func = triple;

printf("%d \ n" ,三倍(5));


返回0;

}


克里斯

Woah... is it just me or do C programmers don''t bother talking about
how cool C can be (compared to Lisp, Haskell, etc.) - functionally
speaking?

// Lexical scoping - via nested functions
#include <stdio.h>

int main() {
int x = 10;

// lexical scoping
static void test() {
x = 123;
}
test();

printf("%d", x);
return 0;
}

// Closures
#include <stdio.h>

int triple(int num) {
return num * 3;
}

int square(int num) {
return num * num;
}

int main() {
int x = 10;

int (*func)(int);

func = square;
printf("%d\n", func(5));

func = triple;
printf("%d\n", triple(5));

return 0;
}

Chris

推荐答案

文章< 6e ****************************** ****@e6g2000prf.g ooglegroups.com>,

Khookie< ch ******** @ gmail.comwrote:
In article <6e**********************************@e6g2000prf.g ooglegroups.com>,
Khookie <ch********@gmail.comwrote:

>哇哇哇哇哇哇哇哇哇哇哇哇哇哇哇哇哇哇哇哇哇哇哇哇哇哇哇哇哇哇哇哇哇哇哇哇哇哇哇哇哇哇哇哇哇哇哇哇请讲?
>Woah... is it just me or do C programmers don''t bother talking about
how cool C can be (compared to Lisp, Haskell, etc.) - functionally
speaking?



嗯......

Well...


> //词汇范围 - 通过嵌套函数
>// Lexical scoping - via nested functions



糟糕,C没有嵌套函数。 Gcc将它们作为扩展名。

Oops, C doesn''t have nested functions. Gcc has them as an extension.


> //闭包
#include< stdio.h>

int triple (int num){
返回num * 3;
}
int square(int num){
return num * num;
}

int main(){
int x = 10;

int(* func)(int);

func = square;
printf("%d \ n",func(5));

func = triple;
printf("%d \ n",triple(5) ));

返回0;
}
>// Closures
#include <stdio.h>

int triple(int num) {
return num * 3;
}

int square(int num) {
return num * num;
}

int main() {
int x = 10;

int (*func)(int);

func = square;
printf("%d\n", func(5));

func = triple;
printf("%d\n", triple(5));

return 0;
}



我没有看到任何闭包,只是函数指针。没有

嵌套函数,关闭没什么好玩的。


- Richard

-

:wq

I don''t see any closures there, just pointers to functions. Without
nested functions, there''s nothing interesting to close over.

-- Richard
--
:wq


Philip Potter说:
Philip Potter said:

jacob navia写道:
jacob navia wrote:



< snip>

<snip>


>>
是的,例如
long long a,b,c;
...

c = a / b;

由于操作员超载,这会在C中生成一个函数调用...
分区过载并且工作正常使用long long类型,但是在32位系统中,这意味着函数调用
>>
Yeah, for instance
long long a,b,c;
...

c = a/b;

This generates a function call in C due to operator overloading...
Division is overloaded and works with long long types, but in a
32 bit system that means a function call



不一定。它很容易意味着一个惯用的机器序列

指令(在概念上类似于C宏)。


Not necessarily. It could easily mean an idiomatic sequence of machine
instructions (similar in concept to a C macro) instead.



或者它可以简单地转换为原生分区指令,在具有这种功能的
机器上。规则中没有任何内容可以防止这种情况发生。


-

Richard Heathfield< http://www.cpax.org.uk>

电子邮件:-http:// www。 + rjh @

谷歌用户:< http://www.cpax.org.uk/prg/writings/googly.php>

Usenet是一个奇怪的放置" - dmr 1999年7月29日

Or it could simply be translated to a native division instruction, on
machines that have such a thing. Nothing in the rules prevents this.

--
Richard Heathfield <http://www.cpax.org.uk>
Email: -http://www. +rjh@
Google users: <http://www.cpax.org.uk/prg/writings/googly.php>
"Usenet is a strange place" - dmr 29 July 1999


>>>>""""""" == Khookie< ch ******** @ gmail.comwrites:


KWoah ......是我还是C程序员不打扰说话

Kabout C可以有多酷(与Lisp,Haskell等相比) -

从功能上讲是什么?


因为我们'已经看到comp.lang.lisp会发生什么,这是关于

Lisp是多么酷,Lisp程序员是多么误解,以及如何

现在任何一天都很棒没有洗过的群众最终会意识到这是多么酷的Lisp是什么,放弃所有其他编程语言。


几乎没有一个括号可以找到。


我们花了这么多时间反复向Jacob Navia解释

topicality的概念。如果他把这些帖子贴在他们关于主题的地方,他会如何得到更好的回复?我们就不能给他们带来任何能量。更愚蠢的宣传。


Charlton

-

Charlton Wilbur
cw ***** @ chromatico.net
>>>>"K" == Khookie <ch********@gmail.comwrites:

KWoah... is it just me or do C programmers don''t bother talking
Kabout how cool C can be (compared to Lisp, Haskell, etc.) -
Kfunctionally speaking?

Because we''ve seen what happens in comp.lang.lisp, which is all about
how cool Lisp is and how misunderstood Lisp programmers are, and how
ANY DAY NOW the great unwashed masses are FINALLY going to realize
just how cool Lisp is and abandon all other programming languages.

Scarcely a parenthesis to be found.

And we''re spending so much time repeatedly explaining to Jacob Navia
the concept of "topicality," and how he''d get better responses to his
posts if he posted them where they were on topic, that we just can''t
muster the energy for any more foolish advocacy.

Charlton
--
Charlton Wilbur
cw*****@chromatico.net


这篇关于C闭合&amp;词汇范围的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆