From: ·············@conservatorio.ch
Subject: substitute with keywords
Date: 
Message-ID: <a530f1a9-7a0e-49e2-ab3e-fcec93f939c4@l33g2000pri.googlegroups.com>
Hallo,
I'm just starting with common lisp, and have a maybe stupid question.
How do I substitute the element (60) after the keyword :NOTES in a
list like this?


(((((4 ((2 :NOTES (60)) (3 ((2 :START-TIME 1.6 :NOTES (60)) (-1 :START-
TIME 2.8 :NOTES (60)) (1 :START-TIME 3.4 :NOTES (60)))))) :TIME-
SIGNATURE (4 4))) :INSTRUMENT NIL :STAFF :TREBLE-STAFF))


Thanks for the help!

nadir

From: nunb
Subject: Re: substitute with keywords
Date: 
Message-ID: <9a7e92ae-1dd7-40e6-9af0-49c4e937307e@l39g2000yqn.googlegroups.com>
On Dec 6, 2:59 pm, ·············@conservatorio.ch wrote:
> Hallo,
> I'm just starting with common lisp, and have a maybe stupid question.
> How do I substitute the element (60) after the keyword :NOTES in a
> list like this?
>
> (((((4 ((2 :NOTES (60)) (3 ((2 :START-TIME 1.6 :NOTES (60)) (-1 :START-
> TIME 2.8 :NOTES (60)) (1 :START-TIME 3.4 :NOTES (60)))))) :TIME-
> SIGNATURE (4 4))) :INSTRUMENT NIL :STAFF :TREBLE-STAFF))
>
> Thanks for the help!
>
> nadir

In what context do you wish to do this? Your list apparently
represents a tree structure, so wherever your modified list is going
to be consumed, you'll have to make a copy before sending it there.
Have a look at copy-tree. If you write a function to copy the subtree
which represents (2 :notes (60)) you may be able to use the alist
functions (assoc and friends). If you do not need to maintain the list/
tree structure, you could use flatten and then use assoc.
From: ·············@conservatorio.ch
Subject: Re: substitute with keywords
Date: 
Message-ID: <91b62f77-405e-42fe-af0b-f3e39f6f5545@f20g2000yqg.googlegroups.com>
On 6 Dic, 15:43, nunb <··············@gmail.com> wrote:
> On Dec 6, 2:59 pm, ·············@conservatorio.ch wrote:
>
> > Hallo,
> > I'm just starting with common lisp, and have a maybe stupid question.
> > How do I substitute the element (60) after the keyword :NOTES in a
> > list like this?
>
> > (((((4 ((2 :NOTES (60)) (3 ((2 :START-TIME 1.6 :NOTES (60)) (-1 :START-
> > TIME 2.8 :NOTES (60)) (1 :START-TIME 3.4 :NOTES (60)))))) :TIME-
> > SIGNATURE (4 4))) :INSTRUMENT NIL :STAFF :TREBLE-STAFF))
>
> > Thanks for the help!
>
> > nadir
>
> In what context do you wish to do this? Your list apparently
> represents a tree structure, so wherever your modified list is going
> to be consumed, you'll have to make a copy before sending it there.
> Have a look at copy-tree. If you write a function to copy the subtree
> which represents (2 :notes (60)) you may be able to use the alist
> functions (assoc and friends). If you do not need to maintain the list/
> tree structure, you could use flatten and then use assoc.

to difficult for me...sorry! but solution from rainer joswig was good!
From: Rainer Joswig
Subject: Re: substitute with keywords
Date: 
Message-ID: <joswig-230D28.17542506122008@news-europe.giganews.com>
In article 
<····································@l33g2000pri.googlegroups.com>,
 ·············@conservatorio.ch wrote:


> Hallo,
> I'm just starting with common lisp, and have a maybe stupid question.
> How do I substitute the element (60) after the keyword :NOTES in a
> list like this?

The question is not stupid, but lacks information.

It is a complicated list. How is it created? How do you
traverse it?

> 
> 
> (((((4 ((2 :NOTES (60)) (3 ((2 :START-TIME 1.6 :NOTES (60)) (-1 :START-
> TIME 2.8 :NOTES (60)) (1 :START-TIME 3.4 :NOTES (60)))))) :TIME-
> SIGNATURE (4 4))) :INSTRUMENT NIL :STAFF :TREBLE-STAFF))
> 
> 
> Thanks for the help!
> 
> nadir


Let's try a reconstruction:


Something like   (:a 1 :b 2)  is called a property list.

You can get a value from a property list with GETF.

   (getf list :b)

You can destructively set values like this:

   (setf (getf list :b) 3)


You have basically two versions (other then printing it to
a string, do a textual change and reread it from the string):

a) traverse the data structure and create new data with
   the changes made

b) traverse the data structure and change the data
   destructively


For b) you need a function map-notes

(defun map-notes (function notes)
   ...)

Which calls a function for each data element.

Then you call that similar to this:


(map-notes (lambda (note)
             (when (getf (rest note) :notes)
               (setf (getf (rest note) :notes) 
                     (list 70))))
           *my-notes*)

(defparameter *my-notes*
  (copy-tree '(((((4 ((2 :NOTES (60))
                      (3 ((2 :START-TIME 1.6 :NOTES (60))
                          (-1 :START-TIME 2.8 :NOTES (60))
                          (1 :START-TIME 3.4 :NOTES (60))))))
                  :TIME-SIGNATURE (4 4)))
                :INSTRUMENT NIL :STAFF :TREBLE-STAFF))))

; my attempt to traverse the data-structure

(defun map-notes (function notes)
  (when (and (consp notes) (symbolp (second notes)))
    (funcall function notes))
  (when (consp (first notes))
    (map-notes function (first notes)))
  (when (consp (second notes))
    (map-notes function (rest notes)))
  notes)

Let's try it:

CL-USER 117 > (pprint *my-notes*)

(((((4
     ((2 :NOTES (60))
      (3
       ((2 :START-TIME 1.6 :NOTES (60))
        (-1 :START-TIME 2.8 :NOTES (60))
        (1 :START-TIME 3.4 :NOTES (60))))))
    :TIME-SIGNATURE
    (4 4)))
  :INSTRUMENT
  NIL
  :STAFF
  :TREBLE-STAFF))

CL-USER 118 > (map-notes (lambda (note)
                           (when (getf (rest note) :notes)
                             (setf (getf (rest note) :notes) (list 70))))
                         *my-notes*)
(((((4 ((2 :NOTES (70)) (3 ((2 :START-TIME 1.6 :NOTES (70)) (-1 :START-TIME 2.8 :NOTES (70)) (1 :START-TIME 3.4 :NOTES (70)))))) :TIME-SIGNATURE (4 4))) :INSTRUMENT NIL :STAFF :TREBLE-STAFF))

CL-USER 119 > (pprint *my-notes*)

(((((4
     ((2 :NOTES (70))
      (3
       ((2 :START-TIME 1.6 :NOTES (70))
        (-1 :START-TIME 2.8 :NOTES (70))
        (1 :START-TIME 3.4 :NOTES (70))))))
    :TIME-SIGNATURE
    (4 4)))
  :INSTRUMENT
  NIL
  :STAFF
  :TREBLE-STAFF))

-- 
http://lispm.dyndns.org/
From: William James
Subject: Re: substitute with keywords
Date: 
Message-ID: <ghetg9$n04$1@aioe.org>
Rainer Joswig wrote:

