scheme memory address

classic Classic list List threaded Threaded
13 messages Options
Reply | Threaded
Open this post in threaded view
|

scheme memory address

Gianmaria Lari
Suppose I write 

#(define x '(1 2 3))

is there any way in scheme to print the memory address where x is pointing to? (where is allocated the first element of the list)

Thank you, g.

_______________________________________________
lilypond-user mailing list
[hidden email]
https://lists.gnu.org/mailman/listinfo/lilypond-user
Reply | Threaded
Open this post in threaded view
|

Re: scheme memory address

David Kastrup
Gianmaria Lari <[hidden email]> writes:

> Suppose I write
>
>
> #(define x '(1 2 3))
>
>
> is there any way in scheme to print the memory address where x is pointing
> to? (where is allocated the first element of the list)

What do you need it for?  If it is for identification, (hashq x
1000000000) should usually do a reasonably good job.

--
David Kastrup

_______________________________________________
lilypond-user mailing list
[hidden email]
https://lists.gnu.org/mailman/listinfo/lilypond-user
Reply | Threaded
Open this post in threaded view
|

Re: scheme memory address

Gianmaria Lari


On Tue, 9 Apr 2019 at 10:45, David Kastrup <[hidden email]> wrote:
Gianmaria Lari <[hidden email]> writes:

> Suppose I write
>
>
> #(define x '(1 2 3))
>
>
> is there any way in scheme to print the memory address where x is pointing
> to? (where is allocated the first element of the list)

What do you need it for?  If it is for identification, (hashq x
1000000000) should usually do a reasonably good job.

I tried, it looks working. (Yes, it's for identification)

And is there any way to print the memory address of x? (If I remember correctly was something like &x in c++).

Thank you, g.



_______________________________________________
lilypond-user mailing list
[hidden email]
https://lists.gnu.org/mailman/listinfo/lilypond-user
Reply | Threaded
Open this post in threaded view
|

Re: scheme memory address

David Kastrup
Gianmaria Lari <[hidden email]> writes:

> On Tue, 9 Apr 2019 at 10:45, David Kastrup <[hidden email]> wrote:
>
>> Gianmaria Lari <[hidden email]> writes:
>>
>> > Suppose I write
>> >
>> >
>> > #(define x '(1 2 3))
>> >
>> >
>> > is there any way in scheme to print the memory address where x is
>> pointing
>> > to? (where is allocated the first element of the list)
>>
>> What do you need it for?  If it is for identification, (hashq x
>> 1000000000) should usually do a reasonably good job.
>>
>
> I tried, it looks working. (Yes, it's for identification)
>
> And is there any way to print the memory address of x? (If I remember
> correctly was something like &x in c++).

object-address maybe.  But it's not like you can use it for anything.

--
David Kastrup

_______________________________________________
lilypond-user mailing list
[hidden email]
https://lists.gnu.org/mailman/listinfo/lilypond-user
Reply | Threaded
Open this post in threaded view
|

Re: scheme memory address

Gianmaria Lari


On Tue, 9 Apr 2019 at 11:13, David Kastrup <[hidden email]> wrote:
Gianmaria Lari <[hidden email]> writes:

> On Tue, 9 Apr 2019 at 10:45, David Kastrup <[hidden email]> wrote:
>
>> Gianmaria Lari <[hidden email]> writes:
>>
>> > Suppose I write
>> >
>> >
>> > #(define x '(1 2 3))
>> >
>> >
>> > is there any way in scheme to print the memory address where x is
>> pointing
>> > to? (where is allocated the first element of the list)
>>
>> What do you need it for?  If it is for identification, (hashq x
>> 1000000000) should usually do a reasonably good job.
>>
>
> I tried, it looks working. (Yes, it's for identification)
>
> And is there any way to print the memory address of x? (If I remember
> correctly was something like &x in c++).

object-address maybe.  But it's not like you can use it for anything.


If I didn't make any mistakes, this code print the "address" of the first element of the list x and the "address" of the first element pointed by the parameter "lst" of the "foo" function.

\version "2.21.0"
#(define x '(1 2 3))
#(define (foo lst) (display (hashq lst 1000000000))) 
 
#(display (hashq x 1000000000))#(newline)
#(foo x)
 
The two instructions

#(display (hashq x 1000000000))#(newline)
#(foo x)

print the same value because foo is called with x as argument and x and lst refer to the same address.

I wanted to print the address of the variable x and then the address of the parameter lst just to show that x and lst have different address (so  x is passed by value. I could infer the same assigning to lst a new value and see that at the exit of the function x didn't get update). I hope I didn't say nothing wrong.

Best regards, g.



_______________________________________________
lilypond-user mailing list
[hidden email]
https://lists.gnu.org/mailman/listinfo/lilypond-user
Reply | Threaded
Open this post in threaded view
|

Re: scheme memory address

Andrew Bernard
Hi Gianmaria,

You are trying to use Scheme as though it were C. It isn't. I'd really like you to read the book I referred you to recently.

If you really must learn about how pointers are used underneath everything in Scheme (and which you do not need to know about for programming, mostly), this text is heavily oriented around explaining that aspect:


Most other text refer to locations, rather than using the terminology of pointers.

There are many predicates in Scheme for testing all the types of equivalences of variables. Worth reading the manual.

I have in the past suggested we have another mailing list for the use of scheme in lilypond, but that did not meet general approval. I still think it's a good idea, as I am a bit reluctant to clod the list with very technical Scheme discussions, which is where this is leading.

Andrew






_______________________________________________
lilypond-user mailing list
[hidden email]
https://lists.gnu.org/mailman/listinfo/lilypond-user
Reply | Threaded
Open this post in threaded view
|

Re: scheme memory address

David Kastrup
In reply to this post by Gianmaria Lari
Gianmaria Lari <[hidden email]> writes:

> On Tue, 9 Apr 2019 at 11:13, David Kastrup <[hidden email]> wrote:
>
>> Gianmaria Lari <[hidden email]> writes:
>>
>> > On Tue, 9 Apr 2019 at 10:45, David Kastrup <[hidden email]> wrote:
>> >
>> >> Gianmaria Lari <[hidden email]> writes:
>> >>
>> >> > Suppose I write
>> >> >
>> >> >
>> >> > #(define x '(1 2 3))
>> >> >
>> >> >
>> >> > is there any way in scheme to print the memory address where x is
>> >> pointing
>> >> > to? (where is allocated the first element of the list)
>> >>
>> >> What do you need it for?  If it is for identification, (hashq x
>> >> 1000000000) should usually do a reasonably good job.
>> >>
>> >
>> > I tried, it looks working. (Yes, it's for identification)
>> >
>> > And is there any way to print the memory address of x? (If I remember
>> > correctly was something like &x in c++).
>>
>> object-address maybe.  But it's not like you can use it for anything.
>
>
> If I didn't make any mistakes, this code print the "address" of the first
> element of the list x and the "address" of the first element pointed by the
> parameter "lst" of the "foo" function.

This is Scheme, not C.  1 would not have an address anyway since it is
an immediate value (self-represented).  And you are not printing the
address of the first element of the list but you are printing the
address of its cons cell.

> \version "2.21.0"
> #(define x '(1 2 3))
> #(define (foo lst) (display (hashq lst 1000000000)))
>
> #(display (hashq x 1000000000))#(newline)
> #(foo x)
>
>
> The two instructions
>
>
> #(display (hashq x 1000000000))#(newline)
> #(foo x)
>
>
> print the same value because foo is called with x as argument and x and lst
> refer to the same address.

This is mostly nonsense since it tries to assign some C specific meaning
to Scheme values.

> I wanted to print the address of the variable x and then the address
> of the parameter lst just to show that x and lst have different
> address (so x is passed by value. I could infer the same assigning to
> lst a new value and see that at the exit of the function x didn't get
> update).

And that's complete nonsense since it tries to infer internals like
function passing semantics from purported internals.  Scheme semantics
are not defined by machine-level C analogies but by the Scheme standard.
The whole data and memory model and its management is completely
different to that of C (even though Guile implements it using C and some
fairly weird stack and heap scanning techniques).

If you are trying to interpret Scheme-provided functions as authoritive
proof about the implementation, you are deluding yourself.

> I hope I didn't say nothing wrong.

It's more in the "not even wrong" category.  The behavior of a Scheme
interpreter is determined by the standard.  The kind of stuff you try to
infer is not up to the implementation, so trying to deduce anything from
what you think you can derive about the implementation is putting the
cart before the wrong horse.

--
David Kastrup

_______________________________________________
lilypond-user mailing list
[hidden email]
https://lists.gnu.org/mailman/listinfo/lilypond-user
Reply | Threaded
Open this post in threaded view
|

Re: scheme memory address

Gianmaria Lari


On Tue, 9 Apr 2019 at 17:07, David Kastrup <[hidden email]> wrote:
Gianmaria Lari <[hidden email]> writes:

> On Tue, 9 Apr 2019 at 11:13, David Kastrup <[hidden email]> wrote:
>
>> Gianmaria Lari <[hidden email]> writes:
>>
>> > On Tue, 9 Apr 2019 at 10:45, David Kastrup <[hidden email]> wrote:
>> >
>> >> Gianmaria Lari <[hidden email]> writes:
>> >>
>> >> > Suppose I write
>> >> >
>> >> >
>> >> > #(define x '(1 2 3))
>> >> >
>> >> >
>> >> > is there any way in scheme to print the memory address where x is
>> >> pointing
>> >> > to? (where is allocated the first element of the list)
>> >>
>> >> What do you need it for?  If it is for identification, (hashq x
>> >> 1000000000) should usually do a reasonably good job.
>> >>
>> >
>> > I tried, it looks working. (Yes, it's for identification)
>> >
>> > And is there any way to print the memory address of x? (If I remember
>> > correctly was something like &x in c++).
>>
>> object-address maybe.  But it's not like you can use it for anything.
>
>
> If I didn't make any mistakes, this code print the "address" of the first
> element of the list x and the "address" of the first element pointed by the
> parameter "lst" of the "foo" function.

This is Scheme, not C.  1 would not have an address anyway since it is
an immediate value (self-represented).  And you are not printing the
address of the first element of the list but you are printing the
address of its cons cell.
[...]

Thank you David and Andrew for your extended reply and your suggestions!

g.



_______________________________________________
lilypond-user mailing list
[hidden email]
https://lists.gnu.org/mailman/listinfo/lilypond-user
Reply | Threaded
Open this post in threaded view
|

Re: scheme memory address

Carl Sorensen-3
In reply to this post by Gianmaria Lari

 

 

From: Gianmaria Lari <[hidden email]>
Date: Tuesday, April 9, 2019 at 8:17 AM
To: David Kastrup <[hidden email]>
Cc: lilypond-user <[hidden email]>
Subject: Re: scheme memory address

 

I wanted to print the address of the variable x and then the address of the parameter lst just to show that x and lst have different address (so  x is passed by value. I could infer the same assigning to lst a new value and see that at the exit of the function x didn't get update). I hope I didn't say nothing wrong.

 

 

Here's a stack overflow discussion about the various equal predicates in Scheme.  If you want to find out if two variables are the same object in memory, use eq? or eqv?, depending on the type of data the variables are (i.e., the data that the symbol is bound to).

 

https://stackoverflow.com/questions/16299246/what-is-the-difference-between-eq-eqv-equal-and-in-scheme

 

I think understanding it would help you.

 

Thanks,


Carl

 

 


_______________________________________________
lilypond-user mailing list
[hidden email]
https://lists.gnu.org/mailman/listinfo/lilypond-user
Reply | Threaded
Open this post in threaded view
|

Re: scheme memory address

David Kastrup
Carl Sorensen <[hidden email]> writes:

> From: Gianmaria Lari <[hidden email]>
> Date: Tuesday, April 9, 2019 at 8:17 AM
> To: David Kastrup <[hidden email]>
> Cc: lilypond-user <[hidden email]>
> Subject: Re: scheme memory address
>
> I wanted to print the address of the variable x and then the address
> of the parameter lst just to show that x and lst have different
> address (so x is passed by value. I could infer the same assigning to
> lst a new value and see that at the exit of the function x didn't get
> update). I hope I didn't say nothing wrong.
>
>
> Here's a stack overflow discussion about the various equal predicates
> in Scheme.  If you want to find out if two variables are the same
> object in memory, use eq? or eqv?, depending on the type of data the
> variables are (i.e., the data that the symbol is bound to).
>
> https://stackoverflow.com/questions/16299246/what-is-the-difference-between-eq-eqv-equal-and-in-scheme
>
> I think understanding it would help you.

If you want to find out if two variables are at the same place in memory
(if at all), just use eq? .  The problem is that its output is
unspecified when talking about numerical values exactly because there is
no guarantee that non-immediate numbers share or not share a memory
location even when their pedigree would suggest they are identical.  eq?
will tell you if they do which is neither dependable nor useful in
Scheme.

--
David Kastrup

_______________________________________________
lilypond-user mailing list
[hidden email]
https://lists.gnu.org/mailman/listinfo/lilypond-user
Reply | Threaded
Open this post in threaded view
|

RE: scheme memory address

Andrew Bernard
In reply to this post by Gianmaria Lari

Hi Gianmaria,

 

It’s time to stop and take stock here. It’s clear you have some background in programming, but it is even more clear that you have not studied Lisp and Scheme, except in a passing manner. The Lisp family of languages is fundamentally different from languages like C. It takes a considerable mental effort to grasp the fullness of Lisp and Scheme, but the effort is repaid as there will come a day when you suddenly realise how it all hangs together, and you will never program the same way again. There’s a long history of programmers reporting such an epiphany after studying Lisp for a while, and your outlook is changed forever. It is often said that learning Lisp makes you a better programmer even if you never program in Lisp.

 

At this point, rather than engaging in long winded corrections of your approach, I would really like you to ‘reset’ (after all, this topic started about resetting lists…) and read SICP, Structured Interpretation of Computer Programs by Abelson and Sussman of MIT. It is the definitive introductory text, and I have a hard copy at my desk always. You can study the online version here, the second edition.

 

https://mitpress.mit.edu/sites/default/files/sicp/index.html

 

The book beautifully typeset here:

 

https://web.mit.edu/alexmv/6.037/sicp.pdf

 

At the language level in Lisp and Scheme, there are no pointers. It’s critical to realise this, and important. Obviously the language uses pointers and memory locations internally, because that’s what computers are, but it’s vital that you don’t try to second guess how Scheme works internally, because you will certainly be wrong. The point of Scheme and Lisp is that they increase the level of abstraction we are dealing with, and this is their power. The low level machine issues are abstracted away from you, in order to give you vastly greater power. C is very low in the level of abstraction hierarchy, and is closer to assembler in spirit, with all the access to memory you want, and all the consequent disasters. Scheme relieves you of this.

 

I’d just like to direct you to the answer here in this post on Stack Overflow:

 

https://stackoverflow.com/questions/38277997/what-does-location-means-in-r6rs-of-scheme

 

“The crucial point here is that in languages like Lisp pointers are (and should remain) “invisible” to the user (the programmer).”

 

Your questions may actually be better addressed on Stack Overflow, as they are mostly about Scheme rather than lilypond specific – the point being, there is a lot of help there on languages.

 

Next, and I think this is even more important than chat about pointers. You would benefit from studying Functional Programming. Scheme supports imperative programming but it is also a good functional programming language. Studying this style of computer language will again change the way you think forever. You will study immutable data and how that completely changes the way you program. Haskell is the most pure functional language and every object in Haskell is immutable. Think about that for a minute. You cant change variables but you can actually write programs? Indeed. [I’d recommend studying Haskell too as it will inform you Scheme programming.]

 

Next, I detected a hint in your thread that you would like Scheme to be object oriented. It’s not. Don’t go there. OO is not the panacea it was once thought to be. So it is also wise not to attempt to mould Scheme into an OO model. [Yes, there are dialects that provide OO, but in the context of lilypond, we have Scheme.]

 

Another great learning resource with truly superb documentation is Racket, a Scheme variant. Now at release 7.2 and very mature. It’s a really good environment for learning Scheme, even though it is a greatly extended language.

 

There’s a lot to be said here Gianmaria. I think the outcome here is you will either abandon completely or go on to be a really master Scheme programmer. Hopefully the latter!

 

In my next mail, I will outline an approach to your original question in a way that uses Scheme in an idiomatic way.

 

Andrew

 

 

 

 

 

 

 


_______________________________________________
lilypond-user mailing list
[hidden email]
https://lists.gnu.org/mailman/listinfo/lilypond-user
Reply | Threaded
Open this post in threaded view
|

RE: scheme memory address

Andrew Bernard
In reply to this post by Gianmaria Lari

Hi Gianmaria,

 

Your requirement is to have a numbered list, similar to that in outlines. Basically what you want is a counter, and this can be implemented in Lisp like languages with a closure. A closure results from the fact that in Scheme a function records its environment, and this includes variables in scope. Scheme variables have ‘infinite extent’ and so are kept forever as long as there is a function that references them. I am not going to give a tutorial on closures here, but I will give you an indication.

 

Functional programming shows you that you need to think about ‘state’ very carefully. I won’t go into that here. But a closure effectively produces a function with private state, so a counter for example can remember its value between successive invocations. You want a counter that can remember its value and its ‘indent level’.

 

Here’s a counter that can take an initial value. This is Scheme, not lilypond. Use the guile REPL to try it out.

 

(define make-counter-init

  (lambda (count)

    (lambda ()

      (let ((val count))

                (set! count (+ count 1))

                val

                ))))

 

 

This returns a procedure (the lambda) that counts:

 

(define n1 (make-counter-init 1))

 

then:

 

(n1)

1

(n1)

2

 

But the main point is that instances are independent and contain their own state:

 

(define n2 (make-counter-init 100))

 

(n2)

100

(n2)

101

(n1)

3

(n1)

4

(n2)

102

 

Obviously it is trivial to write something like this that converts numbers to formatted strings. I did not show that in the interests of clarity.

 

I am not going to give a lecture on closures here – that’s an exercise for you.

 

My concept is to elaborate this so that part of the state is the indent level, and we can provide optional arguments to indent and outdent and even ‘reset’ (aha!_ the counter. I will work that up for you. It’s actually a very interesting programming exercise and would make a good student task.

 

 

Andrew

 

 

 

 


_______________________________________________
lilypond-user mailing list
[hidden email]
https://lists.gnu.org/mailman/listinfo/lilypond-user
Reply | Threaded
Open this post in threaded view
|

Re: scheme memory address

Gianmaria Lari
In reply to this post by Andrew Bernard
On Wed, 10 Apr 2019 at 00:44, Andrew Bernard <[hidden email]> wrote:

Hi Gianmaria,

 

It’s time to stop and take stock here.


Thank you Andrew for your message with your suggestions (and also for the other one with the code). Give me some time to go through it. I sincerely appreciate your help.

Best regards!!!!



_______________________________________________
lilypond-user mailing list
[hidden email]
https://lists.gnu.org/mailman/listinfo/lilypond-user