Loading ...
Sorry, an error occurred while loading the content.

13879Re: "ocaml_beginners"::[] accessing record types

Expand Messages
  • Gabriel Scherer
    Apr 15, 2013
    • 0 Attachment
      I'm sorry, but I still have no idea what your question is. Using a
      metaphor of databases to describe values in the language maybe doesn't
      help.

      On Tue, Apr 16, 2013 at 2:50 AM, Jean Saint-Remy
      <jeansaintremy@...> wrote:
      > I don't know how to adequately describe 2nd order of abstraction here. My apologies for the omission. Typically the
      > 'type' declaration does not involve dangling modifier using "of". I am using Person of entry and then break it down into
      > strings for first and last names, but inside a database you have a lot of non-sequteur, heterogenous collections. I want to
      > understand how the type engine allows us to work with strings, numbers, booleans, etc. If you are in the car parts
      > department, an auto part might have a description, manufacturer part number, secondary market part number, a boolean
      > if it is in the inventory, the price may be in floats and item count in ints.
      >
      > Jean
      >
      >
      >
      >
      > ________________________________
      > From: Gabriel Scherer <gabriel.scherer@...>
      > To: ocaml_beginners@yahoogroups.com
      > Sent: Monday, April 15, 2013 3:07 PM
      > Subject: Re: "ocaml_beginners"::[] accessing record types
      >
      >
      >
      >
      > Hi,
      >
      > It is rather difficult to know what you are asking for. I don't think
      > there is any link between the (expr : type) syntax and any of what you
      > describe in your email (in fact this syntax is mostly useless and
      > extremely rarely used). I also have no idea what you mean by "nullary
      > constructor" in your example. In my book a nullary constructor might
      > be (I don't think I've seen the name used consistently) a variant
      > constructor with no parameter
      >
      > type t =
      > | Foo of int
      > | Bar (* nullary ? *)
      >
      > which you have not used in your example.
      >
      > The language manual is intended as a language reference more than a
      > comprehensive introduction for beginners. The website page
      > http://ocaml.org/books.html has some books available online. In my
      > experience both Jason Hickey's "Introduction to OCaml" and the online
      > book "Developping Applications with OCaml" (available both in french
      > or english) are good, comprehensive resources.
      >
      > On Mon, Apr 15, 2013 at 7:43 PM, Jean Saint-Remy
      > <jeansaintremy@...> wrote:
      >> Hi,
      >>
      >> I am working on a no-frills database application, and wanted to better understand the nullary constructors.
      >> There are some examples in the literature, but the details are scarce. Most of the time, we don't access types
      >> directly. They are declared as memory containers so that data can be "safely" allocated, and we use data structures
      >> throughout.
      >>
      >>
      >> type db =
      >> | Person of entry
      >> | Weekly of sched
      >> and entry =
      >>
      >> {
      >> first_name : string;
      >> last_name : string;
      >> }
      >> and sched =
      >>
      >> {
      >> date : float;
      >> starting : float;
      >> ending : float;
      >> } ;;
      >> let molly : db = Person { first_name = "Molly"; last_name = "Peterson"; }
      >> and week1 : db = Weekly { date = 1355062480.; starting = 1355103310.; ending = 1355122250.; } ;;
      >>
      >> The molly and week1 statements can also be written separately. That would be currying.
      >>
      >>
      >> There is also an idiomatic short-hand form
      >>
      >> let molly = Person { first_name = "Molly"; last_name = "Peterson"; } ;;
      >>
      >> The type engine understands that there are two fields of type string.
      >>
      >>
      >> It seems we are creating an enumerated type, and these are declared just like records with (expr : type) syntax.To access the fields, can we use molly.first_name or molly.last_name besides writing a function "match x with"
      >> to decompose the fields? I have read many programs where these types are simply plugged into data structures, as
      >> long as the types are matching, everything works as expected. Obviously mixed type of string and float would become
      >> a tuple or a hash and won't work with lists or arrays which expect one kind of an object. It seems there is a leap of
      >> faith, the types are declared and then we just work with data structures with types being mere place holders.
      >>
      >> Declaring a "db" type with 5 fields is very simple and works well. If we want String.length (molly.first_name) we
      >> need to wrap the field in a data structure and access the elements. Is that all there is to the constructors? It took me
      >> a while to figure out how to get from the type declaration to the instance of molly, until I hit upon the (expr : type)
      >> with parenthesis required in the literature. OCaml core reference goes right away into pattern matching, so I could
      >> not apply the constructor. Would you be so kind and explain the nullary constructor in a greater detail?
      >>
      >> With kind regards,
      >>
      >> Jean
      >>
      >>
      >> [Non-text portions of this message have been removed]
      >>
      >>
      >>
      >> ------------------------------------
      >>
      >> Archives up to December 31, 2011 are also downloadable at http://www.connettivo.net/cntprojects/ocaml_beginners
      >> The archives of the very official ocaml list (the seniors' one) can be found at http://caml.inria.fr
      >> Attachments are banned and you're asked to be polite, avoid flames etc.Yahoo! Groups Links
      >>
      >>
      >>
      >
      >
      >
      > [Non-text portions of this message have been removed]
      >
      >
      >
      > ------------------------------------
      >
      > Archives up to December 31, 2011 are also downloadable at http://www.connettivo.net/cntprojects/ocaml_beginners
      > The archives of the very official ocaml list (the seniors' one) can be found at http://caml.inria.fr
      > Attachments are banned and you're asked to be polite, avoid flames etc.Yahoo! Groups Links
      >
      >
      >
    • Show all 6 messages in this topic