From mboxrd@z Thu Jan 1 00:00:00 1970 Path: news.gmane.org!not-for-mail From: Ian Price Newsgroups: gmane.lisp.guile.user Subject: Re: call-with-values and primitives Date: Fri, 11 Jan 2013 15:43:11 +0000 Message-ID: <87mwwfafow.fsf@Kagami.home> References: <24558.128.117.43.92.1357914200.squirrel@lavabit.com> NNTP-Posting-Host: plane.gmane.org Mime-Version: 1.0 Content-Type: text/plain; charset=iso-2022-jp X-Trace: ger.gmane.org 1357919009 12797 80.91.229.3 (11 Jan 2013 15:43:29 GMT) X-Complaints-To: usenet@ger.gmane.org NNTP-Posting-Date: Fri, 11 Jan 2013 15:43:29 +0000 (UTC) Cc: guile-user@gnu.org To: bromley@lavabit.com Original-X-From: guile-user-bounces+guile-user=m.gmane.org@gnu.org Fri Jan 11 16:43:47 2013 Return-path: Envelope-to: guile-user@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 1Ttgll-0008Ei-Hy for guile-user@m.gmane.org; Fri, 11 Jan 2013 16:43:45 +0100 Original-Received: from localhost ([::1]:56785 helo=lists.gnu.org) by lists.gnu.org with esmtp (Exim 4.71) (envelope-from ) id 1TtglV-00058W-KT for guile-user@m.gmane.org; Fri, 11 Jan 2013 10:43:29 -0500 Original-Received: from eggs.gnu.org ([208.118.235.92]:41293) by lists.gnu.org with esmtp (Exim 4.71) (envelope-from ) id 1TtglO-0004ww-UE for guile-user@gnu.org; Fri, 11 Jan 2013 10:43:24 -0500 Original-Received: from Debian-exim by eggs.gnu.org with spam-scanned (Exim 4.71) (envelope-from ) id 1TtglM-0003i9-6h for guile-user@gnu.org; Fri, 11 Jan 2013 10:43:22 -0500 Original-Received: from mail-wi0-f178.google.com ([209.85.212.178]:43014) by eggs.gnu.org with esmtp (Exim 4.71) (envelope-from ) id 1TtglM-0003hz-1A for guile-user@gnu.org; Fri, 11 Jan 2013 10:43:20 -0500 Original-Received: by mail-wi0-f178.google.com with SMTP id hn3so1111716wib.5 for ; Fri, 11 Jan 2013 07:43:18 -0800 (PST) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=gmail.com; s=20120113; h=x-received:from:to:cc:subject:references:date:in-reply-to :message-id:user-agent:mime-version:content-type; bh=2J4yAyO5yVl11ybP0UsWvhZbW/hrgXDXff8vEq4xg+Y=; b=ROqTHQjumtupQU+fRS32iiNAOEdPoDLJHkixKsZrxdFDZZk/DHZhDsRnkVE2Wn+Dg8 LRcrYHsEnCBQ2DWpo9xD6wTLH8SObbx4QBfp5QiSxyPehBAtESZc6pHQdOEjNkpc/T83 lykfcTQOsZL8dTw34TLzqQs5EfeEtY/zyXWDmdV7JiQFJxfzVYBpRS4Q70TJUSqc5A/S hkrInqWtuwQmhX+7vYDdis8VulPvDy113OyxJ6b2YVKRkcFlEDeIuujScigQE5t8md6d ckFYUR4G2P5cBjSRBjqaLTgkdTUVIJsj0kZb/ofYAYoIiz48L9sxmmgIAJ2LAUgaYQwn OuhQ== X-Received: by 10.180.93.41 with SMTP id cr9mr15456424wib.19.1357918998634; Fri, 11 Jan 2013 07:43:18 -0800 (PST) Original-Received: from Kagami.home (host81-159-201-90.range81-159.btcentralplus.com. [81.159.201.90]) by mx.google.com with ESMTPS id bz12sm7797594wib.5.2013.01.11.07.43.16 (version=TLSv1.2 cipher=RC4-SHA bits=128/128); Fri, 11 Jan 2013 07:43:17 -0800 (PST) In-Reply-To: <24558.128.117.43.92.1357914200.squirrel@lavabit.com> (bromley@lavabit.com's message of "Fri, 11 Jan 2013 09:23:20 -0500 (EST)") User-Agent: Gnus/5.13 (Gnus v5.13) Emacs/23.3 (gnu/linux) X-detected-operating-system: by eggs.gnu.org: GNU/Linux 3.x [fuzzy] X-Received-From: 209.85.212.178 X-BeenThere: guile-user@gnu.org X-Mailman-Version: 2.1.14 Precedence: list List-Id: General Guile related discussions List-Unsubscribe: , List-Archive: List-Post: List-Help: List-Subscribe: , Errors-To: guile-user-bounces+guile-user=m.gmane.org@gnu.org Original-Sender: guile-user-bounces+guile-user=m.gmane.org@gnu.org Xref: news.gmane.org gmane.lisp.guile.user:9842 Archived-At: bromley@lavabit.com writes: > Hello. > > Why does it return -1? Could anyone explain? > (Comments are mine.) The general principle is to try and give sensible results when +,*,etc are called with a number of arguments other than 2. By thinking them through at this level you can, in theory, write more general code and have it "just work". Not only does it apply to various functions, but it is generally considered a sensible macro design principle too, as in the case of, among others, 'and' and 'or'. > > (call-with-values (lambda () (values 4 5)) > (lambda (a b) b)) ; a is 4 and b is 5; return 5 > ⇒ 5 > > (call-with-values * -) > ⇒ -1 If you call * with 0 arguments, it returns one value, the number 1. This makes some sense since 1 is the identity for multiplication. i.e. (* 1 x) = x = (* x 1) for all x If you call - with one argument, it negates that argument, as if you had done (- 0 x). Putting both of these together, (call-with-values * -) is equivalent to (- (*)) scheme@(guile−user)> (*) $2 = 1 scheme@(guile−user)> (- 1) $3 = −1 scheme@(guile−user)> (- (*)) $4 = −1 > (call-with-values + +) > 0 Similarly to multiplication, if you call (+) with one argument, you get the identity for +, which is 0. This expression then, is equivalent to (+ (+)) = (+ 0) = 0 > (call-with-values + -) > 0 Similar to the above situation, you can think of this as being equivalent to (- (+)). Negating 0, naturally enough, gives you 0. scheme@(guile−user)> (+) $5 = 0 scheme@(guile−user)> (- (+)) $6 = 0 > (call-with-values - -) > ERROR: Wrong number of arguments to - While we extend / and - to have sensible results for the one argument case (reciprocal and negation), and we extend it for more than two arguments, there is not an identity for / or - [0], so we mark the zero argument case as errors for both. scheme@(guile−user)> (/) ERROR: In procedure /: ERROR: Wrong number of arguments to / scheme@(guile−user)> (-) ERROR: In procedure −: ERROR: Wrong number of arguments to − [0] Well, more correctly, they have right identities, of 1 and 0 respectively, but no left ones -- Ian Price -- shift-reset.com "Programming is like pinball. The reward for doing it well is the opportunity to do it again" - from "The Wizardy Compiled"