IWETHEY v. 0.3.0 | TODO
1,095 registered users | 0 active users | 0 LpH | Statistics
Login | Create New User
IWETHEY Banner

Welcome to IWETHEY!

New Just a guess
method set_x d = x <- x + d

I think the equivalent Python would be

def set_x (d):
        x = x + d
Jay O'Connor

"Going places unmapped
to do things unplanned
to people unsuspecting"
Expand Edited by Fearless Freep Aug. 17, 2001, 02:35:05 PM EDT
New Then its misnamed
if it adds d to x then its not setting x, its a moveXBy d.

Thats a little confusing to me so I wanted clarification.
New Re: Then its misnamed
Okay, I tried to post this a couple of days ago, but it didn't seem to
work. Here's the five-minute Caml lesson.

First, function definition: function bindings are introduced with a
'let' construct:

let add a b c = a + b + c

To call a function, you do this:

add 3 5 7
- : int = 15

Note that there are no parentheses -- simple juxtaposition will
do. Like all functional languages, Caml supports nested and
first-class functions, and uses tail-recursion to do looping.

let fib n =
let rec loop i a b =
if i = 0 then
a
else
loop (i-1) b (a+b)
in
loop n 1 1

fib 6
- : int = 13


Now, since Caml is functional, by default variables are immutable, and
can't be mutated. As a result, if you want a mutable variable, you need
to use a reference:

let x = ref 3
val x : int ref = {contents=3}

!x (* Dereference x *)
- : int = 3

x := !x + 3 (* Increment the value inside x *)

!x
- : int = 6


Now we come to one of the annoying redundancies in Ocaml syntax. The
':=' notation is not the only way to write mutation. When you have
records with mutable fields, then you can use the '<-' notation for
mutation as well:

type foo = {mutable bar: int}

let x = {bar = 6}

x.bar
- : int = 6

x.bar <- x.bar - 3 (* The record field is mutated *)

x.bar
- : int = 3

Now, the quick and dirty object tutorial. Here's a simple class

class foo =
object
val mutable x = 0 (* A mutable instance variable *)

method get_x = x (* A getter for the instance var x *)
end

let obj = new foo

obj#get_x (* Method call uses '#' instead of '.' *)

You can add arguments to the initalizer like so:

class point u v =
object
val mutable x = u
val mutable y = v

method get_x = x
method get_y = y

method move dx dy =
begin
x <- x + dx;
y <- y + dy;
end
end

let pt = new point 3 5

pt#get_x
- : int = 3

pt#move 4 5

(pt#get_x, pt#get_y)
- : int * int = 7, 10

     I've decided to write it all down - (tuberculosis) - (19)
         I get an error - (ChrisR) - (1)
             Doh! This one - (tuberculosis)
         Pet peeves - (wharris2) - (5)
             Well, if you just want to list those - (tuberculosis) - (1)
                 I don't care what the C++ crowd calls it - (wharris2)
             Argh! - (deSitter) - (2)
                 Well hang in there - (tuberculosis)
                 Two uses - (wharris2)
         "Boy, what a lot of typing that was!" - (a6l6e6x) - (2)
             Re: "Boy, what a lot of typing that was!" - (wharris2) - (1)
                 Thats a good point - (tuberculosis)
         Nice rants. - (static)
         Re: I've decided to write it all down - (neelk) - (4)
             I've read the previously posted rant - (tuberculosis) - (3)
                 Just a guess - (Fearless Freep) - (2)
                     Then its misnamed - (tuberculosis) - (1)
                         Re: Then its misnamed - (neelk)
         Posted a link... - (ChrisR) - (1)
             Thanks - (tuberculosis)

Sorry, you are not a winner.
51 ms