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: Fri, 16 Oct 2015 05:31:35 +0200 Organization: Informatimago Message-ID: <87eggvebfs.fsf@kuiper.lan.informatimago.com> References: <87mvvjeg29.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 1444966531 15911 80.91.229.3 (16 Oct 2015 03:35:31 GMT) X-Complaints-To: usenet@ger.gmane.org NNTP-Posting-Date: Fri, 16 Oct 2015 03:35:31 +0000 (UTC) To: help-gnu-emacs@gnu.org Original-X-From: help-gnu-emacs-bounces+geh-help-gnu-emacs=m.gmane.org@gnu.org Fri Oct 16 05:35:27 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 1Zmvni-0006Qe-Vi for geh-help-gnu-emacs@m.gmane.org; Fri, 16 Oct 2015 05:35:27 +0200 Original-Received: from localhost ([::1]:50672 helo=lists.gnu.org) by lists.gnu.org with esmtp (Exim 4.71) (envelope-from ) id 1Zmvnh-00058e-RB for geh-help-gnu-emacs@m.gmane.org; Thu, 15 Oct 2015 23:35:25 -0400 Original-Path: usenet.stanford.edu!fu-berlin.de!uni-berlin.de!individual.net!not-for-mail Original-Newsgroups: gnu.emacs.help Original-Lines: 172 Original-X-Trace: individual.net 5D/S0VO0BCrFIXiAl9rV/wFC9Zqf9I+c3DF55BSIX1/dAG+VoZ Cancel-Lock: sha1:OTUxMzAxNzcwNjAzNjg4ZTk3MzBjYTZmZDVjNmM2OWRlYzg5YzVhZA== sha1:GG9X6DiXIPAUPoZYV/aTk70k1Jw= 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:215386 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:107670 Archived-At: Emanuel Berg writes: > "Pascal J. Bourguignon" > writes: > >>> Why is there a special syntax for vectors? >> >> To make it easy to introduce literal vectors in >> your programs. > > OK, if you do it all day long that is an advantage > just as I wouldn't want to do strings as lists, not > that I use that much strings - however there is a flaw > to the analogy and that is: having a string instead as > a list isn't nearly as readable or editable as is > a string - compare > > (print "message") > (print '(m e s s a g e)) > > while I don't see any such difference with [1 2 3] and > '(1 2 3)! This is not what you asked above. You asked: Why is there a special syntax for vectors? What you have to compare is: "message" with: (load-time-value (let ((string (make-string 7))) (setf (aref string 0) ?m (aref string 1) ?m (aref string 2) ?m (aref string 3) ?m (aref string 4) ?m (aref string 5) ?m (aref string 6) ?m) string)) Now, concerning the use of lists vs. strings, you answered the question of why the special syntax yourself. to make it more readable. Notice that in early lisp, there was no character and no string, only lists and symbols. Characters were represented by single-character symbols, and strings by symbols. There were functions implode and explode: (defun implode (charsyms) (intern (map 'string (lambda (sym) (aref (symbol-name sym) 0)) charsyms))) (defun explode (sym) (map 'list (lambda (char) (intern (string char))) (symbol-name sym))) (explode 'message) ; --> (m e s s a g e) (implode '(h e l l o \ w o r l d)) ; --> hello\ world > Perhaps if you did a special syntax highlight for the > squared vectors that would make them more visible and > easily detected along with all the other parenthesised > code - again, only if you have tons of vectors this > would make for any practical difference. (It might > look cool tho.) Vector in general are vectors of t, that is they can take any type of element. Strings are vectors of character. Having a distinct syntax here let you avoid the risk of error introducing a non-character in the literal vector. >> Without this syntax, you would only have run-time >> constructors, and you would have to write more >> complex code. > > ... why? If lists are vectors, which they are in terms > of what they hold and how they look, then you don't > need more code compared to vectors, on the contrary > you need less code! I'm assumed that you didn't ask the question you wanted to ask too. There are DIFFERENT TYPES. As much as possible, for pre-defined types, lisp provides different literal syntaxes, so that you can introduce in your programs literal objects of those different types. I showed in all my examples why having such literal syntax is so useful: because constructing objects of those types yourself takes a lot more of code. >> For example, the equivalent of: >> >> (defun permut-0213 (x) >> (aref [0 2 1 3] x)) >> >> would have to be written as: >> >> (defun permut-0213 (x) >> (aref (load-time-value (vector 0 2 1 3)) x)) > > What I mean is, the list '(0 2 1 3) is already > a vector, why not just leave it at that? Right but this is not the question you asked. You asked about literal syntax, I'm answering about literal syntax. >> Therefore a list such as (1 2 3 4 5) will be >> actually a cons cell: (1 . (2 . (3 . (4 . (5 . >> nil))))) > > Indeed, but the same argument as I just made for > strings can be applied here as well: > > 1) Strings and lists are so common so they should look > their best, which is why we can't have strings > lists or lists cons cells, because then they don't > even look like what they are. > > 2) Vectors are not that common, but just because > something is less common doesn't mean it should be > treated worse, fine - still, '(1 2 3) doesn't look > any worse than [1 2 3] - even in math books the > square brackets are sometimes not square, but > parenthesis (denoting lists, ordered n-paris, or > vectors!). > > The rest of the post I appreciate and especially the > the ASCII figures. The rest of the post answers to the question why there are different types. Now, nothing prevents you to travel back to 1960, and ignore those additionnal types, using only lists, symbol and numbers. Notice by the way that in emacs lisp, we lack structure types. We can still implement them using vectors (or lists), and write program using the structure abstraction. If you want to write programs using vector or string abstractions without using actual vector or string type objects you can do so. You might have to avoid a few modern libraries, but it would be no problem to rewrite the required library functions using only the list type: this is 1960 technology. (defstruct point x y) (make-point :x 2 :y 3) ;; --> [cl-struct-point 2 3] (defstruct (vect (:type list)) x y) (make-vect :x 2 :y 3) ;; --> (2 3) (defstruct (vect3 (:type list) :named) x y z) (make-vect3 :x 2 :y 3 :z 4) ;; --> (vect3 2 3 4) -- __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