language-implementation

Why does `vector` implementation have multiple cases?

南笙酒味 提交于 2019-12-23 07:27:58
问题 Here's clojure's definition of vector: (defn vector "Creates a new vector containing the args." {:added "1.0" :static true} ([] []) ([a] [a]) ([a b] [a b]) ([a b c] [a b c]) ([a b c d] [a b c d]) ([a b c d & args] (. clojure.lang.LazilyPersistentVector (create (cons a (cons b (cons c (cons d args)))))))) Why are there so many cases? Or, if there are so many, why aren't there more? My guess is that it's striking a balance between implementation efficiency and probability, but I don't quite see

Lua: understanding table array part and hash part

故事扮演 提交于 2019-12-22 08:14:37
问题 In section 4, Tables, in The Implementation of Lua 5.0 there is and example: local t = {100, 200, 300, x = 9.3} So we have t[4] == nil . If I write t[0] = 0 , this will go to hash part . If I write t[5] = 500 where it will go? Array part or hash part ? I would eager to hear answer for Lua 5.1, Lua 5.2 and LuaJIT 2 implementation if there is difference. 回答1: Contiguous integer keys starting from 1 always go in the array part. Keys that are not positive integers always go in the hash part.

How is “letrec” implemented without using “set!”?

最后都变了- 提交于 2019-12-22 06:29:35
问题 How can letrec be implemented without using set! ? It seems to me that set! is an imperative programming construct, and that by using it, one loses the benefits of functional programming. 回答1: I know usually we ask content to be copied but there is no short answer to your question. http://www.cs.indiana.edu/~dyb/pubs/fixing-letrec.pdf 回答2: No. Just because a functional feature is implemented with imperative code behind the scenes, that doesn't make the feature be imperative. Our computing

How is “letrec” implemented without using “set!”?

て烟熏妆下的殇ゞ 提交于 2019-12-22 06:29:10
问题 How can letrec be implemented without using set! ? It seems to me that set! is an imperative programming construct, and that by using it, one loses the benefits of functional programming. 回答1: I know usually we ask content to be copied but there is no short answer to your question. http://www.cs.indiana.edu/~dyb/pubs/fixing-letrec.pdf 回答2: No. Just because a functional feature is implemented with imperative code behind the scenes, that doesn't make the feature be imperative. Our computing

Why is the main method entry point in most C# programs static?

眉间皱痕 提交于 2019-12-22 01:23:12
问题 Why is the main method entry point in most C# programs static? 回答1: In order to call an instance method you need an instance of an object. This means in order to start your program the CLR would need to create an instance of say Program in order to call the method Main . Hence the constructor of Program would run before Main which defeats the purpose of having a main altogether. 回答2: I'd turn the question around. What is the compelling benefit of implementing the feature that allows Main to

How does a Haskell compiler work?

…衆ロ難τιáo~ 提交于 2019-12-20 08:01:24
问题 Where can I get some paper/doc/whatever which describes how a Haskell compiler actually works? I read quite a few of the docs of GHC, but stopped after getting a headache. So, something which doesn't require a PhD to understand it and isn't written in the You're-supposed-to-be-already-familiar-with-it style would be preferable. It's not a problem if it's really long and takes some time to understand it though. PS: Most interesting would be something about GHC, but anything is ok. 回答1: You can

How is C++'s multiple inheritance implemented?

允我心安 提交于 2019-12-18 11:47:17
问题 Single inheritance is easy to implement. For example, in C, the inheritance can be simulated as: struct Base { int a; } struct Descendant { Base parent; int b; } But with multiple inheritance, the compiler has to arrange multiple parents inside newly constructed class. How is it done? The problem I see arising is: should the parents be arranged in AB or BA, or maybe even other way? And then, if I do a cast: SecondBase * base = (SecondBase *) &object_with_base1_and_base2_parents; The compiler

How are java interfaces implemented internally? (vtables?)

天大地大妈咪最大 提交于 2019-12-17 15:27:44
问题 C++ has multiple inheritance. The implementation of multiple inheritance at the assembly level can be quite complicated, but there are good descriptions online on how this is normally done (vtables, pointer fixups, thunks, etc). Java doesn't have multiple implementation inheritance, but it does have multiple interface inheritance, so I don't think a straight forward implementation with a single vtable per class can implement that. How does java implement interfaces internally? I realize that

where python store global and local variables?

删除回忆录丶 提交于 2019-12-14 02:26:59
问题 Almost same as question Where are the local, global, static, auto, register, extern, const, volatile variables are stored?, the difference is this thread is asking how Python language implement this. 回答1: Of all those, Python only has "local", "global" and "nonlocal" variables. Some of those are stored in a Dictionary or dictionary like object, which usually can be explicitly addressed. "global" : Actually "global" variables are global relatively to the module where they are defined -

Lisp macros quotation implementation in JavaScript

一笑奈何 提交于 2019-12-11 07:36:52
问题 I have basic scheme like lisp in JavaScript and have problem with backquote and quote macros, they evaluate symbols if they are at first element of the array like > `(foo 10) give error foo not found it work for code like this > (define x '(1 2 3)) > (print `(1 2 ,@x 4 5)) my eval function look like this: function evaluate(code, env) { env = env || global_env; var value; if (typeof code === 'undefined') { return; } var first = code.car; var rest = code.cdr; if (first instanceof Pair) { value