Monday, July 7, 2008

1) An Introduction to Designing Classes in C# - Pt.3

Programming Language: C#
Type: Tutorial
Level: Beginners
Topic Category: General Programming
Main Series:
Designing and Implementing Classes in C#
Topic Title: 1) An Introduction to Designing Classes in C# - Pt.3

Quick Review:
In part 1 of this article, we discussed classes and how to design them. We established what classes are and what they are used for and what they consist of. In part 2, we introduced the general syntax for defining classes in C# and discussed the first part of the declaration: access modifiers. In this article, we will continue discussing the declaration of classes and will introduce the different types of members of classes.

Remind me again how to declare a class in C#!
Here's a small reminder:
access-modifier [static|abstract|sealed] class class-name [: [parentClass][, implementedInterface]*]

Oh yes, I remember now. So we stopped at access modifiers. What are these three other words (static, sealed and abstract)?
I'll introduce each of these modifiers briefly. However, the concepts behind them depend on other concepts we have not discussed yet such as inheritance, polymorphism and scope.
We start with the scope modifier, static.
Static Classes
Classes are usually defined so that we can declare objects of them later. In some cases however, we only need to declare one object of the class, no more no less. For example, if we want to define a class from which we will declare an object that describes the monitor of the system, the memory of the system, the cpu of the system or perhaps, the company for which we are developing the software. Classes like that are called singleton classes. Simply, one use of static classes is to represent these classes. Static, in C#, is usually used to describe members. It means in some way: "belonging to the class itself and not to any specific object of the class". For example, if we are defining a Point class, representing geometrical point objects, we might want to run the code in one of two modes: Cartesian and polar coordinates. The fact that "points" are expressed in either format, is irrelevant to any specific "point". In fact, it affects "all" points. That's a member that belongs to the class of points as a whole not to objects of the class. Now, static classes, are by definition in C# classes with all members being static. Meaning all members of a static class has to be declared static, which in turn means they belong to the class itself. By this, we can think of the class itself as being the only object declared. Static classes are also used to group a set of methods and properties that are related by task but not belonging to any specific object. For example, a set of methods that compare/convert objects of different types in some application are all related by task, but they do not belong to a specific object as they will always behave the same no matter what object they are in (or are not in for that matter). These members are all grouped as static members of a static class. Now we discuss the second modifier: abstract
Abstract classes:
Abstract classes are used when building hierarchies of classes that are related. To fully understand this modifier, you must be familiar with inheritance and polymorphism, so I will only say, they are classes with at least one abstract member. We will discuss abstract members immediatly after inheritance. That leaves us with only one modifier: sealed
Sealed classes:
Sealed classes are simply classes that do not allow any other class to inherit from them. Again, you must be familiar with the concept of inheritance to fully understand sealed classes, and they will be revisited in inheritance
Fair enough. So, can I learn to declare my first class now, or do I have to learn inheritance first?
Actually, it will be nearly impossible and in vain to learn inhertance when you don't know how to declare members. Also, except for an empty class, classes will usually need you declare one or more members. So let's start by a brief introduction about the syntax of these members and then writing the first class?
Types of Class Members:
Classes at most, consist of Fields, Methods, Properties, Events and Nested Types. Following is a brief description of each one:
Nested Types: These are simply types nested within other types. In other words, they are types that are defined within another type. They are used when a major type needs to gather a set of information to be used only by that type. For example, a software that interacts with a hardware device might need a structure to hold the settings of the device not as seen by a high level user, but after converting them to the complex and more detailed binary values that will actually be sent to the device. Nesting types within each other is usually seen in large software libraries development to emphasize a type is only declared for the use of another type only. You nest types simply by declaring the nested type/class within the scope boundaries of the nesting class as you would normally declare any class:
class PrinterDriver {
    class hardwarePageSettings{
    }
}
Fields: fields can be thought of as being the data storage slots of the class. Fields are data (informational) members that do actually store the object's data (unlike properties). In other words, if a class having a field called "length", is used to declare objects, all objects of this class will have its own "length". That's of course if the field is not static. As we mentioned earlier, static members belong to the class, meaning only one copy of the member will be available at anytime regardless of the objects declared from that class. Well, fields are no exception! Also, we mentioned earlier that members have access modifiers. Being a data storage, you must also provide the type of data that will be stored. According to this, the simplest good field declaration will look something like:
public class MailPackage{
    public int width;
    public int height;
    public int depth;
}
Methods: are used to represent the operational attributes of a class. They can be thought of as being blocks of code that takes inputs, makes some processing and return an output. The fields of the class (and the values of these fields in object of the class) are always available to the methods in the class to read and modify. The last feature eleminates the need for long parameters list in methods. Inputs are given to the method in the form of parameters (some methods take no inputs and so have no parameters). Each parameter is defined by a data type and an alias to be used by the moethod to distinguish it from other parameters. Parameters can be passed by value (default behavior), by reference (ref parameters) and as output parameters (out parameters). C# also supports variable length parameters lists (params arguments). Here's a sample method that will take an integer and calculates its square:
public class IntegerCalculations{
    public int Squared( int x){
        return x*x;
    }
}
Properties: are fake fields, or at least, that what they look like to C++ programmers! You use a property just like you would use a field in code. However, when declared, a property is actually two methods! One is used to get the value of the fake field and an optional one that will set that value. You usually use properties for the following purposes:
  1. To act like proxies for you fields. That is, you declare private fiields and create a public property for each field that will return and set its value. This is a common practice amongst programmers because it gives you the opportunity to modify the way a field is retrieved if it was needed or may be call some method everytime the field is written to.
  2. To declare calculated fields. These are fields that do not actually need to be stored because they can be inferred/calculated from other fields in the object. An example would be the area of a circle object which can be calculated from its radius.
  3. To make a field read-only for users of the class and still be able to write to it within the class. This is done by only defining the get method of the property
An example of properties is shown below where CenterX, CenterY and Radius are accessor (proxy) properties and Area is a calculated read only property:
public class Circle{
    private double x,y,r;
    public double CenterX{get{return x;}set{x = value;}}
    public double CenterY{get{return y;}set{y = value;}}
    public double Radius{get{return r;}set{r = value;}}
    public double Area{get{return Math.PI*r*r;}}
}
Events: wil actually be fully discussed in depth along with delegates in a coming article. In brief, they provide a means for other developers who will use your class to be notified when some event of interest occurs. This is done in a Publisher-Event-Subscriber model that works as follows:
  1. You publish an event by creating an event member in your class that will be accessible by subscribers
  2. Subscribers who are interested in the event will provide a method (or more) that should be executed when the event occurs. This is called "Attaching" "Handlers" to event, where handlers are the methods subscribers provide.
  3. Whenever the event occurs within your class, you find all methods attached to the event and execute them.
An example will be in the case where you want to notify develoeprs of a change in the radius of a circle, you will declare an event within the Circle class like this:
public event
EventHandler RadiusChanged;

Events are declared as if they were fields and their type must be a delegate.
Finally, Your First Class
Here we go, the first full class (well, not exactly full, but it will do). We will develop a Name class that is capable of holding the structured information of a person's name (First Name, MiddleName, LastName)
public class Name{
  //Private fields
  private string first, middle, last;
  private string prefix, suffix;
  //Public Accessor Properties
  public string First{get{return first;}set{first = value;}}
  public string Middle{get{return middle;}set{middle = value;}}
  public string Last{get{return last;}set{last = value;}}
  public string Prefix{get{return prefix;}set{prefix = value;}}
  public string Suffix{get{return suffix;}set{suffix = value;}}
  //Public Read-Only Calculated Properties
  public string Full{get{return last + ", " + first + " " + middle;}}
}

Now that you've written your first class, the following articles, will first tackle common basic types in C# and the .NET framework. The understanding of these fundamental types and some of there commonly used members is essential to writing any code. That concludes it for today's article.

Sunday, July 6, 2008

Get Started With Prolog

Prolog - Wikipedia, the free encyclopedia:
As usual, a good place will always be to go visit wikipedia and search for whatever it is you're looking for. This page includes all starting information you need to know about Prolog before you go search for any other resource.

prolog :- tutorial
A very neat and full of examples tutorial by J.R. Fisher on the Prolog programming language. Excellent place to begin learning prolog hands-on approach

