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

Help on exercize semantic and synthax

Expand Messages
  • pinomugo149
    HIin this exerc. I have a string and I want to count how many times some letters appears; for that i can use a hashtable with characters as keys and int
    Message 1 of 6 , Oct 6, 2012
    • 0 Attachment
      HIin this exerc. I have a string and I want to count how many times
      some letters appears; for that i can use a hashtable with characters as
      keys and "int counters" as value:
      let rec verify str hash = let len = String.length str in
      if len = 1 then hash else match str.[0] with
      'p' -> Hashtbl.replace hash "p" (Hashtbl.find hash "p" + 1)
      | 'e' -> Hashtbl.replace hash "e"(Hashtbl.find hash "e" + 1)
      | 'r' -> Hashtbl.replace hash "r" (Hashtbl.find hash "r" + 1)
      | _ -> () ; verify (String.sub str 1 (len - 1)) hash;;

      But the following error raise :
      Characters 165-217: 'p' -> Hashtbl.replace hash "p"
      (Hashtbl.find hash "p" + 1)
      ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^Error: This
      expression has type unit but an expression was expected of type
      (string, int) Hashtbl.t
      I still do not understand whyI still do not understand why an
      expression was expected of type(string, int) Hashtbl.t but not
      "unit'"?...wher is my error?and furthermore the semantic that I used is
      correct or I could write the function in a better way?thanks in advance!


      [Non-text portions of this message have been removed]
    • pinomugo149
      PS. for this message I used a rich-text-editor but the text formatting was lost!!and the code can not be read in clear way!! Why? thanks again
      Message 2 of 6 , Oct 6, 2012
      • 0 Attachment
        PS.
        for this message I used a rich-text-editor but the text formatting was lost!!and the code can not be read in clear way!!
        Why?
        thanks again
      • Rémi Vanicat
        ... Your code correctly indented is: let rec verify str hash = let len = String.length str in if len = 1 then hash else match str.[0] with p -
        Message 3 of 6 , Oct 7, 2012
        • 0 Attachment
          "pinomugo149" <pinomugo149@...> writes:

          > HIin this exerc. I have a string and I want to count how many times
          > some letters appears; for that i can use a hashtable with characters as
          > keys and "int counters" as value:

          Your code correctly indented is:

          let rec verify str hash =
          let len = String.length str in
          if len = 1 then hash
          else match str.[0]
          with
          'p' -> Hashtbl.replace hash "p" (Hashtbl.find hash "p" + 1)
          | 'e' -> Hashtbl.replace hash "e"(Hashtbl.find hash "e" + 1)
          | 'r' -> Hashtbl.replace hash "r" (Hashtbl.find hash "r" + 1)
          | _ -> () ;
          verify (String.sub str 1 (len - 1)) hash;;

          Note that the ; don't end the last match case, you need to enclose it
          into a begin end for the recursive call to be evaluated every time.


          Bye the way your code is really very very inefficient: creating the sub
          string is costly, and you do it a lot. You would probably be better
          doing a plain for loop.

          [...]

          >
          > [Non-text portions of this message have been removed]

          Please, don't post in html here, use plain text.


          --
          Rémi Vanicat
        • pinomugo149
          HI thanks for answered! ok but I would know why this error occurs: .....Hashtbl.replace hash p (Hashtbl.find hash p + 1)
          Message 4 of 6 , Oct 7, 2012
          • 0 Attachment
            HI thanks for answered!
            ok but I would know why this error occurs:

            .....Hashtbl.replace hash "p" (Hashtbl.find hash "p" + 1)
            ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^Error:
            This expression has type unit but an expression was expected of type
            (string, int) Hashtbl.t...

            why this expr isn't :of type (string, int) Hashtbl.t?? but unit?

            and another question: Ocaml leads that this expr must be of type """(string, int) Hashtbl.t""" becouse in the """ if len = 1 then hash...""" we declare "hashtable type" with "hash"?(that is an Hashtable..)??? and then also "else" must return an hashtable type?? it's right or else can return also a different type?
            thanks for your help!!!
            ps. I'll change the semantic of this code with a for loop...thanks
          • Toby Kelsey
            ... In the expression: if cond then X else Y the types of X and Y must agree, and this is the type of the if expression as a whole. Regards, Toby
            Message 5 of 6 , Oct 7, 2012
            • 0 Attachment
              On 07/10/12 13:30, pinomugo149 wrote:

              > and another question: Ocaml leads that this expr must be of type """(string, int) Hashtbl.t""" becouse in the """ if len = 1 then hash...""" we declare "hashtable type" with "hash"?(that is an Hashtable..)??? and then also "else" must return an hashtable type?? it's right or else can return also a different type?

              In the expression:

              if cond then X else Y

              the types of X and Y must agree, and this is the type of the 'if' expression as
              a whole.

              Regards,
              Toby
            • pinomugo149
              thanks Toby ;)
              Message 6 of 6 , Oct 7, 2012
              • 0 Attachment
                thanks Toby ;)
              Your message has been successfully submitted and would be delivered to recipients shortly.