codesnippets:avoidconflictprelude
Differences
This shows you the differences between two versions of the page.
| Next revision | Previous revision | ||
| codesnippets:avoidconflictprelude [2021/03/17 19:30] – f2b216 | codesnippets:avoidconflictprelude [2025/10/08 00:48] (current) – external edit 127.0.0.1 | ||
|---|---|---|---|
| Line 1: | Line 1: | ||
| - | ====== Avoiding name conflict with prelude | + | ====== Avoiding name conflict with Prelude |
| + | The avoidance of prelude may make sense for the following reasons: | ||
| + | * reprogramming prelude functions to understand what it does | ||
| + | * reprogramming prelude functions to create improved versions | ||
| + | * avoiding conflicts with same name | ||
| + | |||
| + | Methods: | ||
| + | * Hide functions from prelude. | ||
| * The directive '' | * The directive '' | ||
| * If you import '' | * If you import '' | ||
| - | * Alternatively you can import the funtions that you need from module Prelude if they do not conflict with other functions. | + | * Alternatively, you can import the funtions that you need from module Prelude if they do not conflict with other functions. |
| NOTE: Examples work with GHC 8.10.4. Has not been tested with other versions. | NOTE: Examples work with GHC 8.10.4. Has not been tested with other versions. | ||
| ===== Example #1 ===== | ===== Example #1 ===== | ||
| + | |||
| + | Hide functions from prelude. | ||
| + | |||
| + | <code Haskell> | ||
| + | module Main where | ||
| + | |||
| + | import Prelude hiding( reverse ) | ||
| + | |||
| + | main :: IO () | ||
| + | main = do | ||
| + | putStrLn $ reverse " | ||
| + | |||
| + | |||
| + | reverse :: [a] -> [a] | ||
| + | reverse l = rev l [] | ||
| + | where | ||
| + | rev [] acc = acc | ||
| + | rev (e:r) acc = rev r (e : acc) | ||
| + | </ | ||
| + | |||
| + | |||
| + | ===== Example #2 ===== | ||
| Qualified import of preload overrides implicit import. | Qualified import of preload overrides implicit import. | ||
| Line 34: | Line 63: | ||
| </ | </ | ||
| - | ===== Example #2 ===== | + | ===== Example #3 ===== |
| The same with directive '' | The same with directive '' | ||
| Line 62: | Line 91: | ||
| </ | </ | ||
| - | ===== Example #3 ===== | + | ===== Example #4 ===== |
| With import of all functions that are needed only. | With import of all functions that are needed only. | ||
| Line 88: | Line 117: | ||
| </ | </ | ||
| - | ===== Example #4 ===== | + | ===== Example #5 ===== |
| Shows what happens without measures, like the above. | Shows what happens without measures, like the above. | ||
| Line 120: | Line 149: | ||
| </ | </ | ||
| - | ===== Example #5 ===== | + | ===== Example #6 ===== |
| Shows what happens with directive '' | Shows what happens with directive '' | ||
| Line 151: | Line 180: | ||
| + | ===== ✎ ===== | ||
| + | ~~DISCUSSION~~ | ||
codesnippets/avoidconflictprelude.1616005841.txt.gz · Last modified: (external edit)
