Re: Page 76 of OCaml by John Whitington ???
"Douglas Lewit [email protected] [ocaml_beginners]" <[email protected]> Thu, 24 Mar 2016 11:49:44 -0500
| Newsgroups | gmane.comp.lang.ocaml.beginners |
|---|---|
| Message-ID | <CAM0XMJSeR8-_gha4W-jKM+tQ7D09Qb3791HD55O=Wn7GoOjYnQ@mail.gmail.com> |
Um.... never mind that message, I got it! I just needed extra parentheses around those arguments. So.... *append ( Cons (5, Nil) ) Nil ;; *works, as does *append ( Cons (5, Nil) ) ( Cons (6, Nil) ) ;;* I'm just starting to learn about user-defined types. Interesting stuff. I know that in Java it is possible to combine ints and doubles in the same array if you do something like this: *Object[ ] dougsArray = new Object[2] ;* *dougsArray[0] = new Integer(10) ;* *dougsArray[1] = new Double(3.14159) ;* Since the Integer and Double classes both inherit from Object, it is possible in Java to do something like this even if it's not a very common practice. I know less about Ocaml than I do about Java, but I should think that in Ocaml there must be some way to create a user-defined type that encompasses both ints and floats (or doubles). Then again I might be wrong! I don't know. Whatever the case, I appreciate the guidance. Best, Douglas. P.S. I almost forgot. Is LISP considered the great granddaddy of all functional programming languages? Is it correct to say that OCaml is descended from LISP? Thanks for the feedback. On Thu, Mar 24, 2016 at 11:32 AM, Douglas Lewit [email protected] [ocaml_beginners] <[email protected]> wrote: > > > Good morning, > > I'm working on something on page 76 of "OCaml From the Very Beginning" by > John Whitington. This page is part of the Chapter on creating user-defined > types. I'm trying to use a function of his at the top of the page in the > utop interface, but Ocaml doesn't like what I'm typing into utop. Could > someone please tell me what I'm doing wrong. The functions are: > > *type 'a sequence = Nil | Cons of 'a * 'a sequence ;;* > > *let rec append a b = * > *match a with * > *Nil -> b* > *| Cons (h, t) -> Cons( h, append t b ) ;;* > > So I'm typing in utop > > append Cons(5, Nil) Nil ;; > > This returns an error! > > I also tried > > append Cons(5, Nil) Cons(6, Nil) ;; > > but again this returned an error. Could someone please explain this. I > think the purpose of this type is to simulate the builtin list type. It's > a great example IF I COULD GET THIS TO WORK! Thanks for the guidance. > > >