Square | Triangle | |
---|---|---|
Perimeter | squareP |
triangleP |
Area | squareA |
triangleA |
Фигура = данные, которые ее определяют
enum SHAPE { SQUARE, TRIANGLE };
struct square_t {
double side;
};
struct triangle_t {
double side1, side2, side3;
};
struct figure_t {
enum SHAPE shape;
union {
struct square_t square_data;
struct triangle_t triangle_data;
} figure_data;
};
double square_p(struct square_t square) {
return square.side * 4;
}
double triangle_p(struct triangle_t triangle) {
return triangle.side1 + triangle.side2 + triangle.side3;
}
double perimeter(struct figure_t figure) {
switch (figure.shape) {
case SQUARE:
return square_p(figure.figure_data.square_data);
case TRIANGLE:
return triangle_p(figure.figure_data.triangle_data);
}
}
data Figure
= Square Double
| Triangle Double Double Double
perimeter (Square side) = side * 4
perimeter (Triangle side1 side2 side3) = side1 + side2 + side3
Square | Triangle | |
---|---|---|
Perimeter | squareP |
triangleP |
Area | squareA |
triangleA |
TCP | UDP | |
---|---|---|
send | tcpSend |
udpSend |
recv | tcpRecv |
udpRecv |
data Transport = TCP Socket | UDP Socket
send :: Transport -> ByteString -> IO ()
send (TCP sock) packet = ...
send (UDP sock) packet = ...
TCP | UDP | |
---|---|---|
send | tcpSend |
udpSend |
recv | tcpRecv |
udpRecv |
TCP | UDP | |
---|---|---|
send | tcpSend |
udpSend |
recv | tcpRecv |
udpRecv |
data Transport = Transport
{ send :: ByteString -> IO ()
, recv :: Int -> IO ByteString
}
tcp :: Socket -> Transport
tcp sock =
Transport
{ send = \packet ->
Network.send sock packet
, recv = \bytes ->
Network.recv sock bytes
}
data Figure = Figure
{ area :: Double
, perimeter :: Double
}
square :: Double -> Figure
square side = Figure
{ area = side^2
, perimeter = side * 4
}
Примеры:
equals
union
Решения: