help-gnu-emacs
[Top][All Lists]
Advanced

[Date Prev][Date Next][Thread Prev][Thread Next][Date Index][Thread Index]

Re: why are there [v e c t o r s] in Lisp?


From: Pascal J. Bourguignon
Subject: Re: why are there [v e c t o r s] in Lisp?
Date: Sat, 17 Oct 2015 18:34:29 +0200
User-agent: Gnus/5.13 (Gnus v5.13) Emacs/24.3 (gnu/linux)

Javier <nospam@nospam.com> writes:

> Emanuel Berg <embe8573@student.uu.se> wrote:
>> "Pascal J. Bourguignon" <pjb@informatimago.com>
>> writes:
>> 
>>> Obviously not.
>>>
>>> And linear algebra deals with matrices and tensors
>>> etc, which are arrays!
>> 
>> They are not called arrays but matrices so there is no
>> confusing the data structure array with the linear
>> algebra concept matrix.
>
> If you want to avoid confusions with linear algebra you can use the
> word 'tuples' instead of 'vectors', like in Python.
>
> https://en.wikipedia.org/wiki/Tuple
>
> One question: is a Python tuple exactly the same as a Lisp vector?

Well, the tuple notion is tained, notably by some purely functional
programming language, where only a single argument can be passed to a
function (and therefore multiple arguments are packed into a single
tuple), and where multiple resulting values are similarly packed into a
single tlupe to have a single value result.


In emacs lisp and CL, we have multiple argument functions, and if you
consider a vector to be a tuple, you cannot pass it easily to functions:

    (apply (function +) (coerce [1 2 3] 'list))

when you'd just write +[1 2 3] in some language.

And in CL we have multiple values results, but they're not a tuple,
since multiple values are not reified:

  (let (q r)
     (setf (values q r) (truncate 10 3))  
     (list q r))
  --> (3 1)

In emacs lisp, there's no such thing.  
With (require 'cl), values is actually list:

    (values 1 2 3) --> (1 2 3)

which you can handle with destructuring-bind:

    (destructuring-bind (q r) (values (truncate 10 3) (rem* 10 3))
       (list q r))
    --> (3 1)

but this still doesn't make a tuple, and there's no help from the
language to deal with vector results.

So a Python tuple is not like a lisp vector at all.
(but nothing prevents you to implement in lisp something like Python
tuples and using vectors to represent them).


(setf lexical-binding t)

(defmacro tuple (&rest arguments) `(vector ,@arguments))

(defmacro define-tupled-function (name parameters &rest body)
  (let ((tuple (gensym)))
    `(defun ,name (,tuple)
       (let (,@(let ((index -1))
                 (mapcar (lambda (parameter)
                           (list parameter `(aref ,tuple ,(incf
                                                           index))))
                         parameters)))
         ,@body))))

(defmacro tuple-setq (var-tuple val-tuple)
  (let ((tuple (gensym)))
    `(let ((,tuple ,val-tuple))
       ,@(map 'list (let ((index -1))
                      (lambda (var)
                        `(setq ,var (aref ,tuple ,(incf index)))))
           var-tuple))))

(define-tupled-function addsub (a b) (tuple (+ a b) (- a b)))

(let (s d)
  (tuple-setq [s d] (addsub [2 3]))
  (list s d))
;; --> (5 -1)

(addsub [2 3])                   --> [5 -1]
(addsub (addsub [2 3]))          --> [4 6]
(addsub (addsub (addsub [2 3]))) --> [10 -2]

-- 
__Pascal Bourguignon__                 http://www.informatimago.com/
“The factory of the future will have only two employees, a man and a
dog. The man will be there to feed the dog. The dog will be there to
keep the man from touching the equipment.” -- Carl Bass CEO Autodesk


reply via email to

[Prev in Thread] Current Thread [Next in Thread]