🚀 go-pugleaf

RetroBBS NetNews Server

Inspired by RockSolid Light RIP Retro Guy

3 total messages Started by Michael S Sat, 10 Oct 2026 19:28
Re: Rust (was: Optimization by programmers)
#31
Author: Michael S
Date: Sat, 10 Oct 2026 19:28
74 lines
3972 bytes
On Tue, 06 Oct 2026 08:29:19 GMT
anton@mips.complang.tuwien.ac.at (Anton Ertl) wrote:

> cross@spitfire.i.gajendra.net (Dan Cross) writes:
> >In article <2026Oct3.131214@mips.complang.tuwien.ac.at>,
> >Anton Ertl <anton@mips.complang.tuwien.ac.at> wrote:  
> >>Tim Rentsch <tr.17687@z991.linuxsc.com> writes:  
> >>>That's good to know but what is a good resource to learn the Rust
> >>>language?  I've looked at the Rust book several times and each
> >>>time come away rather frustrated.  
> >>
> >>If you mean "The Rust Programming Language", I came away from it
> >>disillusioned.
> >>
> >>The reason was that I could not get a search-tree insertion
> >>technique to work that can be implemented in e.g., Pascal, C, and
> >>Forth, but not in Java (because in Java I cannot produce references
> >>to a field inside an object).  
> >
> >I think what you mean to say is that you couldn't find a way to
> >implement that in the manner you were accustomed to thinking
> >about the problem, given your prior experience.  
> 
> My goal was to implement search tree insertion as efficiently and
> compactly as in Pascal, 

I suppose, you mean some extended Pascal dialect here, because I can not
imagine how you code an example below in Standard Pascal. 
Then again, I learned Pascal ~40 years ago and didn't use it in
practice since 1989 or 1990, so can easily miss something.

I would think that it is possible to code your example in Standard
Ada83, but not in "checked" subset of it.

> C or Forth (and in contrast to Java), ideally
> in safe Rust.  Here it is in C (untested):
> 
> struct tree {
>   int key; /* or some other key type */
>   int val; /* or some other value type */
>   struct tree *left;
>   struct tree *right;
> };
> 
> struct tree **search(struct tree **rootp, int key)
> {
>   while ((root=*rootp)!=NULL && key != root->key) {
>     if (key < root->key)
>       rootp = &(root->left);
>     else
>       rootp = &(root->right);
>   }
>   return rootp;
> }
> 
> void insert(struct tree **rootp, int key, int val)
> {
>   struct tree **nodep = **search(rootp, key);
>   struct tree *node = *nodep;
>   if (node == NULL) {
>     node = malloc(sizeof(tree));
>     node->key = key;
>     node->left = NULL;
>     node->right = NULL;
>     *nodep = node;
>   }
>   node->val = val;
> }
> 
> Passing a reference to a mutable field is what is done in these
> languages, and I don't know an efficient and similarly compact
> technique that does not pass a reference to a mutable field.
> 
Re: Rust (was: Optimization by programmers)
#35
Author: Anton Ertl
Date: Sun, 11 Oct 2026 09:27
70 lines
3561 bytes
Michael S <already5chosen@yahoo.com> writes:
>On Tue, 06 Oct 2026 08:29:19 GMT
>anton@mips.complang.tuwien.ac.at (Anton Ertl) wrote:
>> My goal was to implement search tree insertion as efficiently and
>> compactly as in Pascal, 
>
>I suppose, you mean some extended Pascal dialect here, because I can not
>imagine how you code an example below in Standard Pascal. 
>Then again, I learned Pascal ~40 years ago and didn't use it in
>practice since 1989 or 1990, so can easily miss something.

Page 75 and 76 of my copy of "Programming in Modula-2" (Second Edition)
contain this:

TreePtr = POINTER TO TreeNode;
TreeNode =
  Record
    key: CARDINAL;
    data: ...
    left, right: TreePtr;
  END;

PROCEDURE search(VAR p: TreePtr; x: CARDINAL): TreePtr;
BEGIN
  IF p # NIL THEN
    IF p^.key < x THEN
      RETURN search(p^.right,x)
    ELSIF p^.key > x THEN
      RETURN search(p^.left, x)
    ELSE
      RETURN p
    END
  ELSE (* not found, hence insert *)
    NEW(p);
    WITH p^ DO
      key := x;
      left := NIL;
      right := NIL;
    END;
    RETURN p
  END
END search

You would invoke this with

search(root,needle)^.data := ...;

in order to actually change the field "data", performing the insert
functionality.

One would not want to use this procedure for lookup, given that it
inserts a node with an uninitialized data field when the key is not
found.  Wirth gives a loop implementation of lookup before the
PROCEDURE search.

Note that the "VAR" in front of the p makes p a reference parameter
rather than a value parameter (like x), which is key to making this
work for insertion.  This language feature is also present in Pascal;
the Pascal syntax is different, but I don't think that prevents
implementing this approach.

Using recursion is necessary here because Modula-2 and Pascal support
references to variables or fields only for parameters.  But the
recursive calls are tail-recursive (I don't think that Wirth's
Modula-2 or Parcal compilers optimized tail recursion, though).

- anton
-- 
'Anyone trying for "industrial quality" ISA should avoid undefined behavior.'
  Mitch Alsup, <c17fcd89-f024-40e7-a594-88a85ac10d20o@googlegroups.com>
Re: Rust (was: Optimization by programmers)
#40
Author: Michael S
Date: Sun, 11 Oct 2026 19:43
78 lines
4002 bytes
On Sun, 11 Oct 2026 09:27:04 GMT
anton@mips.complang.tuwien.ac.at (Anton Ertl) wrote:

> Michael S <already5chosen@yahoo.com> writes:
> >On Tue, 06 Oct 2026 08:29:19 GMT
> >anton@mips.complang.tuwien.ac.at (Anton Ertl) wrote:  
> >> My goal was to implement search tree insertion as efficiently and
> >> compactly as in Pascal,   
> >
> >I suppose, you mean some extended Pascal dialect here, because I can
> >not imagine how you code an example below in Standard Pascal. 
> >Then again, I learned Pascal ~40 years ago and didn't use it in
> >practice since 1989 or 1990, so can easily miss something.  
> 
> Page 75 and 76 of my copy of "Programming in Modula-2" (Second
> Edition) contain this:
> 
> TreePtr = POINTER TO TreeNode;
> TreeNode =
>   Record
>     key: CARDINAL;
>     data: ...
>     left, right: TreePtr;
>   END;
> 
> PROCEDURE search(VAR p: TreePtr; x: CARDINAL): TreePtr;
> BEGIN
>   IF p # NIL THEN
>     IF p^.key < x THEN
>       RETURN search(p^.right,x)
>     ELSIF p^.key > x THEN
>       RETURN search(p^.left, x)
>     ELSE
>       RETURN p
>     END
>   ELSE (* not found, hence insert *)
>     NEW(p);
>     WITH p^ DO
>       key := x;
>       left := NIL;
>       right := NIL;
>     END;
>     RETURN p
>   END
> END search
> 
> You would invoke this with
> 
> search(root,needle)^.data := ...;
> 
> in order to actually change the field "data", performing the insert
> functionality.
> 
> One would not want to use this procedure for lookup, given that it
> inserts a node with an uninitialized data field when the key is not
> found.  Wirth gives a loop implementation of lookup before the
> PROCEDURE search.
> 
> Note that the "VAR" in front of the p makes p a reference parameter
> rather than a value parameter (like x), which is key to making this
> work for insertion.  This language feature is also present in Pascal;
> the Pascal syntax is different, but I don't think that prevents
> implementing this approach.
> 
> Using recursion is necessary here because Modula-2 and Pascal support
> references to variables or fields only for parameters.  But the
> recursive calls are tail-recursive (I don't think that Wirth's
> Modula-2 or Parcal compilers optimized tail recursion, though).
> 
> - anton


Thank you. I forgot about call by reference feature of Pascal.
But even if I was not forgetting about it, using recursive function
call to emulate & operator of C probably would not cross my mind.


Thread Navigation

This is a paginated view of messages in the thread with full content displayed inline.

Messages are displayed in chronological order, with the original post highlighted in green.

Use pagination controls to navigate through all messages in large threads.

Back to All Threads