codesnippets:recordsyntax
Differences
This shows you the differences between two versions of the page.
| Next revision | Previous revision | ||
| codesnippets:recordsyntax [2021/03/20 19:57] – created f2b216 | codesnippets:recordsyntax [2025/10/08 00:48] (current) – external edit 127.0.0.1 | ||
|---|---|---|---|
| Line 1: | Line 1: | ||
| ====== Record syntax ====== | ====== Record syntax ====== | ||
| + | * algebraic sum type )< | ||
| + | * provides access functions | ||
| + | * fields in curly brackets, comma separated | ||
| + | |||
| + | * short example: | ||
| + | * <code Haskell> | ||
| + | data MatrixCursor = MatrixCursor { rnRow :: Integer, rnCol :: Integer } | ||
| + | </ | ||
| + | |||
| + | ---- | ||
| + | |||
| + | )< | ||
| ===== Advantages===== | ===== Advantages===== | ||
| Line 32: | Line 44: | ||
| ==== With sum types combined: risk of runtime errors ==== | ==== With sum types combined: risk of runtime errors ==== | ||
| - | |||
| - | An algebraic sum type (see also https:// | ||
| * example: | * example: | ||
| Line 47: | Line 57: | ||
| </ | </ | ||
| - | * output: | + | * output |
| * < | * < | ||
| Test2-exe.exe: | Test2-exe.exe: | ||
| Line 54: | Line 64: | ||
| ==== Risk of namespace colisions without strong coding rules ==== | ==== Risk of namespace colisions without strong coding rules ==== | ||
| + | Fields from records can be extrated by the accessor functions that have been declared within the record. Alternatively, | ||
| - | <code Haskell> | + | <WRAP center round info 62%> |
| + | Risk can be completely excluded by compiling with <wrap em>ghc parameter -Wall</ | ||
| + | </WRAP> | ||
| + | <WRAP center round info 62%> | ||
| + | Risk can be mitigated by <wrap em> | ||
| + | </ | ||
| + | |||
| + | |||
| + | |||
| + | * example 1: | ||
| + | * <code Haskell> | ||
| module Main where | module Main where | ||
| main :: IO () | main :: IO () | ||
| main = do | main = do | ||
| - | print $ nCol xc | + | print $ nCol xcToPrint |
| {-| to be able to print into a ' | {-| to be able to print into a ' | ||
| Line 76: | Line 97: | ||
| deriving Show | deriving Show | ||
| - | xc :: MatrixCursor | + | xcToPrint |
| - | xc = incMatrixCursorCol 5 $ incMatrixCursorCol 2 $ incMatrixCursorCol 3 $ initMatrixCursor | + | xcToPrint |
| incMatrixCursorCol :: Integer -> MatrixCursor -> MatrixCursor | incMatrixCursorCol :: Integer -> MatrixCursor -> MatrixCursor | ||
| - | incMatrixCursorCol n (MatrixCursor | + | incMatrixCursorCol n xc@(MatrixCursor |
| initMatrixCursor :: MatrixCursor | initMatrixCursor :: MatrixCursor | ||
| Line 86: | Line 107: | ||
| </ | </ | ||
| - | * compiler | + | * compiler |
| - | * < | + | * < |
| - | app\Main.hs: | + | [1 of 1] Compiling Main ( app\Main.hs, |
| - | This binding for `nRow' shadows the existing binding | + | |
| - | defined at app\Main.hs: | + | app\Main.hs: |
| + | This binding for `nCol' shadows the existing binding | ||
| + | defined at app\Main.hs: | ||
| | | | | ||
| - | 25 | incMatrixCursorCol n (MatrixCursor nRow nCol) = (MatrixCursor nRow (nCol+n)) | + | 24 | |
| - | | + | |
| + | Linking app\Main.exe ... | ||
| + | </ | ||
| + | |||
| + | |||
| + | |||
| + | * example 2: | ||
| + | * <code Haskell> | ||
| + | module Main where | ||
| + | |||
| + | main :: IO () | ||
| + | main = do | ||
| + | print $ nCol xcToPrint | ||
| + | |||
| + | {-| to be able to print into a ' | ||
| + | |||
| + | * prefix: xc | ||
| + | * instance of Formatting to print the stored characters | ||
| + | -} | ||
| + | data MatrixCursor = | ||
| + | MatrixCursor { | ||
| + | -- | the row, starting at 0 | ||
| + | nRow :: Integer, | ||
| + | -- | the column starting at 0 | ||
| + | nCol :: Integer } | ||
| + | deriving Show | ||
| + | |||
| + | xcToPrint :: MatrixCursor | ||
| + | xcToPrint = incMatrixCursorCol 5 $ incMatrixCursorCol 2 $ incMatrixCursorCol 3 $ initMatrixCursor | ||
| + | |||
| + | incMatrixCursorCol :: Integer -> MatrixCursor -> MatrixCursor | ||
| + | incMatrixCursorCol n xc@(MatrixCursor nRow nCol) = (MatrixCursor (nRow xc) (nCol+n)) | ||
| + | |||
| + | initMatrixCursor :: MatrixCursor | ||
| + | initMatrixCursor = (MatrixCursor 0 0) | ||
| + | </ | ||
| + | |||
| + | * compiler output: | ||
| + | * <code Haskell> | ||
| + | [2 of 2] Compiling Main | ||
| + | |||
| + | app\Main.hs: | ||
| + | * Couldn' | ||
| + | with actual type `Integer' | ||
| + | * The function `nRow' is applied to one argument, | ||
| + | but its type `Integer' | ||
| + | In the first argument of `MatrixCursor', | ||
| + | In the expression: (MatrixCursor (nRow xc) (nCol + n)) | ||
| + | | | ||
| + | 24 | | ||
| + | | ||
| + | </ | ||
| + | |||
| + | |||
| + | |||
| + | * example 3 (warning free with '' | ||
| + | * <code Haskell> | ||
| + | module Main where | ||
| + | |||
| + | main :: IO () | ||
| + | main = do | ||
| + | print $ rnCol xcToPrint | ||
| + | |||
| + | {-| to be able to print into a ' | ||
| + | |||
| + | * prefix: xc | ||
| + | * instance of Formatting to print the stored characters | ||
| + | -} | ||
| + | data MatrixCursor = | ||
| + | MatrixCursor { | ||
| + | -- | the row, starting at 0 | ||
| + | rnRow :: Integer, | ||
| + | -- | the column starting at 0 | ||
| + | rnCol :: Integer } | ||
| + | deriving Show | ||
| + | |||
| + | xcToPrint :: MatrixCursor | ||
| + | xcToPrint = incMatrixCursorCol 5 $ incMatrixCursorCol 2 $ incMatrixCursorCol 3 $ initMatrixCursor | ||
| + | |||
| + | incMatrixCursorCol :: Integer -> MatrixCursor -> MatrixCursor | ||
| + | incMatrixCursorCol n xc@(MatrixCursor _ nCol) = (MatrixCursor (rnRow xc) (nCol+n)) | ||
| + | |||
| + | initMatrixCursor :: MatrixCursor | ||
| + | initMatrixCursor = (MatrixCursor 0 0) | ||
| + | </ | ||
| + | |||
| + | * output: | ||
| + | * < | ||
| + | 10 | ||
| </ | </ | ||
| + | ===== ✎ ===== | ||
| + | ~~DISCUSSION~~ | ||
codesnippets/recordsyntax.1616266664.txt.gz · Last modified: (external edit)
