From mboxrd@z Thu Jan 1 00:00:00 1970 Path: news.gmane.org!not-for-mail From: Tassilo Horn Newsgroups: gmane.emacs.help Subject: Re: Real-life examples of lexical binding in Emacs Lisp Date: Wed, 17 Jun 2015 21:30:01 +0200 Message-ID: <87twu6xhdy.fsf@gnu.org> References: <87bnh3eqiv.fsf@mbork.pl> <874mmuxyd5.fsf@gnu.org> <87k2v6wmpy.fsf@kuiper.lan.informatimago.com> <87vbendwq5.fsf@debian.uxu> <871thatxro.fsf@kuiper.lan.informatimago.com> <87wpz2sj1u.fsf@kuiper.lan.informatimago.com> <55819E10.4040102@easy-emacs.de> NNTP-Posting-Host: plane.gmane.org Mime-Version: 1.0 Content-Type: text/plain; charset=utf-8 Content-Transfer-Encoding: quoted-printable X-Trace: ger.gmane.org 1434569444 9979 80.91.229.3 (17 Jun 2015 19:30:44 GMT) X-Complaints-To: usenet@ger.gmane.org NNTP-Posting-Date: Wed, 17 Jun 2015 19:30:44 +0000 (UTC) Cc: help-gnu-emacs@gnu.org To: Andreas =?utf-8?Q?R=C3=B6hler?= Original-X-From: help-gnu-emacs-bounces+geh-help-gnu-emacs=m.gmane.org@gnu.org Wed Jun 17 21:30:31 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 1Z5J2c-0000l0-SE for geh-help-gnu-emacs@m.gmane.org; Wed, 17 Jun 2015 21:30:31 +0200 Original-Received: from localhost ([::1]:48825 helo=lists.gnu.org) by lists.gnu.org with esmtp (Exim 4.71) (envelope-from ) id 1Z5J2b-0001MD-Jz for geh-help-gnu-emacs@m.gmane.org; Wed, 17 Jun 2015 15:30:29 -0400 Original-Received: from eggs.gnu.org ([2001:4830:134:3::10]:57995) by lists.gnu.org with esmtp (Exim 4.71) (envelope-from ) id 1Z5J2H-0001Bx-Ei for help-gnu-emacs@gnu.org; Wed, 17 Jun 2015 15:30:15 -0400 Original-Received: from Debian-exim by eggs.gnu.org with spam-scanned (Exim 4.71) (envelope-from ) id 1Z5J2D-0005Nd-Ec for help-gnu-emacs@gnu.org; Wed, 17 Jun 2015 15:30:09 -0400 Original-Received: from out5-smtp.messagingengine.com ([66.111.4.29]:50785) by eggs.gnu.org with esmtp (Exim 4.71) (envelope-from ) id 1Z5J2D-0005ND-9W for help-gnu-emacs@gnu.org; Wed, 17 Jun 2015 15:30:05 -0400 Original-Received: from compute6.internal (compute6.nyi.internal [10.202.2.46]) by mailout.nyi.internal (Postfix) with ESMTP id 007F8211F4 for ; Wed, 17 Jun 2015 15:30:04 -0400 (EDT) Original-Received: from frontend1 ([10.202.2.160]) by compute6.internal (MEProxy); Wed, 17 Jun 2015 15:30:04 -0400 DKIM-Signature: v=1; a=rsa-sha1; c=relaxed/relaxed; d= messagingengine.com; h=cc:content-transfer-encoding:content-type :date:from:in-reply-to:message-id:mime-version:references :subject:to:x-sasl-enc:x-sasl-enc; s=smtpout; bh=3ECFRqb00x+Y5rY A0/1ombachC0=; b=JpJUFvi7y7h1o2z+XDcSRYnGWG1FfBYsZqjhCGwg0pibjur 9F0jPJwCl0f/RKa6hfUSuuDXGlFS0Yl+iD9Pp+pqTKn5Ve0ueeEwbs6jrYxMBNsX ZiE9PH7oTOEREl1Eu/bjn0oprmi2XInP/PRgmXDGCBO5UqimBS0Rwgsuqvtk= X-Sasl-enc: A7ZD4AFLz0Taa8MwJjgsJe3/98KU0kEVAdZVDiroH7QB 1434569404 Original-Received: from thinkpad-t440p (unknown [2.160.171.233]) by mail.messagingengine.com (Postfix) with ESMTPA id F1EF1C00019; Wed, 17 Jun 2015 15:30:03 -0400 (EDT) Mail-Followup-To: Andreas =?utf-8?Q?R=C3=B6hler?= , help-gnu-emacs@gnu.org In-Reply-To: <55819E10.4040102@easy-emacs.de> ("Andreas \=\?utf-8\?Q\?R\=C3\=B6h\?\= \=\?utf-8\?Q\?ler\=22's\?\= message of "Wed, 17 Jun 2015 18:19:28 +0200") User-Agent: Gnus/5.130014 (Ma Gnus v0.14) Emacs/25.0.50 (gnu/linux) X-detected-operating-system: by eggs.gnu.org: GNU/Linux 2.2.x-3.x [generic] X-Received-From: 66.111.4.29 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:105013 Archived-At: Andreas R=C3=B6hler writes: > ;;; -*- lexical-binding: t; -*- > > (defun my-map (f l) > (if l (cons (funcall f (car l)) (my-map f (cdr l))))) > > (defun my-function (input) > (let ((l ())) > (my-map (lambda (x) (unless (memq x l) (error "invalid mapping"))) > input))) > > (my-function '(4 5)) > > Got identic results also without lexical-binding seen here. l is > taken from inside lambda in both modes. You have to byte-compile. Then you get for the lexical version: --8<---------------cut here---------------start------------->8--- byte code for my-function: doc: ... args: 257 0 constant nil 1 constant my-map 2 constant make-byte-code 3 constant 257 4 constant "\211\300>?\205\n.\301\302!\207" 5 constant vconcat 6 constant vector 7 stack-ref 6 9 call 1 10 constant [error "invalid mapping"] 11 call 2 12 constant 3 13 constant "\n\n(fn X)" 14 call 5 15 stack-ref 3 16 call 2 17 return=09=20=20 --8<---------------cut here---------------end--------------->8--- That gives: (my-function '(4 5)) =3D> Lisp error: (error "invalid mapping") For the dynamic version: --8<---------------cut here---------------start------------->8--- byte code for my-function: args: (input) 0 constant nil 1 varbind l 2 constant my-map 3 constant args: (x) 0 varref x 1 varref l 2 memq=20=20=20=20=20=20 3 not=09=20=20=20=20=20=20 4 goto-if-nil-else-pop 1 7 constant error 8 constant "invalid mapping" 9 call 1 10:1 return=20=20=20=20 4 varref input 5 call 2 6 unbind 1 7 return=09=20=20 --8<---------------cut here---------------end--------------->8--- Note the varref l. When the lambda is called in the dynamic version, l will refer to the l argument from my-map. That gives: (my-function '(4 5)) =3D> (nil nil) Bye, Tassilo