From mboxrd@z Thu Jan 1 00:00:00 1970 Return-Path: Received: from mail2-relais-roc.national.inria.fr (mail2-relais-roc.national.inria.fr [192.134.164.83]) by c5ff346549e7 (Postfix) with ESMTPS id 383BC5D6 for ; Sat, 10 Aug 2019 20:34:41 +0000 (UTC) X-IronPort-AV: E=Sophos;i="5.64,370,1559512800"; d="scan'208,217";a="395056512" Received: from sympa.inria.fr ([193.51.193.213]) by mail2-relais-roc.national.inria.fr with ESMTP; 10 Aug 2019 22:34:40 +0200 Received: by sympa.inria.fr (Postfix, from userid 20132) id 4EF557ED20; Sat, 10 Aug 2019 22:34:40 +0200 (CEST) Received: from mail3-relais-sop.national.inria.fr (mail3-relais-sop.national.inria.fr [192.134.164.104]) by sympa.inria.fr (Postfix) with ESMTPS id EA9AD7ED20 for ; Sat, 10 Aug 2019 22:34:34 +0200 (CEST) Authentication-Results: mail3-smtp-sop.national.inria.fr; spf=None smtp.pra=murthy.chet@gmail.com; spf=Pass smtp.mailfrom=murthy.chet@gmail.com; spf=None smtp.helo=postmaster@mail-lj1-f176.google.com IronPort-PHdr: =?us-ascii?q?9a23=3A3mbHERWHtnivDn1jPrnrhsbLCrHV8LGtZVwlr6E/?= =?us-ascii?q?grcLSJyIuqrYbBSCt8tkgFKBZ4jH8fUM07OQ7/m6HzVZsN3Y7CFKWacPfidNsd?= =?us-ascii?q?8RkQ0kDZzNImzAB9muURYHGt9fXkRu5XCxPBsdMs//Y1rPvi/6tmZKSV3wOgVv?= =?us-ascii?q?O+v6BJPZgdip2OCu4Z3TZBhDiCagbb9oIxi6sBvdutMLjYd/Lqs9xQbFr3RMdu?= =?us-ascii?q?9L2W5mOFWfkgrm6Myt5pBj6SNQu/wg985ET6r3erkzQKJbAjo7LW07/dXnuhbf?= =?us-ascii?q?QwSB4HscSXgWnQFTAwfZ9hH6X4z+vTX8u+FgxSSVJ8z2TbQzWTS/86dmTQLjhS?= =?us-ascii?q?kbOzIl9mzcl9d9h7xHrh2/uxN/wpbUYICLO/p4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j?= =?us-ascii?q?EI2r?= X-IronPort-Anti-Spam-Filtered: true X-IronPort-Anti-Spam-Result: =?us-ascii?q?A0AFAAB0KU9dhrDQVdFmGwEBAQEDAQEBB?= =?us-ascii?q?wMBAQGBUwYBAQELAYQIKoQegR2CXoQhhwuCD4ltdI4vgXsJAQMBDC8BAYQ/AoJ?= =?us-ascii?q?hGwcBBDAJDgEEAQEEAQEEAQMHARMBAQEICwsIKYUuDII6KQGCZgEBAQECASMdA?= =?us-ascii?q?RsdAQMBCwYFBAcNKgICIgERAQUBHAYBEoMigWoBAw4Pnz08iyOBFQUBF4J7BYR?= =?us-ascii?q?LChknDWKBPQIHEoEiAYtjghaEIz6HT4JYBJVclH5tBwKCH4IgkhIbc5dKjVWYH?= =?us-ascii?q?A8hgS+CEDMaI1AxgjuCQhqDWIpzIjCQJAEB?= X-IPAS-Result: =?us-ascii?q?A0AFAAB0KU9dhrDQVdFmGwEBAQEDAQEBBwMBAQGBUwYBAQE?= =?us-ascii?q?LAYQIKoQegR2CXoQhhwuCD4ltdI4vgXsJAQMBDC8BAYQ/AoJhGwcBBDAJDgEEA?= =?us-ascii?q?QEEAQEEAQMHARMBAQEICwsIKYUuDII6KQGCZgEBAQECASMdARsdAQMBCwYFBAc?= =?us-ascii?q?NKgICIgERAQUBHAYBEoMigWoBAw4Pnz08iyOBFQUBF4J7BYRLChknDWKBPQIHE?= =?us-ascii?q?oEiAYtjghaEIz6HT4JYBJVclH5tBwKCH4IgkhIbc5dKjVWYHA8hgS+CEDMaI1A?= =?us-ascii?q?xgjuCQhqDWIpzIjCQJAEB?= X-IronPort-AV: E=Sophos;i="5.64,370,1559512800"; d="scan'208,217";a="316097627" X-MGA-submission: =?us-ascii?q?MDEPpo3chSIqWJX2i0d6E6BzJfO9uGSfVYrG0E?= =?us-ascii?q?ZPeP3QigG6UDF0Z0vW7BA+8/r+ZB+IDU6E4+7lC/Glg7SQqqvD6F2Mwr?= =?us-ascii?q?jvs6e4oVeX3p3p2kNKdMI3/Qvpr12vaR0K6F6rc11A0TNP9Dtj0a4310?= =?us-ascii?q?zU1aZ541HTX3Chr22tRNAgVw=3D=3D?= Received: from mail-lj1-f176.google.com ([209.85.208.176]) by mail3-smtp-sop.national.inria.fr with ESMTP/TLS/AES128-GCM-SHA256; 10 Aug 2019 22:34:33 +0200 Received: by mail-lj1-f176.google.com with SMTP id m8so61500197lji.7 for ; Sat, 10 Aug 2019 13:34:33 -0700 (PDT) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=gmail.com; s=20161025; h=mime-version:references:in-reply-to:from:date:message-id:subject:to :cc; bh=GV+p+Mu9cEHHVmmz09lOaV3+pjVZydXquLRt+N0kARU=; b=OZPknOxdM38+RCJIvmKQQzYa4DzdNYCWIps+zu1b4oLfxhRA2G7gDJsdvXhdVC0k5P iRbHUFfoaC03XBQDkhcUpdjo3ezjUkSH1ZZI+7Is6NF/zI6IHQr/EDVASjPOL+xp7lf9 VuO8Aufoo/6XMI9HqyNzHH+d9S5K8S8v2i3WqLIoU1vvkh6YFxVeo8IsOd7E+MYk9COF wKXlpBmEpf01tvzNQVpmOfW+9hFbUGYb4/Yoom80K+pM7G/4Ij2rOYg0V5lMJEp/hhda GUlMadvGxjvcTKpMn+va2Lk50IqkL9Ckyw8aUGAsFa+B91owBFEVNkmAXzt+TedO6OVb 79ag== X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20161025; h=x-gm-message-state:mime-version:references:in-reply-to:from:date :message-id:subject:to:cc; bh=GV+p+Mu9cEHHVmmz09lOaV3+pjVZydXquLRt+N0kARU=; b=YeRkDkAT+GgUfrg2kNjUOGFNhyhBgzn7BWgSWZoiZOeY8Rw7rz9GQnvoCK6MMhcp1i K5vMLWDwXGkWrSIbgvZhGGeYN/XPZrvrHir9PCPKBo4YVTk5eFWyepwbRBZ6akaOYxbQ kVZbSywoILoKR/CsW+MYMeryRmDlEEXTLL/oOUNnzJ4V4sA8c6xpACpqksSILJ1Z61AS BcTKRsN1NGWXWMDSa8z8R+jRM/RKH6s42fvrT6RJU+FKIZ9OwFmR+iQROuBODIY3MEnx SqzKxiQb9Ll8+o6/J69HZfhLQEye0l2sa7PFPbOc2roqSYU9oF7Z6YsbMSvRBnnBOt92 NN/Q== X-Gm-Message-State: APjAAAVDdN0Di1GK/tFBVuOFUrGsxnijJZa06Uw5WyWuBrQwkzjEcdxc 1OeT0M/l8u3tUGJovlV60nwVRXZTfA5TruD8WgVg4gns X-Google-Smtp-Source: APXvYqzkMyuIUOOk7IZ5PzqTDEitV1WE2H56IzRxgmH64U7/VIcBUH/gGJos1jiPxNSqiD7yZq8T/5q98msrpt+y3Ns= X-Received: by 2002:a2e:89d0:: with SMTP id c16mr14636145ljk.219.1565469272879; Sat, 10 Aug 2019 13:34:32 -0700 (PDT) MIME-Version: 1.0 References: <20190809142719.ff4kogz4fq6r7vfr@annexia.org> In-Reply-To: From: Chet Murthy Date: Sat, 10 Aug 2019 13:34:21 -0700 Message-ID: To: Josh Berdine , caml-list Cc: "Richard W.M. Jones" Content-Type: multipart/alternative; boundary="000000000000eab867058fc93721" Subject: Re: [Caml-list] Syntax puzzle / suggestion Reply-To: Chet Murthy X-Loop: caml-list@inria.fr X-Sequence: 17760 Errors-to: caml-list-owner@inria.fr Precedence: list Precedence: bulk Sender: caml-list-request@inria.fr X-no-archive: yes List-Id: List-Archive: List-Help: List-Owner: List-Post: List-Subscribe: List-Unsubscribe: --000000000000eab867058fc93721 Content-Type: text/plain; charset="UTF-8" Slight fixups: let all_items = ref ([] : string list) let add_item item = (all_items := item :: !all_items ; item) let item1 = add_item "hello" let item2 = add_item "world" let all_items = List.rev !all_items On Fri, Aug 9, 2019 at 7:53 AM Josh Berdine wrote: > What about good old-fashioned state: > > let all_items = ref [] > let add_item item = all_items := (item :: !all_items, item) > > let item1 = add_item "hello" > let item2 = add_item "world" > > let all_items = List.rev !all_items > > > On 9 Aug 2019, at 15:27, Richard W.M. Jones wrote: > > > Let's imagine you have a list of named things, but you also want to > > collect them up into a single list. For example: > > > > let item1 = "hello" > > let item2 = "world" > > let all_items = [ item1; item2 ] > > > > let () = > > printf "item1 = %s\n" item1; > > printf "all_items = %s\n" (String.concat ", " all_items) > > > > This is fine, but there's a danger that a programmer could add item3 > > but forget to add it to the "all_items" list. (In the real world > > problem to which this applies, my items are complex and lengthy > > structures, and the "all-things-list" is well off the bottom of the > > page when you're viewing the top item). > > > > My idea to fix this was to write: > > > > let all_items = [ > > ("hello" as item1); > > ("world" as item2); > > ] > > > > Actually I was slightly surprised to find this doesn't compile. I > > guess because "as" can only be used like this in patterns, not > > expressions. Also the scoping is wrong because the "as item1" if it > > worked probably wouldn't apply outside the list. > > > > I haven't worked out if it's possible to write this in ordinary OCaml, > > although I suppose ppx could solve it. Any ideas if this is possible? > > > > Rich. > --000000000000eab867058fc93721 Content-Type: text/html; charset="UTF-8" Content-Transfer-Encoding: quoted-printable
Slight fixups:

let all_items =3D ref ([= ] : string list)
let add_item item =3D (all_items :=3D item :: !all_item= s ; item)

let item1 =3D add_item "hello"
let item2 =3D = add_item "world"

let all_items =3D List.rev !all_items
=


On Fri, Aug 9, 2019 at 7:53 AM Josh Berdine <josh@berdine.net> wrote:
What about good old-fashioned= state:

let all_items =3D ref []
let add_item item =3D all_items :=3D (item :: !all_items, item)

let item1 =3D add_item "hello"
let item2 =3D add_item "world"

let all_items =3D List.rev !all_items


On 9 Aug 2019, at 15:27, Richard W.M. Jones wrote:

> Let's imagine you have a list of named things, but you also want t= o
> collect them up into a single list.=C2=A0 For example:
>
>=C2=A0 =C2=A0let item1 =3D "hello"
>=C2=A0 =C2=A0let item2 =3D "world"
>=C2=A0 =C2=A0let all_items =3D [ item1; item2 ]
>
>=C2=A0 =C2=A0let () =3D
>=C2=A0 =C2=A0 =C2=A0printf "item1 =3D %s\n" item1;
>=C2=A0 =C2=A0 =C2=A0printf "all_items =3D %s\n" (String.conca= t ", " all_items)
>
> This is fine, but there's a danger that a programmer could add ite= m3
> but forget to add it to the "all_items" list.=C2=A0 (In the = real world
> problem to which this applies, my items are complex and lengthy
> structures, and the "all-things-list" is well off the bottom= of the
> page when you're viewing the top item).
>
> My idea to fix this was to write:
>
>=C2=A0 =C2=A0let all_items =3D [
>=C2=A0 =C2=A0 =C2=A0("hello" as item1);
>=C2=A0 =C2=A0 =C2=A0("world" as item2);
>=C2=A0 =C2=A0]
>
> Actually I was slightly surprised to find this doesn't compile.=C2= =A0 I
> guess because "as" can only be used like this in patterns, n= ot
> expressions.=C2=A0 Also the scoping is wrong because the "as item= 1" if it
> worked probably wouldn't apply outside the list.
>
> I haven't worked out if it's possible to write this in ordinar= y OCaml,
> although I suppose ppx could solve it.=C2=A0 Any ideas if this is poss= ible?
>
> Rich.
--000000000000eab867058fc93721--