all messages for Emacs-related lists mirrored at yhetil.org
 help / color / mirror / code / Atom feed
From: "Pascal J. Bourguignon" <pjb@informatimago.com>
To: help-gnu-emacs@gnu.org
Subject: Re: why are there [v e c t o r s] in Lisp?
Date: Sat, 17 Oct 2015 18:34:29 +0200	[thread overview]
Message-ID: <87oafxbgiy.fsf@kuiper.lan.informatimago.com> (raw)
In-Reply-To: mvtrbs$oc1$1@speranza.aioe.org

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


  parent reply	other threads:[~2015-10-17 16:34 UTC|newest]

Thread overview: 56+ messages / expand[flat|nested]  mbox.gz  Atom feed  top
2015-10-16  1:12 why are there [v e c t o r s] in Lisp? Emanuel Berg
2015-10-17  1:11 ` Aurélien Aptel
2015-10-17  4:22   ` Emanuel Berg
2015-10-17  7:58   ` Jude DaShiell
2015-10-19 16:33     ` Nick Dokos
     [not found]   ` <mailman.490.1445055179.7904.help-gnu-emacs@gnu.org>
2015-10-17 15:09     ` Pascal J. Bourguignon
     [not found] ` <mailman.488.1445044303.7904.help-gnu-emacs@gnu.org>
2015-10-17  2:22   ` Pascal J. Bourguignon
2015-10-17  4:56     ` Emanuel Berg
2015-10-17  5:49       ` Barry Margolin
2015-10-17 15:04         ` Emanuel Berg
     [not found]         ` <mailman.506.1445093727.7904.help-gnu-emacs@gnu.org>
2015-10-17 15:20           ` Pascal J. Bourguignon
2015-10-17 15:38             ` Emanuel Berg
     [not found]             ` <mailman.511.1445095810.7904.help-gnu-emacs@gnu.org>
2015-10-17 16:01               ` Javier
2015-10-17 16:03                 ` Javier
2015-10-17 16:34                 ` Pascal J. Bourguignon [this message]
2015-10-17 21:18                   ` Emanuel Berg
2015-10-17 16:15               ` Pascal J. Bourguignon
2015-10-17 21:37                 ` Emanuel Berg
2015-10-17 15:18       ` Pascal J. Bourguignon
     [not found] <mailman.428.1444957396.7904.help-gnu-emacs@gnu.org>
2015-10-16  1:51 ` Pascal J. Bourguignon
2015-10-16  2:31   ` Emanuel Berg
2015-10-16  2:29     ` Random832
2015-10-16  2:51       ` Emanuel Berg
2015-10-16  2:56         ` Random832
2015-10-16 23:30           ` Emanuel Berg
     [not found]           ` <mailman.482.1445037713.7904.help-gnu-emacs@gnu.org>
2015-10-17  1:55             ` Pascal J. Bourguignon
2015-10-17  4:47               ` Emanuel Berg
     [not found]               ` <mailman.494.1445057637.7904.help-gnu-emacs@gnu.org>
2015-10-17 15:25                 ` Pascal J. Bourguignon
2015-10-17 21:12                   ` Emanuel Berg
     [not found]                   ` <mailman.519.1445115776.7904.help-gnu-emacs@gnu.org>
2015-10-18  1:08                     ` Pascal J. Bourguignon
     [not found]         ` <mailman.432.1444964227.7904.help-gnu-emacs@gnu.org>
2015-10-16  3:57           ` Pascal J. Bourguignon
2015-10-16  4:17             ` Random832
     [not found]             ` <mailman.434.1444970033.7904.help-gnu-emacs@gnu.org>
2015-10-16  5:16               ` Pascal J. Bourguignon
     [not found]   ` <mailman.429.1444962165.7904.help-gnu-emacs@gnu.org>
2015-10-16  3:31     ` Pascal J. Bourguignon
2015-10-16 23:46       ` Emanuel Berg
     [not found]       ` <mailman.483.1445038647.7904.help-gnu-emacs@gnu.org>
2015-10-17  2:04         ` Pascal J. Bourguignon
2015-10-17  4:40           ` Random832
2015-10-17  5:00             ` Emanuel Berg
2015-10-17  4:40           ` Emanuel Berg
2015-10-17  5:56           ` Barry Margolin
2015-10-17 15:06             ` Emanuel Berg
     [not found]           ` <mailman.491.1445056308.7904.help-gnu-emacs@gnu.org>
2015-10-17  5:53             ` Barry Margolin
2015-10-17 15:16             ` Pascal J. Bourguignon
2015-10-17 21:06               ` Emanuel Berg
     [not found]               ` <mailman.518.1445115463.7904.help-gnu-emacs@gnu.org>
2015-10-18  1:07                 ` Pascal J. Bourguignon
2015-10-18 12:32                   ` Emanuel Berg
     [not found]                   ` <mailman.551.1445171034.7904.help-gnu-emacs@gnu.org>
2015-10-18 12:55                     ` Pascal J. Bourguignon
2015-10-18 14:28                       ` Emanuel Berg
2015-10-18 21:17                         ` Robert Thorpe
     [not found]                       ` <mailman.557.1445177952.7904.help-gnu-emacs@gnu.org>
2015-10-18 19:48                         ` Barry Margolin
2015-10-18 21:25                           ` Emanuel Berg
2015-10-18 21:39                             ` Random832
2015-10-19  0:46                         ` Pascal J. Bourguignon
2015-10-16 13:32 ` Barry Margolin
2015-10-16 23:47   ` Emanuel Berg
     [not found] <mailman.581.1445203060.7904.help-gnu-emacs@gnu.org>
2015-10-19  0:45 ` Pascal J. Bourguignon

Reply instructions:

You may reply publicly to this message via plain-text email
using any one of the following methods:

* Save the following mbox file, import it into your mail client,
  and reply-to-all from there: mbox

  Avoid top-posting and favor interleaved quoting:
  https://en.wikipedia.org/wiki/Posting_style#Interleaved_style

* Reply using the --to, --cc, and --in-reply-to
  switches of git-send-email(1):

  git send-email \
    --in-reply-to=87oafxbgiy.fsf@kuiper.lan.informatimago.com \
    --to=pjb@informatimago.com \
    --cc=help-gnu-emacs@gnu.org \
    /path/to/YOUR_REPLY

  https://kernel.org/pub/software/scm/git/docs/git-send-email.html

* If your mail client supports setting the In-Reply-To header
  via mailto: links, try the mailto: link
Be sure your reply has a Subject: header at the top and a blank line before the message body.
Code repositories for project(s) associated with this external index

	https://git.savannah.gnu.org/cgit/emacs.git
	https://git.savannah.gnu.org/cgit/emacs/org-mode.git

This is an external index of several public inboxes,
see mirroring instructions on how to clone and mirror
all data and code used by this external index.