Compound data type
You are encouraged to solve this task according to the task description, using any language you may know.
Data Structure
This illustrates a data structure, a means of storing data within a program.
Create a compound data type Point(x,y).
A compound data type is one that holds multiple independent values. See also Enumeration.
Ada
Tagged Type
Ada tagged types are extensible through inheritance. The reserved word tagged causes the compiler to create a tag for the type. The tag identifies the position of the type in an inheritance hierarchy.
type Point is tagged record X : Integer := 0; Y : Integer := 0; end record;
Record Type
Ada record types are not extensible through inheritance. Without the reserved word tagged the record does not belong to an inheritance hierarchy.
type Point is record X : Integer := 0; Y : Integer := 0; end record;
Parameterized Types
An Ada record type can contain a discriminant. The discriminant is used to choose between internal structural representations. Parameterized types were introduced to Ada before tagged types. Inheritance is generally a cleaner solution to multiple representations than is a parameterized type.
type Person (Gender : Gender_Type) is record Name : Name_String; Age : Natural; Weight : Float; Case Gender is when Male => Beard_Length : Float; when Female => null; end case; end record;
In this case every person will have the attributes of gender, name, age, and weight. A person with a male gender will also have a beard length.
BASIC
TYPE Point x AS INTEGER y AS INTEGER END TYPE
C
Libraries: Standard
typedef struct Point { int x; int y; } Point;
C++
struct Point { int x; int y; };
It is also possible to add a constructor (this allows the use of Point(x, y) in expressions):
struct Point { int x; int y; Point(int ax, int ay): x(ax), y(ax) {} };
Point can also be parametrized on the coordinate type:
template<typename Coordinate> struct point { Coordinate x, y; }; // A point with integer coordinates Point<int> point1 = { 3, 5 }; // a point with floating point coordinates Point<float> point2 = { 1.7, 3.6 };
Of course, a constructor can be added in this case as well.
C#
struct Point { public int x, y; public Point(int x, int y) { this.x = x; this.y = y; } }
Clean
Record type
:: Point = { x :: Int, y :: Int }
Parameterized Algebraic type
:: Point a = Point a a // usage: (Point Int)
Synonym type
:: Point :== (Int, Int)
Common Lisp
(defstruct point x y)
E
def makePoint(x, y) { def point { to getX() { return x } to getY() { return y } } return point }
Forth
There is no standard structure syntax in Forth, but it is easy to define words for creating and accessing data structures.
: pt>x ( point -- x ) ; : pt>y ( point -- y ) CELL+ ; : .pt ( point -- ) dup pt>x @ . pt>y @ . ; \ or for this simple structure, 2@ . . create point 6 , 0 , 7 point pt>y ! .pt \ 6 7
Some Forths have mechanisms for declaring complex structures. For example, GNU Forth uses this syntax:
struct cell% field pt>x cell% field pt>y end-struct point%
Fortran
In ISO Fortran 90 or later, use a TYPE declaration, "constructor" syntax, and field delimiter syntax:
PROGRAM TYPEDEMO TYPE RATIONAL ! Type declaration INTEGER :: NUMERATOR INTEGER :: DENOMINATOR END TYPE RATIONAL TYPE( RATIONAL ), PARAMETER :: ZERO = RATIONAL( 0, 1 ) ! Variables initialized by constructor syntax TYPE( RATIONAL ), PARAMETER :: ONE = RATIONAL( 1, 1 ) TYPE( RATIONAL ), PARAMETER :: HALF = RATIONAL( 1, 2 ) INTEGER :: N, HALFD. HALFN TYPE( RATIONAL ), DIMENSION(20) :: & ONE_OVER_N = (/ (RATIONAL( 1, N ), N = 1, 20) /) ! Array initialized with constructor inside implied-do initializer INTEGER, DIMENSION(20) :: OON_DENOMS HALFD = HALF%DENOMINATOR ! field access with "%" delimiter HALFN = HALF%NUMERATOR OON_DEMOMS(:) = ONE_OVER_N(:)%DENOMINATOR ! Access denominator field in every rational array element ! & store as integer array END PROGRAM TYPEDEMO
Haskell
Tagged Type
data Point = Point Integer Integer instance Show Point where show (Point x y) = "("++(show x)++","++(show y)++")" p = Point 6 7
Record Type
data Point = Point { x :: Integer, y :: Integer } deriving (Eq, Show)
The deriving clause here provides default instances for equality and conversion to string.
Different equivalent ways of constructing a point:
p = Point 2 3 p' = Point { x=4, y=5 }
Tuple Type
The declared type is a type synonym.
type Point = (2,3) p = (2,3)
Discriminated Type
data Person = Male { name :: String, age :: Integer, weight :: Double, beard_length :: Double } | Female { name :: String, age :: Integer, weight :: Double } deriving (Eq, Show)
Note that the field names may be identical in alternatives.
IDL
point = {x: 6 , y: 0 } point.y = 7 print, point ;=> { 6 7}
Java
public class Point { public int x, y; public Point() { this(0); } public Point(int x0) { this(x0,0); } public Point(int x0, int y0) { x = x0; y = y0; } public static void main(String args[]) { Point point = new Point(1,2); System.out.println("x = " + point.x ); System.out.println("y = " + point.y ); } }
JavaScript
var point = new Object(); point.x = 1; point.y = 2;
JSON
var point = { x:1, y:2 };
Logo
In Logo, a point is represented by a list of two numbers. For example, this will draw a triangle:
setpos [100 100] setpos [100 0] setpos [0 0] show pos ; [0 0]
Access is via normal list operations like FIRST and BUTFIRST (BF). X is FIRST point, Y is LAST point. For example, a simple drawing program which exits if mouse X is negative:
until [(first mousepos) < 0] [ifelse button? [pendown] [penup] setpos mousepos]
MAXScript
Point is a built-in object type in MAX, so...
struct myPoint (x, y) newPoint = myPoint x:3 y:4
In practice however, you'd use MAX's built in Point2 type
newPoint = Point2 3 4
OCaml
type point = < x: int; y: int >
OpenEdge/Progress
The temp-table is a in memory database table. So you can query sort and iterate it, but is the data structure that comes closest.
def temp-table point field x as int field y as int .
Another option would be a simple class.
Perl
This is a hash (associative array), but accomplishes the task.
my %point = ( x => 3, y => 8 );
PHP
# Using pack/unpack $point = pack("ii", 1, 2);
$u = unpack("ix/iy", $point); echo $x; echo $y;
list($x,$y) = unpack("ii", $point); echo $x; echo $y;
Pop11
uses objectclass; define :class Point; slot x = 0; slot y = 0; enddefine;
Python
The simplest way it to use a tuple, or a list if it should be mutable:
X, Y = 0, 1 p = (3, 4) p = [3, 4] print p[X]
If needed, you can use class:
class Point: def __init__(self, x=0, y=0): self.x = x self.y = y p = Point() print p.x
One could also simply instantiate a generic object and "monkeypatch" it:
point = object() point.x, point.y = 0, 1
Ruby
Point = Struct.new(:x,:y) p = Point.new(6,7) p.y=3 puts p => #<struct Point x=6, y=3>
Scheme
(define (make-point x y) (cons x y)) (define (point-x point) (car point)) (define (point-y point) (cdr point))
Tcl
This appears to be a sub-functionality of a proper associative array:
array set point {x 4 y 5} set point(y) 7 puts "Point is {$point(x),$point(y)}" # => Point is {4,7}