From mboxrd@z Thu Jan 1 00:00:00 1970 Return-Path: X-Original-To: caml-list@sympa.inria.fr Delivered-To: caml-list@sympa.inria.fr Received: from mail4-relais-sop.national.inria.fr (mail4-relais-sop.national.inria.fr [192.134.164.105]) by sympa.inria.fr (Postfix) with ESMTPS id 3B1207EE1A for ; Fri, 30 Nov 2012 10:54:06 +0100 (CET) Received-SPF: None (mail4-smtp-sop.national.inria.fr: no sender authenticity information available from domain of gabriel.scherer@gmail.com) identity=pra; client-ip=209.85.223.182; receiver=mail4-smtp-sop.national.inria.fr; envelope-from="gabriel.scherer@gmail.com"; x-sender="gabriel.scherer@gmail.com"; x-conformance=sidf_compatible Received-SPF: Pass (mail4-smtp-sop.national.inria.fr: domain of gabriel.scherer@gmail.com designates 209.85.223.182 as permitted sender) identity=mailfrom; client-ip=209.85.223.182; receiver=mail4-smtp-sop.national.inria.fr; envelope-from="gabriel.scherer@gmail.com"; x-sender="gabriel.scherer@gmail.com"; x-conformance=sidf_compatible; x-record-type="v=spf1" Received-SPF: None (mail4-smtp-sop.national.inria.fr: no sender authenticity information available from domain of postmaster@mail-ie0-f182.google.com) identity=helo; client-ip=209.85.223.182; receiver=mail4-smtp-sop.national.inria.fr; envelope-from="gabriel.scherer@gmail.com"; x-sender="postmaster@mail-ie0-f182.google.com"; x-conformance=sidf_compatible X-IronPort-Anti-Spam-Filtered: true X-IronPort-Anti-Spam-Result: Ah8BACyBuFDRVd+2mGdsb2JhbABErUqSOwgWDgEBAQEBCAkNBxQngh4BAQQBJxkBGxILAQMBCwYFCwMKDSEhAQERAQUBChIGExIJh2IBAwkGDKBljDOCeoULChknAwpZiHUBBQyLS2mEQQOSToFdgVWBHIgugWqDMBYphBQ X-IronPort-AV: E=Sophos;i="4.84,191,1355094000"; d="scan'208";a="164285710" Received: from mail-ie0-f182.google.com ([209.85.223.182]) by mail4-smtp-sop.national.inria.fr with ESMTP/TLS/RC4-SHA; 30 Nov 2012 10:54:02 +0100 Received: by mail-ie0-f182.google.com with SMTP id s9so627454iec.27 for ; Fri, 30 Nov 2012 01:54:01 -0800 (PST) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=gmail.com; s=20120113; h=mime-version:in-reply-to:references:from:date:message-id:subject:to :cc:content-type; bh=9MQRgGKXWRPBFY1QpG0mdScKvhr3BA+QphZ7/D6aNoY=; b=gW339Xgv7LAFkhYvA30NJ0dAlCkAStFGY00/0aHxSKU/D6Vn0ISqiqNOw8/l+e+N1Y RhetiIGhkZNn+EbpdRe4Jz12rfJRE1XAgmvhxdYUgUWkzPwWa8Fc8Idlk/ScCp/Z3fCz Lw5IbyBoeEkeyPOXikndHlhgPuWYUTG1kSWKjxfkf1kp6CIxT77GcIcHDjGnByTuRdXW uJie5Vl8OKM496XaOZ6bky5fI74xdlqcTe86GlnMGpA41wQ1QPVTppayPle2tSnwzYqT jGAtyVyATczLKQAjWPVp7ZQqSHdhjvwkRGUhV4EB1tg0HgJcQwfBPv1VhEvVcEayRPOh 2HFg== Received: by 10.50.202.73 with SMTP id kg9mr28445328igc.51.1354269241922; Fri, 30 Nov 2012 01:54:01 -0800 (PST) MIME-Version: 1.0 Received: by 10.50.184.199 with HTTP; Fri, 30 Nov 2012 01:53:21 -0800 (PST) In-Reply-To: <50B822A6.7000504@emu-bark.com> References: <50B822A6.7000504@emu-bark.com> From: Gabriel Scherer Date: Fri, 30 Nov 2012 10:53:21 +0100 Message-ID: To: William Smith Cc: Caml-list Content-Type: text/plain; charset=ISO-8859-1 Subject: Re: [Caml-list] List.fold_left vs. Hashtbl.fold > Before I saw other people's explanations, I was thinking about how the > parameter order fold_left is more useful for currying. This is a correct observation. It is the reason why, in Haskell, both foldl and foldr request the initial accumulator before the list. I personally think it is a mistake because you break the nice property that you're lifting a function-on-an-argument (either ('elem -> 'acc -> 'acc) or ('acc -> 'elem -> 'acc)) to a function-on-the-list with the exact same typing structure. Designing for currying is a fragile thing to do in the general case (you can be wrong on which argument happens to be the more often delayed) and creates tensions with other surface API design choice (such as putting the function argument last for syntactic convenience). My personal opinion is that currying is overrated and we should think about language design without currying, and with a nice short syntax for partial application (inspired by Scala's admittedly dubious (foo _ bar) syntax for (fun x -> foo x bar)). Labels also provide a solution for this that is usable in OCaml APIs today (but I think for the Language of the Future we could forget labels and use a flexible notion of records to provide similar naming and order-irrelevance) -- just remember to avoid optional arguments that have other downsides. On Fri, Nov 30, 2012 at 4:06 AM, William Smith wrote: > That seems like an easy mnemonic--left means the base case argument is on > the left and right means that the base case is on the right. This is both > for the parameters to fold_left/right and the parameters of its > argument. And then, if there is not _right/_left distinction available for > a container, it defaults to being equivalent to _right. > > Before I saw other people's explanations, I was thinking about how the > parameter order fold_left is more useful for currying. > > If you want to create an operator, fold_left makes it a lot easier to curry > in its base case and then apply the operator to different lists that the > operator is processing last. > > If I wanted to do that with fold_right or the other container's folds, I > would have to write a wrapper to swap the arguments--which isn't really a > big deal, but it's a way that fold_left is more useful that no one > mentioned. > > Bill > >> On Wed, Nov 28, 2012 at 6:25 PM, Gabriel Scherer >> wrote: > > >> That's for the type. Regarding the implementation, fold_left applies >> the argument function starting from "the left" (the beginning) of the >> list first, while fold_right begins "on the right" (at the end): for >> some infix operator (++) : >> fold_left (++) init [x; y; z] is ((init ++ x) ++ y) ++ z >> fold_right (++) [x; y; z] init is (x ++ (y ++ (z ++ init))) >> (The evaluation "begins" in the innermost nested parentheses) > > > On Wed, Nov 28, 2012 at 5:40 AM, William Smith wrote: > >> Hashtbl.fold expects the Hasthbl as the second parameter with the 3rd >> parameter being the initial value... just the opposite of List.fold_left. > > > -- > Caml-list mailing list. Subscription management and archives: > https://sympa.inria.fr/sympa/arc/caml-list > Beginner's list: http://groups.yahoo.com/group/ocaml_beginners > Bug reports: http://caml.inria.fr/bin/caml-bugs