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

string -> charlist

Expand Messages
  • Oliver Bandel
    Hello, I got a problem, that only occurs in compilation and not in the toplevel (but the toplevel prints warnings about the type (should be unit)). Here is the
    Message 1 of 6 , May 8 1:52 PM
      Hello,

      I got a problem, that only occurs in compilation and not
      in the toplevel (but the toplevel prints warnings about
      the type (should be unit)).

      Here is the problematic code:


      ####################################################

      let rec charlist_of_string str =
      let len = String.length str in
      if len = 0 then []
      else
      str.[0] :: charlist_of_string (String.sub str 1 (len -1))



      charlist_of_string "Hallo9z g g"

      ####################################################

      In the toplevel I got the result: a list of char's.
      But compiling with ocamlc or ocamlopt says that
      the types in the application of the function are not
      matching.

      What is the problem here?


      TIA,
      Oliver
    • Gerd Stolpmann
      ... I guess you entered two phrases, ending every phrase with ;; . ... Either put ;; after the let rec definition, or write let _ = charlist_of_string
      Message 2 of 6 , May 8 2:35 PM
        On 2002.05.08 22:52 Oliver Bandel wrote:
        > Hello,
        >
        > I got a problem, that only occurs in compilation and not
        > in the toplevel (but the toplevel prints warnings about
        > the type (should be unit)).
        >
        > Here is the problematic code:
        >
        >
        > ####################################################
        >
        > let rec charlist_of_string str =
        > let len = String.length str in
        > if len = 0 then []
        > else
        > str.[0] :: charlist_of_string (String.sub str 1 (len -1))
        >
        >
        >
        > charlist_of_string "Hallo9z g g"
        >
        > ####################################################
        >
        > In the toplevel I got the result: a list of char's.

        I guess you entered two phrases, ending every phrase with ";;".

        > But compiling with ocamlc or ocamlopt says that
        > the types in the application of the function are not
        > matching.

        Either put ";;" after the "let rec" definition, or write

        let _ = charlist_of_string "Hallo9z g g"

        - otherwise the parser has no chance to see where "let rec" stops,
        and a new expression begins.

        Gerd
        --
        ----------------------------------------------------------------------------
        Gerd Stolpmann Telefon: +49 6151 997705 (privat)
        Viktoriastr. 45
        64293 Darmstadt EMail: gerd@...
        Germany
        ----------------------------------------------------------------------------
      • Nicolas Barbulesco
        ... Hi, I am new to this list and it was written that this is the place for not(yet)-caml-gurus, so I ll ask a question, even though some will maybe find it is
        Message 3 of 6 , May 8 3:04 PM
          > > ####################################################
          > >
          > > let rec charlist_of_string str =
          > > let len = String.length str in
          > > if len = 0 then []
          > > else
          > > str.[0] :: charlist_of_string (String.sub str 1
          > (len -1))
          > >
          > >
          > >
          > > charlist_of_string "Hallo9z g g"
          > >
          > > ####################################################
          > >
          > > In the toplevel I got the result: a list of char's.
          >
          > I guess you entered two phrases, ending every phrase with ";;".
          >
          > > But compiling with ocamlc or ocamlopt says that
          > > the types in the application of the function are not
          > > matching.
          >
          > Either put ";;" after the "let rec" definition, or write
          >
          > let _ = charlist_of_string "Hallo9z g g"
          >
          > - otherwise the parser has no chance to see where "let rec" stops,
          > and a new expression begins.

          Hi,

          I am new to this list and it was written that this is the place for
          not(yet)-caml-gurus, so I'll ask a question, even though some will maybe
          find it is a stupid question...

          What does
          let _ = myExpression ;;
          do ? I think _ cannot be an identifier.

          Anyway,

          # let _ = 4 ;;
          - : int = 4
          # _ ;;
          Syntax error

          Normal.
        • Michel Quercia
          Le Wed, 8 May 2002 22:52:34 +0200 (MET DST) ... You forgot to end the definition of charlist_of_string with two semicolons. By the way, your code is
          Message 4 of 6 , May 9 12:09 AM
            Le Wed, 8 May 2002 22:52:34 +0200 (MET DST)
            Oliver Bandel <oliver@...-berlin.de> écrivit :

            > Hello,
            >
            > I got a problem,...
            > let rec charlist_of_string str =
            > let len = String.length str in
            > if len = 0 then []
            > else
            > str.[0] :: charlist_of_string (String.sub str 1
            > (len -1))
            >
            >
            >
            > charlist_of_string "Hallo9z g g"

            You forgot to end the definition of charlist_of_string with two
            semicolons.

            By the way, your code is inefficient because you ask Ocaml to copy the
            string with one character less each time you put a character in the list.
            You can avoid copying the string by giving an index in the string as
            second argument. The following code does this, and builds the list from
            right to left so as to be tail recursive :

            let char_list_of_string str =
            let rec job i res =
            if i < 0 then res else job (i-1) (str.[i] :: res)
            in job (String.length(str)-1) []
            ;;

            Regards,
            --
            Michel Quercia
            23 rue de Montchapet, 21000 Dijon
            http://michel.quercia.free.fr (maths)
            http://pauillac.inria.fr/~quercia (informatique)
            mailto:michel.quercia@...
          • giangiammy
            ... Hi, and welcome to the group. let _ = expression ;; is just a way to say: evaluate expression and ignore the result As far as I know, _ alone has no
            Message 5 of 6 , May 9 12:25 AM
              --- In ocaml_beginners@y..., Nicolas Barbulesco <nbarbulesco@y...> wrote:

              > What does
              > let _ = myExpression ;;
              > do ? I think _ cannot be an identifier.
              >
              > Anyway,
              >
              > # let _ = 4 ;;
              > - : int = 4
              > # _ ;;
              > Syntax error
              >
              > Normal.

              Hi, and welcome to the group.

              let _ = expression ;; is just a way to say: "evaluate expression
              and ignore the result"
              As far as I know, "_" alone has no meaning, so the syntax error.

              By the way, to make the thing easy to everyone, you should
              use a subject related to your question, and don't worry to
              start a new topic.
              If you reply to a message, quote just the relevant part.

              Regards
              Gianluca
            • stalkern2
              Let me quote a previous message on this list ... Regards Ernesto
              Message 6 of 6 , May 9 9:35 AM
                Let me quote a previous message on this list

                >> 2) why do you all write let _ = expression?
                >>
                >> is this "_" the very same "catchall" pattern that is used in pattern
                >> matchings? In other words, is
                >> let _ = expression
                >> a way to get only the side effects of the declaration of the
                >> expression?

                >yes.
                >a let binding is nothing but a simplified version of the pattern
                >matching.


                Regards
                Ernesto
              Your message has been successfully submitted and would be delivered to recipients shortly.