> In article 
> <····································@l33g2000pri.googlegroups.com>,
>  ·············@conservatorio.ch wrote:
> 
> 
> > Hallo,
> > I'm just starting with common lisp, and have a maybe stupid
> > question.  How do I substitute the element (60) after the keyword
> > :NOTES in a list like this?
> 
> The question is not stupid, but lacks information.
> 
> It is a complicated list. How is it created? How do you
> traverse it?
> 
> > 
> > 
> > (((((4 ((2 :NOTES (60)) (3 ((2 :START-TIME 1.6 :NOTES (60)) (-1
> > :START- TIME 2.8 :NOTES (60)) (1 :START-TIME 3.4 :NOTES (60))))))
> > :TIME- SIGNATURE (4 4))) :INSTRUMENT NIL :STAFF :TREBLE-STAFF))
> > 
> > 
> > Thanks for the help!
> > 
> > nadir
> 
> 
> Let's try a reconstruction:
> 
> 
> Something like   (:a 1 :b 2)  is called a property list.
> 
> You can get a value from a property list with GETF.
> 
>    (getf list :b)
> 
> You can destructively set values like this:
> 
>    (setf (getf list :b) 3)
> 
> 
> You have basically two versions (other then printing it to
> a string, do a textual change and reread it from the string):
> 
> a) traverse the data structure and create new data with
>    the changes made
> 
> b) traverse the data structure and change the data
>    destructively
> 
> 
> For b) you need a function map-notes
> 
> (defun map-notes (function notes)
>    ...)
> 
> Which calls a function for each data element.
> 
> Then you call that similar to this:
> 
> 
> (map-notes (lambda (note)
>              (when (getf (rest note) :notes)
>                (setf (getf (rest note) :notes) 
>                      (list 70))))
>            *my-notes*)
> 
> (defparameter *my-notes*
>   (copy-tree '(((((4 ((2 :NOTES (60))
>                       (3 ((2 :START-TIME 1.6 :NOTES (60))
>                           (-1 :START-TIME 2.8 :NOTES (60))
>                           (1 :START-TIME 3.4 :NOTES (60))))))
>                   :TIME-SIGNATURE (4 4)))
>                 :INSTRUMENT NIL :STAFF :TREBLE-STAFF))))
> 
> ; my attempt to traverse the data-structure
> 
> (defun map-notes (function notes)
>   (when (and (consp notes) (symbolp (second notes)))
>     (funcall function notes))
>   (when (consp (first notes))
>     (map-notes function (first notes)))
>   (when (consp (second notes))
>     (map-notes function (rest notes)))
>   notes)
> 
> Let's try it:
> 
> CL-USER 117 > (pprint *my-notes*)
> 
> (((((4
>      ((2 :NOTES (60))
>       (3
>        ((2 :START-TIME 1.6 :NOTES (60))
>         (-1 :START-TIME 2.8 :NOTES (60))
>         (1 :START-TIME 3.4 :NOTES (60))))))
>     :TIME-SIGNATURE
>     (4 4)))
>   :INSTRUMENT
>   NIL
>   :STAFF
>   :TREBLE-STAFF))
> 
> CL-USER 118 > (map-notes (lambda (note)
>                            (when (getf (rest note) :notes)
>                              (setf (getf (rest note) :notes) (list
> 70))))                          *my-notes*)
> (((((4 ((2 :NOTES (70)) (3 ((2 :START-TIME 1.6 :NOTES (70)) (-1
> :START-TIME 2.8 :NOTES (70)) (1 :START-TIME 3.4 :NOTES (70))))))
> :TIME-SIGNATURE (4 4))) :INSTRUMENT NIL :STAFF :TREBLE-STAFF))
> 
> CL-USER 119 > (pprint *my-notes*)
> 
> (((((4
>      ((2 :NOTES (70))
>       (3
>        ((2 :START-TIME 1.6 :NOTES (70))
>         (-1 :START-TIME 2.8 :NOTES (70))
>         (1 :START-TIME 3.4 :NOTES (70))))))
>     :TIME-SIGNATURE
>     (4 4)))
>   :INSTRUMENT
>   NIL
>   :STAFF
>   :TREBLE-STAFF))

Ruby:

list =
[[[[[4, [[2, :NOTES, [60]], [3, [[2, :START_TIME, 1.6,
:NOTES, [60]], [-1, :START_TIME, 2.8, :NOTES, [60]],
[1, :START_TIME, 3.4, :NOTES, [60]]]]]], :TIME_SIGNATURE,
[4, 4]]], :INSTRUMENT, nil, :STAFF, :TREBLE_STAFF]]

# Replace value for key in deeply nested list.
def replace_val list, key, val
  replace = false
  list.inject([]){|accum,x|
    accum <<
      if replace
        replace = nil
        val
      else
        if x.class == Array
          replace_val( x, key, val )
        else
          replace = val  if x == key
          x
        end
      end }
