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

OCaml square funciton is not working

Expand Messages
  • suresh
    I tried the followoing... # square(4);; Characters 0-6: square(4);; ^^^^^^ Error: Unbound value square sqrt is working fine though. Can some one help me
    Message 1 of 6 , Feb 28, 2011
    • 0 Attachment
      I tried the followoing...
      # square(4);;

      Characters 0-6:
      square(4);;
      ^^^^^^
      Error: Unbound value square

      sqrt is working fine though.

      Can some one help me please?
    • Ashish Agarwal
      There is no square function. Only sqrt is defined in Pervasives. ... [Non-text portions of this message have been removed]
      Message 2 of 6 , Feb 28, 2011
      • 0 Attachment
        There is no square function. Only sqrt is defined in Pervasives.


        On Mon, Feb 28, 2011 at 2:44 PM, suresh <surri28@...> wrote:

        >
        >
        > I tried the followoing...
        > # square(4);;
        >
        > Characters 0-6:
        > square(4);;
        > ^^^^^^
        > Error: Unbound value square
        >
        > sqrt is working fine though.
        >
        > Can some one help me please?
        >
        >
        >


        [Non-text portions of this message have been removed]
      • Arlen Cuss
        There s no such function defined by default, but there is an exponentiation operator (**) defined on floats: # 4. ** 2.;; - : float = 16. # Note that it only
        Message 3 of 6 , Feb 28, 2011
        • 0 Attachment
          There's no such function defined by default, but there is an
          exponentiation operator (**) defined on floats:

          # 4. ** 2.;;
          - : float = 16.
          #

          Note that it only works on floats. You could define a 'square' function
          to do what you want as follows:

          # let square n = int_of_float ((float_of_int n) ** 2.);;
          val square : int -> int = <fun>
          # square 4;;
          - : int = 16
          #

          But you're better off working with floats, as that's how they're given
          to you by (**).

          HTH,
          Arlen

          On Mon, 2011-02-28 at 19:44 +0000, suresh wrote:
          > I tried the followoing...
          > # square(4);;
          >
          > Characters 0-6:
          > square(4);;
          > ^^^^^^
          > Error: Unbound value square
          >
          > sqrt is working fine though.
          >
          > Can some one help me please?
          >
          >
          >
          > ------------------------------------
          >
          > Archives up to December 31, 2010 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
          >
          >
          >
        • Sergei Steshenko
          The OP seems to need square of integers, so why working with floats ? Regards, Sergei. ... From: Arlen Cuss Subject: Re:
          Message 4 of 6 , Feb 28, 2011
          • 0 Attachment
            The OP seems to need square of integers, so why working with floats ?

            Regards,
            Sergei.

            --- On Mon, 2/28/11, Arlen Cuss <celtic@...> wrote:

            From: Arlen Cuss <celtic@...>
            Subject: Re: "ocaml_beginners"::[] OCaml square funciton is not working
            To: ocaml_beginners@yahoogroups.com
            Date: Monday, February 28, 2011, 7:24 PM







             









            [snip]

            But you're better off working with floats, as that's how they're given

            to you by (**).



            HTH,

            Arlen



            On Mon, 2011-02-28 at 19:44 +0000, suresh wrote:

            > I tried the followoing...

            > # square(4);;

            >

            > Characters 0-6:

            > square(4);;

            > ^^^^^^

            > Error: Unbound value square

            >

            > sqrt is working fine though.

            >

            > Can some one help me please?

            >

            >

            >

            > ------------------------------------

            >

            > Archives up to December 31, 2010 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

            >

            >

            >
          • Sylvain Le Gall
            Hello, ... You can keep working on integer. The square function for integers is maybe just too trivial to be part of the standard library (whereas the square
            Message 5 of 6 , Mar 1, 2011
            • 0 Attachment
              Hello,

              On 01-03-2011, Sergei Steshenko <sergstesh@...> wrote:
              >
              > The OP seems to need square of integers, so why working with floats ?
              >

              You can keep working on integer. The square function for integers is
              maybe just too trivial to be part of the standard library (whereas the
              square root is not that trivial).

              # let square x = x * x;;

              # square 2;
              -> 4


              Cheers,
              Sylvain Le Gall
              --
              My company: http://www.ocamlcore.com
              Linkedin: http://fr.linkedin.com/in/sylvainlegall
              Start an OCaml project here: http://forge.ocamlcore.org
              OCaml blogs: http://planet.ocamlcore.org
            • Arlen Cuss
              ... Whoops, didn t even think of the definition of a square and just went straight for the exponential ..! A [Non-text portions of this message have been
              Message 6 of 6 , Mar 1, 2011
              • 0 Attachment
                On Mon, 2011-02-28 at 20:31 -0800, Sergei Steshenko wrote:
                > The OP seems to need square of integers, so why working with floats ?
                >
                > Regards,
                > Sergei.

                Whoops, didn't even think of the definition of a square and just went
                straight for the exponential ..!

                A


                [Non-text portions of this message have been removed]
              Your message has been successfully submitted and would be delivered to recipients shortly.