Point geometrical data type - tuple versus record?

I can imagine three ways to define a geometric point as a type of algebraic data in Haskell. I want to define it as a type of algebraic data for better documentation.

As a type of tuple:

data Point a = Point a a

Like a tuple:

data Point a= Point (a,a)

As a record:

data Point a = Point { pointx :: a, pointy :: a}

What is better design / style and why?

+4
source share
3 answers

I think it depends mainly on your use case.

Although I would never use data Point = Point (a, a)because it introduces one layer of indirection that inflates the data structure - with two machine words - one for Pointand one for a pointer pointing to a tuple inside it.

type Point a = (a,a), newtype Point a = Point (a,a), , .

( ) , pr1 pr2 " / ". .

@nikitavolkov - , ,

{-# LANGUAGE TemplateHaskell -#}
{-# LANGUAGE DeriveFoldable -#}
{-# LANGUAGE DeriveFunctor -#}
{-# LANGUAGE DeriveTraversable -#}
{-# LANGUAGE RecordWildCards #-}

import Control.Lens

data Point a = Point { _pr1 :: !a
                     , _pr2 :: !a
                     } deriving (..)
$(makeLenses ''Point)

norm Point{..} = sqrt (_pr1^2 + _pr2^2) RecordWildCards .

(2017-09-03):

, , -. , , : Data.Strict.Maybe

, Maybe return ⊥ >>= f = ⊥, f ⊥.

, , / .

+5

- Haskell. , - , , .

, , Haskellers . .

:

{-# LANGUAGE DeriveFoldable, DeriveFunctor, DeriveTraversable #-}

data Point a =
  Point !a !a
  deriving (Functor, Foldable, Traversable)

, .

+4

In my opinion, the tuple is more rigid because it is difficult to create any changes in tuples. The best of them is the last.

-2
source

Source: https://habr.com/ru/post/1626736/


All Articles