LISP, lingual analysis and multiple object references
In a meantime I think about a knowledge database creation, which could work like a long-term memory.
I want to parse a text and build a weighted graph of words (actually their roots only) connected into sentences or lingual blocks. The first task I want to complete is a simple replacement of the pronouns with the appropriate nounses from the previous blocks.
But while thinking about this idea I stumbled upon a non-trivial problem with LISP and object pointers. It is a simple task to have a single object to be referenced from multiple other data structures – we can allocate memory and have pointers stored in multiple objects (I will leave object removal and reference counter problems for now).
But in LISP I basically do not know how to do this. Or more simple task: how to have the same data structure (namely LISP object) to be referenced, accessible and editable from two lists for example.
In LISP I only know how to push an object into list, but not its pointer. Here is an example:
[1]> (defparameter *test-list* (list 1 2 3))
*TEST-LIST*
[2]> (defparameter *storage-list* (list))
*STORAGE-LIST*
[3]> (defparameter *another-list* (list))
*ANOTHER-LIST*
[4]> (push *test-list* *storage-list*)
((1 2 3))
[5]> (push *test-list* *another-list*)
((1 2 3))
[6]> (push "string" *test-list*)
("string" 1 2 3)
[7]> *storage-list*
((1 2 3))
[8]> *another-list*
((1 2 3))
I added *test-list* into *storage-list* and *another-list*, but actually I added its copy, since added object modification was not represented in the destination storages.
With object it is a bit simple – we do can modify it in-place, but still I do not know how to make it being referenced from multiple storages (like lists).
And thus I do not know how to implement a weighted graph, where each word is supposed to be linked and referenced to/from multiple other words. Plus each object should be accessible from the indexed data structure (like hash table or tree) for fast access.
Any ideas how to implement multiple references in LISP? I can switch to some other language of course, but I like idea to make this in LISP :)
Elliptics network got extended statistics support New elliptics version and new features.
Comments are currently closed.

Here’s a messy way to do it
- Erik Nomitch (enomitch at gmail dot com)
(defvar *foo* '(1 2 3)) (defvar *bar* '(4 5 6)) (defvar *foo-bar* '(*foo* *bar*)) (defun read-foo-bar () (mapcar #'(lambda (element) (if (and (symbolp element) (boundp element)) (eval element) element)) *foo-bar*))Look at http://www.lispworks.com/documentation/lw51/CLHS/Body/f_rplaca.htm
for rplaca and rplacd. They replace parts of the list (technicallly
parts of a “cons”, so they don’t work on the null list).
(defparameter *test-list* (list 1 2 3))
(defparameter *storage-list* (list 999))
(rplaca *storage-list* *test-list*)
((1 2 3))
(rplaca *test-list* 7)
(7 2 3)
*test-list*
(7 2 3)
*storage-list*
((7 2 3))
I don’t think this is the best way to do what you want however.
I don’t understand your application well enough, but I’ve been
programming lisp for years and I have rarely used rplaca and
rplacd. You might want to get on #lisp and ask.
BTW I think it’s pretty uncommon to write LISP these days;
most people write lisp or Lisp.
Liam
The problem is actually that “push” is a macro for (setf *test-list* (cons “string” *test-list*)). The easy solution is to stick an extra cons cell on the start of the list, and use its cdr instead of the variable.
(defvar *test-list* (list ‘tag 1 2 3))
(defvar *storage-list* (list))
(push *test-list* *storage-list*)
(push “string” (cdr *test-list*))
(map cdr *storage-list*)
(n.b., I only really know the syntax and library of Scheme, so my example may not be quite right; the design works, though).
The problem you’re having is, while you are sharing the same list object between *test-list* and *storage-list* initially, your attempt to modify the list actually only modified the variable, not the list. In lisp-like languages (and Java), a variable is a single pointer, so there isn’t a ton of copying, but you need an object that is a pointer (so you can change it and affect other variables). There’s no plain pointer object (at least in Scheme), but cons cells are two pointers. You can use one of them as your pointer, and stick something arbitrary in the other.