From mboxrd@z Thu Jan 1 00:00:00 1970 Path: news.gmane.org!not-for-mail From: "Pascal J. Bourguignon" Newsgroups: gmane.emacs.help Subject: Re: why are there [v e c t o r s] in Lisp? Date: Sat, 17 Oct 2015 18:34:29 +0200 Organization: Informatimago Message-ID: <87oafxbgiy.fsf@kuiper.lan.informatimago.com> References: <87oafzpqfj.fsf@debian.uxu> <87lhb2cjy5.fsf@kuiper.lan.informatimago.com> <876126w0t8.fsf@debian.uxu> <871tctcyj3.fsf@kuiper.lan.informatimago.com> NNTP-Posting-Host: plane.gmane.org Mime-Version: 1.0 Content-Type: text/plain; charset=utf-8 Content-Transfer-Encoding: 8bit X-Trace: ger.gmane.org 1445099741 13784 80.91.229.3 (17 Oct 2015 16:35:41 GMT) X-Complaints-To: usenet@ger.gmane.org NNTP-Posting-Date: Sat, 17 Oct 2015 16:35:41 +0000 (UTC) To: help-gnu-emacs@gnu.org Original-X-From: help-gnu-emacs-bounces+geh-help-gnu-emacs=m.gmane.org@gnu.org Sat Oct 17 18:35:22 2015 Return-path: Envelope-to: geh-help-gnu-emacs@m.gmane.org Original-Received: from lists.gnu.org ([208.118.235.17]) by plane.gmane.org with esmtp (Exim 4.69) (envelope-from ) id 1ZnUS1-0002nZ-QK for geh-help-gnu-emacs@m.gmane.org; Sat, 17 Oct 2015 18:35:21 +0200 Original-Received: from localhost ([::1]:59058 helo=lists.gnu.org) by lists.gnu.org with esmtp (Exim 4.71) (envelope-from ) id 1ZnUS1-0000H6-67 for geh-help-gnu-emacs@m.gmane.org; Sat, 17 Oct 2015 12:35:21 -0400 Original-Path: usenet.stanford.edu!fu-berlin.de!uni-berlin.de!individual.net!not-for-mail Original-Newsgroups: gnu.emacs.help Original-Lines: 101 Original-X-Trace: individual.net nmKKrrZngThIfwm1LsCq6g3KZ7/sX24O5FwJaJnGDv9QN2ROHv Cancel-Lock: sha1:NWU3MGMxN2RlYjBiZDc1MDczMjgwZTIwNDhhNTI3MWI3NDFiYzAwZQ== sha1:lE4o1AVjpbo0Eu7yG0eCLoyYhzA= Face: iVBORw0KGgoAAAANSUhEUgAAADAAAAAwAQMAAABtzGvEAAAABlBMVEUAAAD///+l2Z/dAAAA oElEQVR4nK3OsRHCMAwF0O8YQufUNIQRGIAja9CxSA55AxZgFO4coMgYrEDDQZWPIlNAjwq9 033pbOBPtbXuB6PKNBn5gZkhGa86Z4x2wE67O+06WxGD/HCOGR0deY3f9Ijwwt7rNGNf6Oac l/GuZTF1wFGKiYYHKSFAkjIo1b6sCYS1sVmFhhhahKQssRjRT90ITWUk6vvK3RsPGs+M1RuR mV+hO/VvFAAAAABJRU5ErkJggg== X-Accept-Language: fr, es, en User-Agent: Gnus/5.13 (Gnus v5.13) Emacs/24.3 (gnu/linux) Original-Xref: usenet.stanford.edu gnu.emacs.help:215434 X-BeenThere: help-gnu-emacs@gnu.org X-Mailman-Version: 2.1.14 Precedence: list List-Id: Users list for the GNU Emacs text editor List-Unsubscribe: , List-Archive: List-Post: List-Help: List-Subscribe: , Errors-To: help-gnu-emacs-bounces+geh-help-gnu-emacs=m.gmane.org@gnu.org Original-Sender: help-gnu-emacs-bounces+geh-help-gnu-emacs=m.gmane.org@gnu.org Xref: news.gmane.org gmane.emacs.help:107718 Archived-At: Javier writes: > Emanuel Berg wrote: >> "Pascal J. Bourguignon" >> 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