caml-list - the Caml user's mailing list
 help / color / mirror / Atom feed
From: David Allsopp <dra-news@metastack.com>
To: "caml-list@inria.fr" <caml-list@inria.fr>
Subject: RE: [Caml-list] Differences between Array and Strings
Date: Sat, 30 Apr 2011 13:34:23 +0000	[thread overview]
Message-ID: <E51C5B015DBD1348A1D85763337FB6D972AB2CBE@Remus.metastack.local> (raw)
In-Reply-To: <dbf8808a-8fce-4f1c-adc8-04f2400b6e52@glegroupsg2000goo.googlegroups.com>

Radu Grigore wrote:
> On Friday, April 29, 2011 12:51:59 PM UTC+1, louis....@ens.fr wrote:
> > > let l () = "1" ;;
> > > [...]
> > > l()==l();;
> > # val l : unit -> string = <fun>
> > # [...]
> > # - : bool = true
> 
> Is there a good reason for this behavior?

Whether it's good is debatable, but in most instances you don't want a fresh string being allocated each time for a constant value as it would be a waste of time and memory (most strings are used immutably).

> My first thought was that that code should behave the same as
> 
>   let f () = ref () in f () == f ()
> (A satisfying explanation would *not* involve the compiler.)

Look at the OCaml language specification - "1" is a constant and the result of that expression is the constant itself (6.7.1). [|1|] is an array expression and the result of that expression is a "1-element array, whose element(s) are initialized with the value(s) 1" (roughly quoting section 6.7.3). i.e. an array expression implies the creation of the array as its result but a constant evaluation does not.

C behaves in the same (or at least in a very similar) way... not that that could ever be used as a justification for behaviour in a sane language like OCaml ;o)
 

David 


  reply	other threads:[~2011-04-30 13:34 UTC|newest]

Thread overview: 6+ messages / expand[flat|nested]  mbox.gz  Atom feed  top
     [not found] <fa.YVp+LBcZBn2VVSqyBV+/Bw0Mb/I@ifi.uio.no>
2011-04-30 11:03 ` Radu Grigore
2011-04-30 13:34   ` David Allsopp [this message]
2011-04-30 23:10     ` Martin Jambon
2011-04-29 11:51 louis.jachiet
2011-04-29 14:36 ` David Allsopp
2011-04-30 14:06 ` Philippe Wang

Reply instructions:

You may reply publicly to this message via plain-text email
using any one of the following methods:

* Save the following mbox file, import it into your mail client,
  and reply-to-all from there: mbox

  Avoid top-posting and favor interleaved quoting:
  https://en.wikipedia.org/wiki/Posting_style#Interleaved_style

* Reply using the --to, --cc, and --in-reply-to
  switches of git-send-email(1):

  git send-email \
    --in-reply-to=E51C5B015DBD1348A1D85763337FB6D972AB2CBE@Remus.metastack.local \
    --to=dra-news@metastack.com \
    --cc=caml-list@inria.fr \
    /path/to/YOUR_REPLY

  https://kernel.org/pub/software/scm/git/docs/git-send-email.html

* If your mail client supports setting the In-Reply-To header
  via mailto: links, try the mailto: link
Be sure your reply has a Subject: header at the top and a blank line before the message body.
This is a public inbox, see mirroring instructions
for how to clone and mirror all data and code used for this inbox;
as well as URLs for NNTP newsgroup(s).