Re: Discuss changing the behaviour of append()

new topic     » goto parent     » topic index » view thread      » older message » newer message
gimlet said...

Just a question:

Assuming that appending/prepending to atoms is on
we have:

? append(1,{})  -- {1,{}} 
? append({},1)  -- {1} 
? prepend(1,{}) -- {{},1} 
? prepend({},1) -- {1} 

I'm not saying it's wrong but don't you find the difference strange?

Thanks to Gimlet, the documentation has hopefully been improved:

I think you can choose to view this as strange or as not strange. From there you can design two forks of Euphoria.


Consider append and prepend to be special cases of the insert function.

The proposed changes to O[ are going to put emphasis on object behavior and blur some previous atom-sequence distinctions. While a != {a} remains true, the new behavior tolerates the idea that a becomes {a} when it is convenient in a prepend, insert, or append.

TARGET is an atom, like  1, is promoted to { 1   ,$} using prepend, insert, append  
 
prepend    append 
      V    V 
   {    a     ,$ }   -- all sequences can be written using the ,$ end marker 
      ^    ^     
      1    2   -- insert index value 
                  -- 1 is like prepend 
                  -- 2 is like append 
 
 
when inserting a empty sequence into an "promoted" atom the results are     
{ {}, 1}   {1, {} }  
     

TARGET is an empty sequence, like {}, can be written as {$}     
 
prepend    append 
      V    V 
   {     $   }   -- an empty sequence can be written using the $ end marker 
      ^    ^     
      1    2   -- insert index value 
                  -- 1 is like prepend 
                  -- 2 is like append 
 
when inserting an atom into an empty sequence the results are     
{ 1 }   { 1 }  
     

The length function is now oddly named. It could have been named item_count (items, count, size, topsize, ... ?)

  • on its own {} has a zero item count, but inside a sequence it is itself one item
  • length( { } ) or length( "" ) is zero, this does not mean a null sequence, but a sequence with no item count
  • length( a ) is one, this does not mean a number has "length", but that one atom is one item
  • length( {a} ) = length( a ) means there is one item count in each example

Concatenation combines objects into one. The total item count is equal to the sum of the individual item counts.

Insert, prepend, or append extends the item count by exactly one.

The results of an insertion follow a consistent pattern; from that basis the results are not strange.

If the results seem "strange" is it because an empty sequence can be both paradoxically "length zero and one item"? Note that quantum physics copes with ideas like this.

Another consideration is that syntax is not mathematics. Sometimes syntax is based on mathematics (consider lisp, haskel), sometimes syntax just looks like mathematics (consider the assignment operator = which is better represented by :=), and sometimes syntax just happens when a language is designed.

You must always believe in the documentation, the basis of all truth, and disregard all previous knowledge; documentation in progress see above. (If you are not in a humorous mood, ignore the previous sentence.)

_tom

new topic     » goto parent     » topic index » view thread      » older message » newer message

Search



Quick Links

User menu

Not signed in.

Misc Menu