Mailing list for all users of the OCaml language and system.
 help / color / mirror / Atom feed
From: Richard Jones <rich@annexia.org>
Cc: Tyler Eaves <tyler@ml1.net>, caml-list@inria.fr
Subject: Re: [Caml-list] Frustrated Beginner
Date: Tue, 23 Dec 2003 12:16:57 +0000	[thread overview]
Message-ID: <20031223121657.GB29933@redhat.com> (raw)
In-Reply-To: <20031223012223.W47896@bowser.eecs.harvard.edu>

On Tue, Dec 23, 2003 at 01:31:42AM -0500, Michael Jeffrey Tucker wrote:
> struct foo_type {
> 	enum {Some, None} opt;
> 	value_t val;
> };
> 
> If the opt field of the structure is set to Some, then you are free to
> look at the val field. If it's none, then the value in the val field is
> unspecified and should be ignored (of course this is more stringently
> enforced in OCaml). In OCaml an "option type" variable has either the
> value "Some xxx" or "None" where xxx is a value. So, a "string option"
> variable might have the value Some "hi", Some "qwery" or None. If you
> pattern match on a variable of this type, as in:

I like to think of the 'string option' type as being a bit like
two concepts in more familiar languages:

(1) NULL pointers in C, or
(2) Scalars in Perl, which can be 'undef'.

For example:

	C			OCaml			Perl

	char *str = "hello";	Some "hello"		$s = "hello"
	char *str = NULL;	None			$s = undef

The basic 'string' type in OCaml can never have a NULL value.  Sure,
it can be an empty string ("") but this is different from it having no
value at all.

BTW, 'string option' is very easily defined in OCaml as:

	type 'a option = None | Some of 'a

which naturally brings us to the subject of polymorphic types.  The above
definition defines a whole family of types, which can be thought of like
a wildcard '* option' where '*' is any existing type.  eg:

	Expression	Type

	Some 1		int option
	Some 'c'	char option
	Some [1]	int list option
	Some (Some 1)	int option option
	Some []		'a list option		(tricky one, this)

etc.

Rich.

-- 
Richard Jones. http://www.annexia.org/ http://freshmeat.net/users/rwmj
Merjis Ltd. http://www.merjis.com/ - improving website return on investment
MOD_CAML lets you run type-safe Objective CAML programs inside the Apache
webserver. http://www.merjis.com/developers/mod_caml/

-------------------
To unsubscribe, mail caml-list-request@inria.fr Archives: http://caml.inria.fr
Bug reports: http://caml.inria.fr/bin/caml-bugs FAQ: http://caml.inria.fr/FAQ/
Beginner's list: http://groups.yahoo.com/group/ocaml_beginners


  reply	other threads:[~2003-12-23 12:17 UTC|newest]

Thread overview: 37+ messages / expand[flat|nested]  mbox.gz  Atom feed  top
2003-12-23  4:03 Tyler Eaves
2003-12-23  4:19 ` jayanta nath
2003-12-23  5:34   ` Matt Gushee
2003-12-23  6:11     ` Tyler Eaves
2003-12-23  6:21       ` Michael Vanier
2003-12-23  6:31       ` Michael Jeffrey Tucker
2003-12-23 12:16         ` Richard Jones [this message]
2003-12-23 20:23           ` Dustin Sallings
2003-12-23  6:32       ` Shawn Wagner
2003-12-23  6:43       ` Matt Gushee
2003-12-23  5:58 ` Dustin Sallings
     [not found]   ` <EAEE2FF2-3510-11D8-B3A1-000A9584A16E@ml1.net>
2003-12-23  6:53     ` Dustin Sallings
2003-12-23  7:23       ` Tyler Eaves
2003-12-23  8:26         ` Dustin Sallings
2003-12-23  6:20 ` Tom Murray
2003-12-23  8:52 ` Stefano Zacchiroli
2003-12-23 16:47   ` [Caml-list] Ocaml syntax David Brown
2003-12-23 20:19     ` Dustin Sallings
2003-12-23 21:03       ` Eric Merritt
2003-12-23 21:52     ` brogoff
2003-12-24 10:27       ` skaller
2003-12-24 11:42         ` Peter Jolly
2003-12-24 12:19           ` skaller
2003-12-30  8:14     ` dmitry grebeniuk
2003-12-30 17:48       ` David Brown
2003-12-23 10:26 ` [Caml-list] Frustrated Beginner Samuel Lacas
2003-12-23 11:01   ` Dustin Sallings
2003-12-23 14:34     ` Oleg Trott
2003-12-23 20:25       ` Dustin Sallings
2003-12-23 16:11 ` Brian Hurt
2003-12-23 16:20   ` Sven Luther
2003-12-23 16:52     ` David Brown
2003-12-23 20:32       ` Dustin Sallings
2003-12-24 10:41         ` Issac Trotts
2003-12-23 17:39     ` Brian Hurt
2003-12-24  9:35       ` Jacques Garrigue
2003-12-24  9:49 ` skaller

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=20031223121657.GB29933@redhat.com \
    --to=rich@annexia.org \
    --cc=caml-list@inria.fr \
    --cc=tyler@ml1.net \
    /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