ISO 13211-1:1995 - The ISO Standard for Prolog
For those interested in the ISO standard for Prolog, this is the official place to go. However, the ISO standards are only sold on this site!
Instead, you can go here to find some description on the standard, this time for free!
This site contains the December 1991 draft, the March 1993 draft, Richard O'Keefe's 1984 Prolog standard draft, and Michael Covington's summary of the standard. Note that no one at that site can answer any questions about the standard; it is just an FTP site for the standard in the USA.

FAQ - comp.lang.prolog
An extensive FAQ on Prolog, available in different formats (PDF, HTML, Text, SGML, Sources)
Look at this question on the HTML version of the FAQ, it will contain a list of where you can get a free Prolog compiler.

Online guide to Prolog Programming
This is a Prolog book/guide with a very distinguished idea that I personally liked very much, which is the "Test Zone" which is a java applet where you can test your samples and code. The site and guide is designed and written by an Associate Professor at Charles University, Prague, Roman Barták. I like this very much. It's also very useful of course!

Building Expert Systems in Prolog
A good old fashioned book in Prolog of 12 chapters ending with the Rubik's Cube solving expert system. The book is well organized, have many examples, and very useful.

You'll also find a lot of useful links in the Wikipedia page for Prolog, so really start there in any case. I included an extract from the comp.lang.prolog FAQ which is a list of places you can get a free Prolog:

ALF (Albgebraic Logic Functional language)
Platforms: UNIX
Available: Unknown
E-mail: Rudolf Opalla
Info: WAM-based language with narrowing/rewriting
Amzi! Prolog + Logic Server
Platforms: Window, Linux and Solaris
Available: http://www.amzi.com/download/
E-mail:
Info: Registration is compulsory, except for the Free Academic/Personal/Evaluation License.
Aquarius Prolog 1.0
Platforms: UNIX
Available: http://www.info.ucl.ac.be/people/PVR/aquarius.html
Info: High performance, commercial functionality except debugging and modules.
Argo Prolog v.1.1
Platforms: Solaris 1.x and HP-UX 9.x
Available: Unknown
Contact: Takao Doi
Arity/Prolog32
Platforms: Win32
Available: http://www.arity.com/www.pl/products/ap.htm
Info: Arity/Prolog32 provides a complete Prolog programming environment in which you can write, debug, and run Prolog programs in 32-bit Windows environments (95/98/NT/2000). Arity/Prolog32 is a powerful, highly optimized, and extended version of the logic programming language Prolog. Arity/Prolog32 is a complete compiler and interpreter written in Prolog, C, and Assembly language and is a superset of Clocksin and Mellish Prolog.
B-Prolog 4.0
Platforms: Win32, Solaris, SunOS, UNIX, FreeBSD and Linux
Available: http://www.probp.com/
E-mail: Neng-Fa Zhou
Info: Freely available for non-commercial use. For other use a license is needed.
BinProlog 7.0
Platforms: Windows 95/98/NT, Linux and all major Unix platforms.
Available: http://www.binnetcorp.com/BinProlog/
E-mail: Paul Tarau
Info: Download free evaluation copies and see online demos. Inexpensive Educational licensing available.Has built-in networking, multi-threading, mobile code and distributed blackboards. Supports BinNet Internet Programming Tool kit (see http://www.binnetcorp.com/Internet).
Brain Aid Prolog (BAP) v1.4
Platforms: Transputer systems
Available: http://www.comnets.rwth-aachen.de/~ost/private.html
Info: BAP is a parallel prolog system for Transputer systems. Available under a Berkely style of copyright.
C#Prolog
Platforms: Win32, UNIX
Available: http://sourceforge.net/projects/cs-prolog/
E-mail: John Pool
Info: A Prolog interpreter written in C#. Can easily be integrated in C# programs. Characteristics: reliable and quite fast beta version, command line interface, builtin DCG, XML-predicates, persistent predicates (using Firebird RDBMS), extendible.
Ciao 1.4
Platforms: Linux, Win32 (95/98/NT), Solaris, SunOS, UNIX in general.
Available: http://www.clip.dia.fi.upm.es/Software/Ciao
E-mail: Developers , Users
Info: Next generation LP/CLP system. Commercial functionality, but freely available w/source. ISO-Prolog + modules, networking, multi-threading, clp(r), clp(q), interfaces (Java, C, tcltk, WWW, databases/ODBC, ...), functions, higher-order, records, persistence, objects, assertions (types, modes, ...), source debugger, auto-documenter, static debugger, and more.
clp(FD)
Platforms: UNIX
Available: anonymous FTP from ftp://ftp.inria.fr/INRIA/Projects/ChLoE/LOGIC_PROGRAMMING/clp_fd
Contact: Daniel Diaz
Info: Constraint logic programming over finite domains. Requires GNU C v.2.4.5 or higher.
clp(FD,S)
Platforms: UNIX
Available: http://contraintes.inria.fr/~georget/software/clp_fds/clp_fds.html
Contact: Yan Georget
Info: Requires GNU C (gcc) version 2.4.5. or higher.
CLP(R)
Platforms: UNIX
Available: E-mail request from Joxan Jaffar .
Info: Constraint logic programming language, for academic and research purposes only.
ECLiPSe Constraint Logic Programming System, subsuming Prolog.
Platforms: Solaris, Linux, Linux/Alpha, Mac OS X, Windows
Available: http://www.eclipse-clp.org/ or http://www.sourceforge.net/projects/eclipse-clp
Info: ECLiPSe is a Prolog and Constraint Programming platform with a long history and has been open-sourced in Sept 2006.
License: MPL
IF Prolog V5.3
Platforms: Windows 95/98/NT/2000/XP, Linux, Solaris, AIX, HP-UX and other UNIX platforms
Available: http://www.ifcomputer.de/Products/Prolog/
E-mail:
Info: IF Prolog is a commercial Prolog system with interfaces to C/C++, Java, sockets, Windows events and a COM servers. A graphical debugger allows step-forward, step backward debugging of Prolog code. A static module concept allows many additional errors to be detected at compile time. Constraint Programming (for finite domains, intervals and booleans using global constraints and linear optimisation).
License: Free evaluation copies and inexpensive educational licensing available.
GNU Prolog
Platforms: Many Unixes, Windows, MacOS X
Available: http://gnu-prolog.inria.fr/
E-mail: Daniel Diaz
Jinni 2.27
Platforms: Java-based
Available: http://www.binnetcorp.com/Jinni
Info: Multi-threaded, Java based Prolog interpreter with built-in networking, distributed blackboards and mobile code (inexpensive shareware licensing available).
JIProlog
Platforms: Java-based
Available: http://www.ugosweb.com/jiprolog
Info: Java Internet Prolog is a cross-platform pure Java 100% prolog interpreter that supplies Java world with the power of prolog language and provides prolog language with a technology to implement new predicates in Java.
KLIC
Platforms: UNIX
Available: Anonymous FTP from ftp://ftp.icot.or.jp/ifs/symbolic-proc/unix/klic/klic.tgz.
Info: ICOT Free Software. Concurrent logic programming. Tested on Sparcs, DEC 7000, Gateway P5-60.
Contact:
LPA Win-Prolog, demo version
Platforms: Windows
Available: Available from http://www.lpa.co.uk/ind_dow.htm
MINERVA
Platforms: Java
Available: Available from http://www.ifcomputer.co.jp/MINERVA
Info: Proprietory commercial ISO-Prolog Compiler in 100% Java support for web programming, XML, servlets, applets, standalones. Free evaluation license.
Modular SB-Prolog (= SB-Prolog version 3.1 plus modules)
Platforms: SPARC, DECstation, MIPS, HP 9000 series, Sun 3.
Available: Anonymous FTP from ftp://ftp.dcs.ed.ac.uk/
Info: Copy-lefted.
Newt Prolog
Platforms: Apple MessagePad Newton
Available: Currently only beta version available; download and more information on http://www.cfht.hawaii.edu/~jlv
E-mail:
Open Prolog
Platforms: Apple Macintosh
Available: http://www.cs.tcd.ie/open-prolog/
E-mail: . (Michael Brady).
Poplog Prolog
Platforms: Various Unixes, including Sun, Dec Alpha, HP and many others. Also a Win32 version is available. Sources available for other combinations.
Available: At the Free Poplog Web/FTP site, including full sources http://www.cs.bham.ac.uk/research/poplog/freepoplog.html Mirror sites at http://www.poplog.org/resources/dist/new/
E-mail: queries may be posted to news://comp.lang.pop/, or to or (Last resort!)
Info: Robust incremental compiler, part of the multi-language Poplog system (including Common Lisp, Pop-11 and Standard ML). Unix, Linux & VMS versions include full support for X window facilities/Motif. More information at http://www.cs.bham.ac.uk/research/poplog/poplog.info.html Licence modelled on XFree86. Can be freely distributed, though copyright is owned by Sussex University and ISL.
PIE2
Platforms: Unknown
Available: On CompuServe in the AIEXPERT forum, interpreter and examples in PIE2.ZIP, documentation in PIEDOC.ZIP.
E-mail: Brent Ruggles
QuProlog
Platforms: UNIX, Linux, beta for MAC
Available: http://www.itee.uq.edu.au/~pjr/HomePages/QuPrologHome.html
E-mail:
Info: Extended WAM with support for quantifiers and substitutions, multi-threaded, high-level communication.
Strawberry Prolog
Platforms: Windows 95/NT, plans for UNIX and Macintosh
Available: http://www.dobrev.com/
E-mail:
SWI Prolog
Platforms: Binaries for Linux, Windows (NT/2000/XP/Vista) and Mac OS X (darwin). Sources: ANSI-C, both 32 and 64-bit machines, compiles on almost all Unix systems and more.
Available: http://www.swi-prolog.org, ftp://ftp.tu-darmstadt.de/pub/programming/languages/prolog/swi-prolog/
Info: Complete, ISO and Edinburgh standard, common optimizations, GC including atoms. Portable graphics, multiple threads, constraints, comprehensive libraries for (semantic) web programming, Unicode, source-level debugger, advanced syntax colouring
License: LGPL
Trinc-Prolog
Platforms: Windows 95/98/NT 4.0, plans for Windows 2000, Linux and Sun Solaris
Available: http://www.trinc-prolog.com/
E-mail:
UPMAIL Tricia Prolog
Platforms: Apple Macintosh
Available: Anonymous FTP from ftp://ftp.csd.uu.se/pub/Tricia; get README first.
Info: UPMAIL is still available, but unsupported.
Visual Prolog
Platforms: Win32
Available: http://www.visual-prolog.com
Info: Includes all the facilities necessary to write mission critical commercial-grade applications. Fully visual development environment. Open architecture. Object-oriented. Built-in database system and ODBC support. Visual Prolog Personal Edition is available on a freeware license.
wamcc
Platforms: UNIX
Available: Anonymous FTP from ftp://ftp.inria.fr/INRIA/Projects/ChLoE/LOGIC_PROGRAMMING/wamcc
Info: Compiler which translates Prolog to C via WAM. Debuggers. Requires GNU C v.2.4.5 or higher.
Contact: Daniel Diaz
XGP
Platforms: Apple Macintosh OS X, 10.2.3+
Available: http://xgp.sourceforge.net/
Info: XGP is an open source (GPL) integrated development environment with user interface and graphics support based on gprolog and Cocoa under Macintosh OS X.
XSB
Platforms: Many, including SunOS, Linux and Windows
Available: http://xsb.sourceforge.net/
E-mail:
Info: system with SLG-resolution, HiLog syntax, and unification factoring.
Yap 4.2.0
Platforms: UNIX-based platforms and Windows
Available: http://www.ncc.up.pt/~vsc/Yap/
E-mail: Vitor Santos Costa
Info: Yap is entirely written in C and Prolog and should be portable to most 32-bit and 64-bit Unix based platforms. A Windows port is also available. Yap4.2 is distributed under Perl's artistic license and can be freely distributed.

Wednesday, July 2, 2008

Get Started with LISP

Lisp (programming language) - Wikipedia, the free encyclopedia
This is a great place to start especially if you're new to lisp. It gives the necessary introduction about the language and a simple quick discussion of its features then it provides you with resources about the language where you can go if you need more information.

Practical Common Lisp
The most appreciated book on LISP. The book is published in HTML format and the site offers a download of the source code (not the book itself).

LISP 1.5 Programmers Manual [PDF]
The first version of LISP

InterLisp Reference Manual [PDF]
A manual for the Interlisp dialect

ANSI LISP Standard
The ANSI standard for the language (Common LISP)

Tutorials: