How to resolve the algorithm Polymorphism step by step in the Haskell programming language
How to resolve the algorithm Polymorphism step by step in the Haskell programming language
Table of Contents
Problem Statement
Create two classes Point(x,y) and Circle(x,y,r) with a polymorphic function print, accessors for (x,y,r), copy constructor, assignment and destructor and every possible default constructors
Let's start with the solution:
Step by Step solution about How to resolve the algorithm Polymorphism step by step in the Haskell programming language
The code provided is in Haskell and defines data types for points and circles, along with instances for the Show
typeclass to provide custom string representations for these types. Additionally, it defines several constructors for circles that set specific values for the x, y, and radius properties.
Here is a breakdown of the code:
-
data Point = Point Integer Integer
: This defines a data type namedPoint
that represents a point in a two-dimensional space. It consists of twoInteger
fields,x
andy
, representing the x and y coordinates of the point. -
instance Show Point where
: This declares an instance of theShow
typeclass for thePoint
data type. TheShow
typeclass provides a way to convert a value into a string representation. Theshow
function defined here returns a string representation of aPoint
in the format "Point at x,y", wherex
andy
are the values of thex
andy
fields. -
ponXAxis = flip Point 0
: This defines a function namedponXAxis
that takes two arguments: a value of typeInteger
and a value of typePoint
. It returns a newPoint
value with the samex
coordinate as the input point and ay
coordinate of 0. -
ponYAxis = Point 0
: This defines a function namedponYAxis
that takes a value of typeInteger
as an argument and returns a newPoint
value with anx
coordinate of 0 and the samey
coordinate as the input value. -
porigin = Point 0 0
: This defines a variable namedporigin
of typePoint
with bothx
andy
coordinates set to 0, effectively representing the origin of the coordinate system. -
data Circle = Circle Integer Integer Integer
: This defines a data type namedCircle
that represents a circle in a two-dimensional space. It consists of threeInteger
fields:x
,y
, andr
, representing the x and y coordinates of the circle's center and its radius, respectively. -
instance Show Circle where
: This declares an instance of theShow
typeclass for theCircle
data type, providing a custom string representation. Theshow
function here returns a string representation of aCircle
in the format "Circle at x,y with radius r", wherex
,y
, andr
are the values of thex
,y
, andr
fields. -
conXAxis = flip Circle 0
: This defines a function namedconXAxis
that takes two arguments: a value of typeInteger
and a value of typeCircle
. It returns a newCircle
value with the samex
coordinate as the input circle and ay
coordinate of 0. -
conYAxis = Circle 0
: This defines a function namedconYAxis
that takes a value of typeInteger
as an argument and returns a newCircle
value with anx
coordinate of 0 and the samey
coordinate as the input value. -
catOrigin = Circle 0 0
: This defines a variable namedcatOrigin
of typeCircle
with bothx
andy
coordinates set to 0 and a radius of 0, effectively representing a circle centered at the origin with zero radius. -
c0OnXAxis = flip (flip Circle 0) 0
: This defines a function namedc0OnXAxis
that takes two arguments: a value of typeInteger
and a value of typeCircle
. It returns a newCircle
value with anx
coordinate of 0, ay
coordinate of the input value, and a radius of 0. -
c0OnYAxis = flip (Circle 0) 0
: This defines a function namedc0OnYAxis
that takes a value of typeInteger
as an argument and returns a newCircle
value with anx
coordinate of 0, ay
coordinate of 0, and a radius of the input value.
Source code in the haskell programming language
data Point = Point Integer Integer
instance Show Point where
show (Point x y) = "Point at "++(show x)++","++(show y)
-- Constructor that sets y to 0
ponXAxis = flip Point 0
-- Constructor that sets x to 0
ponYAxis = Point 0
-- Constructor that sets x and y to 0
porigin = Point 0 0
data Circle = Circle Integer Integer Integer
instance Show Circle where
show (Circle x y r) = "Circle at "++(show x)++","++(show y)++" with radius "++(show r)
-- Constructor that sets y to 0
conXAxis = flip Circle 0
-- Constructor that sets x to 0
conYAxis = Circle 0
-- Constructor that sets x and y to 0
catOrigin = Circle 0 0
--Constructor that sets y and r to 0
c0OnXAxis = flip (flip Circle 0) 0
--Constructor that sets x and r to 0
c0OnYAxis = flip (Circle 0) 0
You may also check:How to resolve the algorithm Mandelbrot set step by step in the ALGOL 68 programming language
You may also check:How to resolve the algorithm Return multiple values step by step in the NetRexx programming language
You may also check:How to resolve the algorithm File modification time step by step in the Gambas programming language
You may also check:How to resolve the algorithm Longest common substring step by step in the Ada programming language
You may also check:How to resolve the algorithm N'th step by step in the Kotlin programming language