From mboxrd@z Thu Jan 1 00:00:00 1970 Path: news.gmane.io!.POSTED.blaine.gmane.org!not-for-mail From: Tassilo Horn Newsgroups: gmane.emacs.help Subject: Re: Closures - do you understand them well? Date: Thu, 08 Dec 2022 20:06:59 +0100 Message-ID: <87tu25d77o.fsf@gnu.org> References: <87lenh7vrn.fsf@web.de> Mime-Version: 1.0 Content-Type: text/plain Injection-Info: ciao.gmane.io; posting-host="blaine.gmane.org:116.202.254.214"; logging-data="33557"; mail-complaints-to="usenet@ciao.gmane.io" User-Agent: mu4e 1.9.3; emacs 30.0.50 Cc: help-gnu-emacs@gnu.org To: Michael Heerdegen Original-X-From: help-gnu-emacs-bounces+geh-help-gnu-emacs=m.gmane-mx.org@gnu.org Thu Dec 08 20:30:47 2022 Return-path: Envelope-to: geh-help-gnu-emacs@m.gmane-mx.org Original-Received: from lists.gnu.org ([209.51.188.17]) by ciao.gmane.io with esmtps (TLS1.2:ECDHE_RSA_AES_256_GCM_SHA384:256) (Exim 4.92) (envelope-from ) id 1p3MbX-0008Up-6u for geh-help-gnu-emacs@m.gmane-mx.org; Thu, 08 Dec 2022 20:30:47 +0100 Original-Received: from localhost ([::1] helo=lists1p.gnu.org) by lists.gnu.org with esmtp (Exim 4.90_1) (envelope-from ) id 1p3Mb8-0003Mv-5w; Thu, 08 Dec 2022 14:30:22 -0500 Original-Received: from eggs.gnu.org ([2001:470:142:3::10]) by lists.gnu.org with esmtps (TLS1.2:ECDHE_RSA_AES_256_GCM_SHA384:256) (Exim 4.90_1) (envelope-from ) id 1p3Mb6-0003MZ-4R for help-gnu-emacs@gnu.org; Thu, 08 Dec 2022 14:30:20 -0500 Original-Received: from fencepost.gnu.org ([2001:470:142:3::e]) by eggs.gnu.org with esmtps (TLS1.2:ECDHE_RSA_AES_256_GCM_SHA384:256) (Exim 4.90_1) (envelope-from ) id 1p3Mb5-0003sN-Si; Thu, 08 Dec 2022 14:30:19 -0500 DKIM-Signature: v=1; a=rsa-sha256; q=dns/txt; c=relaxed/relaxed; d=gnu.org; s=fencepost-gnu-org; h=MIME-Version:In-reply-to:Date:Subject:To:From: References; bh=kNUT4UIVjBKttm0mpqog3rBIx6b1wIpREucqfHBUU0Q=; b=ij3sDbgbaGgKU2 B4ONQBxs4wbbQA9gisgCqCRKrjvTaPXnqAX78C/2c+Z/socYmfv49qo2sWFKE934Bo9vQ69xYAaC8 9+aJ0omO5r678NvD0gbAPYLeKG1Q93/BBLem/FUrbg+rOvZu5Nzb3a0yDnmb9v6XMKRmvwvOjfy3w sCeyewX6I7qgMhfMYNPe+B3YzaRgT7hU4QNph1F9NizYiCPmhIwbnd7IiF0Zdk3QmQXo+PxQHpX26 mIoJXDWeasiTTZMAxvfqR5dvp/5YS5K7mFSB5itGntqMQH738ToBCo5XpI3Yfxm3tSxprzRK1Jc3S JhTCcCnxAKMI4poUTpQw==; Original-Received: from auth1-smtp.messagingengine.com ([66.111.4.227]) by fencepost.gnu.org with esmtpsa (TLS1.2:ECDHE_RSA_AES_128_GCM_SHA256:128) (Exim 4.90_1) (envelope-from ) id 1p3Mau-0000Dc-Nw; Thu, 08 Dec 2022 14:30:19 -0500 Original-Received: from compute6.internal (compute6.nyi.internal [10.202.2.47]) by mailauth.nyi.internal (Postfix) with ESMTP id 2F32027C0054; Thu, 8 Dec 2022 14:30:07 -0500 (EST) Original-Received: from mailfrontend1 ([10.202.2.162]) by compute6.internal (MEProxy); Thu, 08 Dec 2022 14:30:07 -0500 X-ME-Sender: X-ME-Received: X-ME-Proxy-Cause: gggruggvucftvghtrhhoucdtuddrgedvhedrvddtgdduvdejucetufdoteggodetrfdotf fvucfrrhhofhhilhgvmecuhfgrshhtofgrihhlpdfqfgfvpdfurfetoffkrfgpnffqhgen uceurghilhhouhhtmecufedttdenucesvcftvggtihhpihgvnhhtshculddquddttddmne cujfgurhepfhgfhffvvefuffgjkfggtgesthdtredttdertdenucfhrhhomhepvfgrshhs ihhlohcujfhorhhnuceothhsughhsehgnhhurdhorhhgqeenucggtffrrghtthgvrhhnpe dvueduueduieevgeduleetgefhudegfeeuffdvueffffdtgeetffduueejhedvtdenucff ohhmrghinhepihhtvghrrdhmrghpnecuvehluhhsthgvrhfuihiivgeptdenucfrrghrrg hmpehmrghilhhfrhhomhepthhhohhrnhdomhgvshhmthhprghuthhhphgvrhhsohhnrghl ihhthidqkeeijeefkeejkeegqdeifeehvdelkedqthhsughhpeepghhnuhdrohhrghesfh grshhtmhgrihhlrdhfmh X-ME-Proxy: Feedback-ID: ib2b94485:Fastmail Original-Received: by mail.messagingengine.com (Postfix) with ESMTPA; Thu, 8 Dec 2022 14:30:06 -0500 (EST) In-reply-to: <87lenh7vrn.fsf@web.de> X-BeenThere: help-gnu-emacs@gnu.org X-Mailman-Version: 2.1.29 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-mx.org@gnu.org Original-Sender: help-gnu-emacs-bounces+geh-help-gnu-emacs=m.gmane-mx.org@gnu.org Xref: news.gmane.io gmane.emacs.help:141466 Archived-At: Michael Heerdegen writes: > What is the return value of the following expression, and what's your > reasoning? Answer without asking Emacs: Obviously 6. > #+begin_src emacs-lisp > ;; -*- lexical-binding: t -*- > > (let ((i 0) > (funs '())) > (while (< (setq i (1+ i)) > 4) > (push (lambda () i) > funs)) > (apply #'+ (mapcar #'funcall funs))) > #+end_src Shocking!!! Common Lisp agrees: --8<---------------cut here---------------start------------->8--- (let ((funs '())) (loop for i from 1 to 3 do (push (lambda () i) funs)) (apply #'+ (mapcar #'funcall funs))) ;=> 12 --8<---------------cut here---------------end--------------->8--- So I guess the reason is that the `i' is not an integer but a place where the integers 1, 2, and 3? Well, at least rust gets this right: --8<---------------cut here---------------start------------->8--- fn main() { let mut funs = vec![]; let mut i: i32 = 1; while i < 4 { funs.push(move || i); i += 1; } let result: i32 = funs.iter().map(|f| f()).sum(); println!("Result = {}", result); // 6, that's right! } --8<---------------cut here---------------end--------------->8--- Bye, Tassilo