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

beginner question about typing

Expand Messages
  • ben.aurel
    hi I make another attempt with ocaml and it seems like it doesn t fit my brain yet. ... let max a b = if a b then a else b ;; print_newline (max 3 5) ;; ...
    Message 1 of 7 , Nov 26, 2008
    View Source
    • 0 Attachment
      hi
      I make another attempt with ocaml and it seems like it doesn't fit my
      brain yet.

      ---
      let max a b =
      if a > b then a else b
      ;;

      print_newline (max 3 5) ;;

      ---

      The error message is the following:
      This expression has type int but is here used with type unit


      If tried several things like 'int max' or casting like 'then int a',
      but it doesn't work.

      What am I doing wrong here?
      thanks
      ben
    • Mehdi Dogguy
      ... # print_newline;; - : unit - unit = Its argument is just « unit ». So may be you will be interested in using: # print_int;; - : int - unit =
      Message 2 of 7 , Nov 26, 2008
      View Source
      • 0 Attachment
        ben.aurel wrote:
        >
        > let max a b =
        > if a > b then a else b
        > ;;
        >
        > print_newline (max 3 5) ;;
        >

        # print_newline;;
        - : unit -> unit = <fun>

        Its argument is just « unit ».
        So may be you will be interested in using:
        # print_int;;
        - : int -> unit = <fun>

        --
        Mehdi Dogguy مهدي الدقي
        http://www.pps.jussieu.fr/~dogguy
        Tel.: (+33).1.44.27.28.38
      • Richard Jones
        ... There isn t a general print anything operator in OCaml. In that respect, it s closer to C. You can however do this: open Printf let max a b = if a b
        Message 3 of 7 , Nov 26, 2008
        View Source
        • 0 Attachment
          On Wed, Nov 26, 2008 at 02:23:18PM -0000, ben.aurel wrote:
          > let max a b =
          > if a > b then a else b
          > ;;
          >
          > print_newline (max 3 5) ;;

          There isn't a general "print anything" operator in OCaml. In that
          respect, it's closer to C. You can however do this:

          open Printf

          let max a b =
          if a > b then a else b ;;

          printf "%d\n" (max 3 5)

          Have you looked at any OCaml tutorials? There's a big list of
          tutorials and books here:

          http://ocaml-tutorial.org/#External_tutorials

          Rich.

          --
          Richard Jones
          Red Hat
        • William D. Neumann
          On Wed, 26 Nov 2008 14:23:18 -0000, ben.aurel wrote ... As others have pointed out, the immediate problem is that you are passing a value of type int (the
          Message 4 of 7 , Nov 26, 2008
          View Source
          • 0 Attachment
            On Wed, 26 Nov 2008 14:23:18 -0000, ben.aurel wrote
            > hi
            > I make another attempt with ocaml and it seems like it doesn't fit my
            > brain yet.
            >
            > ---
            > let max a b =
            > if a > b then a else b
            > ;;
            >
            > print_newline (max 3 5) ;;
            >
            > ---
            >
            > The error message is the following:
            > This expression has type int but is here used with type unit
            >
            > If tried several things like 'int max' or casting like 'then int a',
            > but it doesn't work.
            >
            > What am I doing wrong here?

            As others have pointed out, the immediate problem is that you are passing a
            value of type int (the result of max 3 5) to a function that can only take
            arguments of type unit (print_newline, which has type unit -> unit).
            They've also given you suggestions on how to work around this particular
            problem. However, it may also help to know why you thought that the above
            example would work. This may allow us to clear up any misconceptions you
            have about OCaml, and get you up to speed a bit faster. That, along with
            going over the tutorials as Rich suggested, ought to help you out quite a
            bit.

            So, can you explain a bit why you thought the above example might type-check
            and run?

            --

            William D. Neumann
          • pisethkem
            ... you miss the case when a = b let max a b = if a
            Message 5 of 7 , Nov 27, 2008
            View Source
            • 0 Attachment
              --- In ocaml_beginners@yahoogroups.com, "ben.aurel" <ben.aurel@...> wrote:
              >
              > hi
              > I make another attempt with ocaml and it seems like it doesn't fit my
              > brain yet.
              >
              > ---
              > let max a b =
              > if a > b then a else b
              > ;;
              >
              > print_newline (max 3 5) ;;
              >
              > ---
              >
              > The error message is the following:
              > This expression has type int but is here used with type unit
              >
              >
              > If tried several things like 'int max' or casting like 'then int a',
              > but it doesn't work.
              >
              > What am I doing wrong here?
              > thanks
              > ben
              >


              you miss the case when a = b

              let max a b =
              if a <= b then b else a;;

              print_int (max 3 5);;


              piseth kem
            • Till Crueger
              ... [snip] ... Please don t confuse beginners.... There is no case missing in the above example. The only Problem was with the print_newline instead of the
              Message 6 of 7 , Nov 27, 2008
              View Source
              • 0 Attachment
                On Thu, 27 Nov 2008 09:52:18 +0100, pisethkem <pisethkem@...> wrote:

                >> let max a b =
                >> if a > b then a else b
                >> ;;
                >>
                >> print_newline (max 3 5) ;;
                [snip]
                > you miss the case when a = b
                >
                > let max a b =
                > if a <= b then b else a;;
                >
                > print_int (max 3 5);;

                Please don't confuse beginners.... There is no "case missing" in the above
                example. The only Problem was with the print_newline instead of the
                print_int...

                The max functions you gave and he gave are completyl identical as can be
                proven by some simple transformations, like
                !(a > b) <=> a <= b
                and
                if !x then a else b <=> if x then b else a

                Apart from that the case a = b is even more irrelevant because in that
                case it does not matter if you return a or b...

                Greetings,
                Till
              • Florent Monnier
                Hi ben, I don t know why ocaml doesn t fit right with your brain, but I can tell you how it fits with mine. Personnally I think of ocaml like these games for
                Message 7 of 7 , Nov 27, 2008
                View Source
                • 0 Attachment
                  Hi ben, I don't know why ocaml doesn't fit right with your brain, but I can
                  tell you how it fits with mine.
                  Personnally I think of ocaml like these games for kids with a board with holes
                  in it of different shapes, and the kid has pieces with shapes that fit in the
                  holes. There are shapes of circle, square, star, duck...
                  This is really the way the type system is represented in my brain.
                  And the drama is that now I can not use any other programming language anymore
                  because I can not imagine anymore use something that doesn't prevent me to
                  put the star shape in the circle hole ;-)
                  So run! Run away before you can not use something else than ocaml you too!

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