end

p replace_val( list, :NOTES, [70] )

--- output ---
[[[[[4, [[2, :NOTES, [70]], [3, [[2, :START_TIME, 1.6,
:NOTES, [70]], [-1, :START_TIME, 2.8, :NOTES, [70]],
[1, :START_TIME, 3.4, :NOTES, [70]]]]]], :TIME_SIGNATURE,
[4, 4]]], :INSTRUMENT, nil, :STAFF, :TREBLE_STAFF]]
From: Stanisław Halik
Subject: Re: substitute with keywords
Date: 
Message-ID: <ghgt97$18ir$1@opal.icpnet.pl>
thus spoke William James <·········@yahoo.com>:

># Replace value for key in deeply nested list.
> def replace_val list, key, val
>  replace = false
>  list.inject([]){|accum,x|
>    accum <<
>      if replace
>        replace = nil
>        val
>      else
>        if x.class == Array
>          replace_val( x, key, val )
>        else
>          replace = val  if x == key
>          x
>        end
>      end }
> end

> p replace_val( list, :NOTES, [70] )

Now do proper destructuring with that fancy thing of yours.

-- 
The great peril of our existence lies in the fact that our diet consists
entirely of souls. -- Inuit saying
From: Kaz Kylheku
Subject: Re: substitute with keywords
Date: 
Message-ID: <20081222231743.860@gmail.com>
On 2008-12-06, William James <·········@yahoo.com> wrote:
> Ruby:
>
> list =
> [[[[[4, [[2, :NOTES, [60]], [3, [[2, :START_TIME, 1.6,
>:NOTES, [60]], [-1, :START_TIME, 2.8, :NOTES, [60]],
> [1, :START_TIME, 3.4, :NOTES, [60]]]]]], :TIME_SIGNATURE,
> [4, 4]]], :INSTRUMENT, nil, :STAFF, :TREBLE_STAFF]]
>
> # Replace value for key in deeply nested list.
> def replace_val list, key, val
>   replace = false
>   list.inject([]){|accum,x|
>     accum <<
>       if replace
>         replace = nil
>         val
>       else
>         if x.class == Array
>           replace_val( x, key, val )
>         else
>           replace = val  if x == key
>           x
>         end
>       end }
> end

This is  stupid programming. Basically you're doing this:

(reduce (lambda (accum x) (nconc accum ...))
        input-list
	:initial-value ())

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? If so, by what syntax do we achieve a real
compile-time constant?

(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?).

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?

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?

Can you show me Ruby code which produces [2, 3] out of [1, 2, 3] without
allocating a single byte of additional memory?

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!

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.

Abstracting this is fine for throwaway programs, but in real-world, situations
arise in which the difference between lists and arrays matters.  

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.

What kind of an engineering decision is it to just have one Array type?
It's what Xah Lee would have designed.
From: Tamas K Papp
Subject: Re: substitute with keywords
Date: 
Message-ID: <6q2pk1Faem6lU1@mid.individual.net>
On Sun, 07 Dec 2008 19:52:20 +0000, 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.

Nay.  While I find Ruby pretty idiotic, designing a language with an 
actual working implementation implies some programming, something Xah is 
not capable of.  He would just write rants with titles like "The 
Fundamental Meaning of Arrays" and "Misconceptions of Morons Who Think 
Otherwise".

:-)

Tamas
From: Pascal J. Bourguignon
Subject: Re: substitute with keywords
Date: 
Message-ID: <871vwjrt9e.fsf@informatimago.com>
Kaz Kylheku <········@gmail.com> 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 <<?  

Yes.  Array.<< is equivalent to Array.push.


> 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 what Xah Lee would 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__
From: Xah Lee
Subject: Re: substitute with keywords
Date: 
Message-ID: <a70fd4db-3677-47e5-9cb6-924361168f45@v5g2000prm.googlegroups.com>
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, ····@informatimago.com (Pascal J. Bourguignon)
wrote:
Kaz Kylheku <········@gmail.com> 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 <<?

Yes.  Array.<< is equivalent to Array.push.

> 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__
From: Stefan Nobis
Subject: Re: substitute with keywords
Date: 
Message-ID: <m11vwh7mtr.fsf@familie-nobis.de>
Kaz Kylheku <········@gmail.com> writes:

> 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.

The problem is, many programmers out there (even those with a degree
in computer science) don't know the difference, today (or at least
they don't think about it). In the last month I had to work with some
beginners (fresh from university) and (using C#) they don't think a
second about using an List (some kind of extensible array -- linked
list of blocks of memory), a LinkedList (proper doubly linked list) or
something else. Even when they know exaclty how many items will be put
in a List, they don't set the capacity.

Those people learn about O-calculus in university, they learn about
datastructures and efficient algorithms, but they don't connect those
theory thingies with their actual work.

Personally, I like Common Lisp among other things because you can
really see what's going on under the hood and at the same time get the
same (and more) abstraction and comfort found in collection libries of
main stream languages like Python, Ruby, C# (especially bad collection
library), Java etc.

-- 
Stefan.
From: George Neuner
Subject: Re: substitute with keywords
Date: 
Message-ID: <8q50k454tdk4qgpmhhltu369bv3v5h0o7f@4ax.com>
On Tue, 09 Dec 2008 09:52:00 +0100, Stefan Nobis <······@gmx.de>
wrote:

>Kaz Kylheku <········@gmail.com> writes:
>
>> 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.
>
>The problem is, many programmers out there (even those with a degree
>in computer science) don't know the difference, today (or at least
>they don't think about it). In the last month I had to work with some
>beginners (fresh from university) and (using C#) they don't think a
>second about using an List (some kind of extensible array -- linked
>list of blocks of memory), a LinkedList (proper doubly linked list) or
>something else. Even when they know exaclty how many items will be put
>in a List, they don't set the capacity.
>
>Those people learn about O-calculus in university, they learn about
>datastructures and efficient algorithms, but they don't connect those
>theory thingies with their actual work.

That's because many programmers - and particularly entry level ones -
just don't encounter large order situations.  If the difference
between using a list and using an array is undetectable to the user
then it doesn't matter.  If the difference is a few seconds, for many
purposes that won't matter either.  It's only when they finally do
something where difference is hours or worse that they rediscover data
structures and algorithm complexity.

>Personally, I like Common Lisp among other things because you can
>really see what's going on under the hood and at the same time get the
>same (and more) abstraction and comfort found in collection libries of
>main stream languages like Python, Ruby, C# (especially bad collection
>library), Java etc.

George
From: =?UTF-8?B?QW5kcsOpIFRoaWVtZQ==?=
Subject: Re: substitute with keywords
Date: 
Message-ID: <ghhv2m$70q$1@news.motzarella.org>
William James schrieb:

> Ruby:
> 
> list =
> [[[[[4, [[2, :NOTES, [60]], [3, [[2, :START_TIME, 1.6,
> :NOTES, [60]], [-1, :START_TIME, 2.8, :NOTES, [60]],
> [1, :START_TIME, 3.4, :NOTES, [60]]]]]], :TIME_SIGNATURE,
> [4, 4]]], :INSTRUMENT, nil, :STAFF, :TREBLE_STAFF]]
> 
> # Replace value for key in deeply nested list.
> def replace_val list, key, val
>   replace = false
>   list.inject([]){|accum,x|
>     accum <<
>       if replace
>         replace = nil
>         val
>       else
>         if x.class == Array
>           replace_val( x, key, val )
>         else
>           replace = val  if x == key
>           x
>         end
>       end }
> end
> 
> p replace_val( list, :NOTES, [70] )

Uh, my eyes hurt.
Ruby reminds me of Comal from the late 70ies.
Better let’s have a solution in Lisp (Clojure here):

(def data [[[[[4, [[2, :NOTES, [60]], [3, [[2, :START_TIME, 1.6,
           :NOTES, [60]], [-1, :START_TIME, 2.8, :NOTES, [60]],
           [1, :START_TIME, 3.4, :NOTES, [60]]]]]], :TIME_SIGNATURE,
           [4, 4]]], :INSTRUMENT, nil, :STAFF, :TREBLE_STAFF]])


(defn replace-val [nvec k v]
   (loop [loc (zip/vector-zip nvec)]
     (if (zip/end? loc)
         (zip/root loc)
         (recur (zip/next
                 (if (= (zip/node loc) k)
                     (zip/replace (zip/next loc) v)
                     loc))))))

user> (replace-val data :NOTES [70])
[[[[[4 [[2 :NOTES [70]] [3 [[2 :START_TIME 1.6 :NOTES [70]] [-1
:START_TIME 2.8 :NOTES [70]] [1 :START_TIME 3.4 :NOTES [70]]]]]]
:TIME_SIGNATURE [4 4]]] :INSTRUMENT nil :STAFF :TREBLE_STAFF]]

Code for replace-val is a modified version of what we can find here:
http://clojure.org/other_libraries

With zippers we don’t need to think very much for a task like this.
Maybe you wrote down your Comal solution very fast, but still I feel
it is much more difficult to work with inject here and blocks there,
and so on. In Lisp however we can state the algorithm as we think
about it. Much easier. Plus half LOC count, compared to your Comalruby.

I still don’t get why you keep posting your suggestions.
You loose pretty much every time you do it.
Maybe in reality you are a Lisp fan and want to support Lisp, by giving
example after example that can be done better in Lisp?

Nothing against Ruby in general, it’s a very nice scripting language.
But still let’s keep in mind what its inventor has to say:
“Some may say Ruby is a bad rip-off of Lisp or Smalltalk, and I
  admit that. But it is nicer to ordinary people.”
- Matz, LL2


André
-- 
Lisp is not dead. It’s just the URL that has changed:
http://clojure.org/
From: ·············@conservatorio.ch
Subject: Re: substitute with keywords
Date: 
Message-ID: <e0f9c63a-96fb-4305-bd08-18d49136213e@33g2000yqm.googlegroups.com>
On 6 Dic, 17:54, Rainer Joswig <······@lisp.de> wrote:
> In article
> <····································@l33g2000pri.googlegroups.com>,
>
>  ·············@conservatorio.ch wrote:
> > Hallo,
> > I'm just starting with common lisp, and have a maybe stupid question.
> > How do I substitute the element (60) after the keyword :NOTES in a
> > list like this?
>
> The question is not stupid, but lacks information.
>
> It is a complicated list. How is it created? How do you
> traverse it?
>
>
>
> > (((((4 ((2 :NOTES (60)) (3 ((2 :START-TIME 1.6 :NOTES (60)) (-1 :START-
> > TIME 2.8 :NOTES (60)) (1 :START-TIME 3.4 :NOTES (60)))))) :TIME-
> > SIGNATURE (4 4))) :INSTRUMENT NIL :STAFF :TREBLE-STAFF))
>
> > Thanks for the help!
>
> > nadir
>
> Let's try a reconstruction:
>
> Something like   (:a 1 :b 2)  is called a property list.
>
> You can get a value from a property list with GETF.
>
>    (getf list :b)
>
> You can destructively set values like this:
>
>    (setf (getf list :b) 3)
>
> You have basically two versions (other then printing it to
> a string, do a textual change and reread it from the string):
>
> a) traverse the data structure and create new data with
>    the changes made
>
> b) traverse the data structure and change the data
>    destructively
>
> For b) you need a function map-notes
>
> (defun map-notes (function notes)
>    ...)
>
> Which calls a function for each data element.
>
> Then you call that similar to this:
>
> (map-notes (lambda (note)
>              (when (getf (rest note) :notes)
>                (setf (getf (rest note) :notes)
>                      (list 70))))
>            *my-notes*)
>
> (defparameter *my-notes*
>   (copy-tree '(((((4 ((2 :NOTES (60))
>                       (3 ((2 :START-TIME 1.6 :NOTES (60))
>                           (-1 :START-TIME 2.8 :NOTES (60))
>                           (1 :START-TIME 3.4 :NOTES (60))))))
>                   :TIME-SIGNATURE (4 4)))
>                 :INSTRUMENT NIL :STAFF :TREBLE-STAFF))))
>
> ; my attempt to traverse the data-structure
>
> (defun map-notes (function notes)
>   (when (and (consp notes) (symbolp (second notes)))
>     (funcall function notes))
>   (when (consp (first notes))
>     (map-notes function (first notes)))
>   (when (consp (second notes))
>     (map-notes function (rest notes)))
>   notes)
>
> Let's try it:
>
> CL-USER 117 > (pprint *my-notes*)
>
> (((((4
>      ((2 :NOTES (60))
>       (3
>        ((2 :START-TIME 1.6 :NOTES (60))
>         (-1 :START-TIME 2.8 :NOTES (60))
>         (1 :START-TIME 3.4 :NOTES (60))))))
>     :TIME-SIGNATURE
>     (4 4)))
>   :INSTRUMENT
>   NIL
>   :STAFF
>   :TREBLE-STAFF))
>
> CL-USER 118 > (map-notes (lambda (note)
>                            (when (getf (rest note) :notes)
>                              (setf (getf (rest note) :notes) (list 70))))
>                          *my-notes*)
> (((((4 ((2 :NOTES (70)) (3 ((2 :START-TIME 1.6 :NOTES (70)) (-1 :START-TIME 2.8 :NOTES (70)) (1 :START-TIME 3.4 :NOTES (70)))))) :TIME-SIGNATURE (4 4))) :INSTRUMENT NIL :STAFF :TREBLE-STAFF))
>
> CL-USER 119 > (pprint *my-notes*)
>
> (((((4
>      ((2 :NOTES (70))
>       (3
>        ((2 :START-TIME 1.6 :NOTES (70))
>         (-1 :START-TIME 2.8 :NOTES (70))
>         (1 :START-TIME 3.4 :NOTES (70))))))
>     :TIME-SIGNATURE
>     (4 4)))
>   :INSTRUMENT
>   NIL
>   :STAFF
>   :TREBLE-STAFF))
>
> --http://lispm.dyndns.org/

