Programming C, C++, Java, PHP, Ruby, Turing, VB
Computer Science Canada 
Programming C, C++, Java, PHP, Ruby, Turing, VB  

Username:   Password: 
 RegisterRegister   
 I fall further for O'Caml...
Index -> Programming, General Programming -> Functional Programming
View previous topic Printable versionDownload TopicSubscribe to this topicPrivate MessagesRefresh page View next topic
Author Message
wtd




PostPosted: Wed Jun 16, 2004 10:27 pm   Post subject: I fall further for O'Caml...

A trivial example to demonstrate some gorgeous code...

The goal: create a precompiled library containing a mutable_point class in a Point namespace/module.

point.h:
code:
#include <string>
#include <sstream>
#include <iostream>

#ifndef __POINT__MUTABLE_POINT_H
#define __POINT__MUTABLE_POINT_H

namespace Point
{
        class mutable_point
        {
                protected:
                        int x;
                        int y;
                public:
                        mutable_point(int init_x, int init_y);

                        int get_x() const;
                        int get_y() const;
                        void set_x(int new_x);
                        void set_y(int new_y);
                        std::string to_str() const;
        };
}

#endif


point.cpp:
code:
#include <string>
#include <sstream>
#include <iostream>
#include "point.h"

Point::mutable_point::mutable_point(int init_x, int init_y)
: x(init_x), y(init_y)
{ }

int Point::mutable_point::get_x() const
{
        return x;
}

int Point::mutable_point::get_y() const
{
        return y;
}

void Point::mutable_point::set_x(int new_x)
{
        x = new_x;
}

void Point::mutable_point::set_y(int new_y)
{
        y = new_y;
}

std::string Point::mutable_point::to_str() const
{
        std::stringstream out;
        out << get_x() << " " << get_y();
        return out.str();
}


main.cpp:
code:
#include <string>
#include <sstream>
#include <iostream>
#include "point.h"

int main()
{
        Point::mutable_point p = Point::mutable_point(42, 27);

        std::cout << p.to_str() << std::endl;
}


point.ml:
code:
open Printf;;

module Point =
struct
        class mutable_point (init_x : int) (init_y : int) =
        object (self)
                val mutable x = init_x
                val mutable y = init_y
                method get_x = x
                method get_y = y
                method set_x new_x = x <- new_x
                method set_y new_y = y <- new_y
                method to_str = Printf.sprintf "%d %d" self#get_x self#get_y
        end;;
end;;


main.ml:
code:
open Point;;

let _ =
        let p = new Point.mutable_point 42 27 in
        print_endline p#to_str;;


C++ compilation:
code:
$ g++ -c point.cpp
$ g++ main.cpp point.o -o cppex
$./cppex
42 27
$


O'Caml compilation:
code:
$ ocamlc -c point.ml
$ ocamlc main.ml point.cmo -o mlex
$ ./mlex
42 27
$


Lines of code for the c++ example: 72. Lines of code for the O'Caml example: 20. Smile
Sponsor
Sponsor
Sponsor
sponsor
Display posts from previous:   
   Index -> Programming, General Programming -> Functional Programming
View previous topic Tell A FriendPrintable versionDownload TopicSubscribe to this topicPrivate MessagesRefresh page View next topic

Page 1 of 1  [ 1 Posts ]
Jump to:   


Style:  
Search: