From: Xah Lee Date: 2008-12-09T03:03:46+09:00 Subject: Re: substitute with keywords Kaz Kylheku wrote: > > What kind of an engineering decision is it to just have one Array type? > > It's what Xah Lee would have designed. Pascal J. Bourguignon wrote: > Ruby's been done by some Japanese newbie... I guess there was some > language barrier preventing him to learn from the 50 years of occidental > experience in programming language design. At least, he wasn't a > "linguist"... > > In any case, why should we suffer for THEIR incompetences?!? if you really feel that way, post your opinion to ruby community, instead of insulting other langs in your own community. You do not need to write it in a insulting way. You can simply express your opinion. This way, we can avoid each lang's fanatics living in their own world and reading daily their own fantasies. See: • Cross-posting & Language Factions http://xahlee.org/Netiquette_dir/cross-post.html Xah ∑ http://xahlee.org/ ☄ ------------------------------------- On Dec 8, 12:00 am, p...@informatimago.com (Pascal J. Bourguignon) wrote: Kaz Kylheku writes: > Firstly, doesn't Ruby have the concept of a literal? You made the initial empty > list using the [] notation. You can modify this with < Does it mean that [] > always allocates a fresh object? Yes. [] is equivalent to Array.new >> [].equal?([]) => false AFAIU: Object.equal? is equivalent to CL:EQ Object.eql? is equivalent to CL:EQL Object.== is equivalent to CL:EQUAL (and is usually overriden by subclasses). Object.=== is equivalent to CL:TYPEP > If so, by what syntax do we achieve a real > compile-time constant? None. > (In Lisp, we don't worry in this case because the empty list is just NIL, so all > of the conses for the accumulated come out of the NCONC. But an empty > array in Ruby isn't just a value like NIL, right?). Right, it's an object like the other Array instances. > Secondly, how about the efficiency of adding at the end of the array? If this > is fast in Ruby, how the hell does it support proper substructure sharing > between lists? There's no structure sharing AFAIK. > Can you cons an existing list with an item to produce a list which shares the > tail structure with the original list, so that just one cell has to be > allocated for the new list? How about CDR? You can always Greenspun. Most Ruby libraries are just that. (For example, there are several Ruby libraries to read Ruby code into "R-exp", Arrays used like S-exp). So, you could do implement Lisp-like lists as [1,[2,[3,nil]]]. (def cons(a , b) [ a , b ] end) (def car(cons) (cons [ 0 ]) end) (def cdr(cons) (cons [ 1 ]) end) (def printSexp(object) (if ((Array === object) and (2 == (object . length))) (first = true) (printf "(") (current = object) (while ((Array === current) and (2 == (current . length))) (if first (first = false) else (printf " ") end) (printSexp (car current)) (current = (cdr current)) end) (if (not(current == nil)) (printf " . ") (printSexp current) end) (printf ")") else (printf "%s" , object) end) object end) >> (printSexp (cons 1 , (cons (cons 2 , 2) , (cons 3 , nil)))) (1 (2 . 2) 3)=> [1, [[2, 2], [3, nil]]] > Can you show me Ruby code which produces [2, 3] out of [1, 2, 3] without > allocating a single byte of additional memory? That's not possible. > Show me the Ruby syntax for this: (#1=(1 2) #1#) -> ((1 2) (1 2)). > Both occurences of (1 2) must be the same object! (a = [1 , 2]) (b = [a , a]) ((b [ 0 ]).equal?(b [ 1 ])) --> true But of course, no literal, it's done at run-time. > What the hell are these arrays anyway? Are they tables or linked lists? > Supposedly, Ruby leaves it up to the implementation. So you don't actually > know! Getting the N-th element? Could be O(1), could be O(N). Whatever. You'd have to have a look at the sources. By the way, the equivalent of CLHS for Ruby are the sources of the single Ruby implementation. The so called "Ruby Core Reference":http://www.ruby-doc.org/core/ > Abstracting this is fine for throwaway programs, but in real-world, situations > arise in which the difference between lists and arrays matters. This is an OOPL. You can also write: (class Cons (attr_accessor :car , :cdr) (def initialize(a , d) (@car = a) (@cdr = d) self end) end) and write: (Cons . new(1 , (Cons . new(2 , 2)) , (Cons . new(3 , nil)))) > It's possible to have a design with two (or more) programmer-visible structures > for maintaing sequences, yet methods which treat them the same way whenever > that is convenient. Yes, this is an OOPL with single inheritance, you can define a Sequence class, and subclasses such as Vector or List. > What kind of an engineering decision is it to just have one Array type? > It's whatXahLeewould have designed. Ruby's been done by some Japanese newbie... I guess there was some language barrier preventing him to learn from the 50 years of occidental experience in programming language design. At least, he wasn't a "linguist"... In any case, why should we suffer for THEIR incompetences?!? -- __Pascal Bourguignon__