Hi!
Thanks for your help, now is working! the list ist created with pwgl
(a software for composer....)
schöne grüße und besten dank
nadir
From: ··················@gmail.com
Subject: Re: substitute with keywords
Date: 
Message-ID: <4b53fb1f-1803-4283-a1f1-5620d3997003@j35g2000yqh.googlegroups.com>
On Dec 6, 8:59 am, ·············@conservatorio.ch wrote:
> Hallo,
> I'm just starting with common lisp, and have a maybe stupid question.
> How do I substitute the element (60) after the keyword :NOTES in a
> list like this?
>
> (((((4 ((2 :NOTES (60)) (3 ((2 :START-TIME 1.6 :NOTES (60)) (-1 :START-
> TIME 2.8 :NOTES (60)) (1 :START-TIME 3.4 :NOTES (60)))))) :TIME-
> SIGNATURE (4 4))) :INSTRUMENT NIL :STAFF :TREBLE-STAFF))
>
> Thanks for the help!
>
> nadir

May I ask what you are using Lisp for as a person at a music
conservatory? Is it the scripting language for a certain program, or
stochastic composition, or...?
From: ··················@gmail.com
Subject: Re: substitute with keywords
Date: 
Message-ID: <a8f5d05d-00c9-4fbd-ac42-e05d45f14bad@d23g2000yqc.googlegroups.com>
On Dec 7, 10:26 pm, ··················@gmail.com wrote:
> On Dec 6, 8:59 am, ·············@conservatorio.ch wrote:
>
> > Hallo,
> > I'm just starting with common lisp, and have a maybe stupid question.
> > How do I substitute the element (60) after the keyword :NOTES in a
> > list like this?
>
> > (((((4 ((2 :NOTES (60)) (3 ((2 :START-TIME 1.6 :NOTES (60)) (-1 :START-
> > TIME 2.8 :NOTES (60)) (1 :START-TIME 3.4 :NOTES (60)))))) :TIME-
> > SIGNATURE (4 4))) :INSTRUMENT NIL :STAFF :TREBLE-STAFF))
>
> > Thanks for the help!
>
> > nadir
>
> May I ask what you are using Lisp for as a person at a music
> conservatory? Is it the scripting language for a certain program, or
> stochastic composition, or...?

rather it seems my question was answered upon further inspection of
posts...