What does 'qualified' mean in 'import qualified Data.List' statement?

后端 未结 3 1337
一整个雨季
一整个雨季 2020-12-14 06:17

I understand import Data.List.

But what does qualified mean in the statement import qualified Data.List?

相关标签:
3条回答
  • 2020-12-14 06:41

    A qualified import makes the imported entities available only in qualified form, e.g.

    import qualified Data.List
    
    result :: [Int]
    result = Data.List.sort [3,1,2,4]
    

    With just import Data.List, the entities are available in qualified form and in unqualified form. Usually, just doing a qualified import leads to too long names, so you

    import qualified Data.List as L
    
    result :: [Int]
    result = L.sort [3,1,2,4]
    

    A qualified import allows using functions with the same name imported from several modules, e.g. map from the Prelude and map from Data.Map.

    0 讨论(0)
  • 2020-12-14 06:44

    If you do an unqualified import (the default), you can refer to anything imported just by its name.

    If you do a qualified import, you have to prefix the name with the module it's imported from.

    E.g.,

    import Data.List (sort)
    

    This is an unqualified import. You can now say either sort or Data.List.sort.

    import qualified Data.List (sort)
    

    This is a qualified import. Now sort by itself doesn't work, and you have to say Data.List.sort.

    Because that's quite lengthy, normally you say something like

    import qualified Data.List (sort) as LS
    

    and now you can write LS.sort, which is shorter.

    0 讨论(0)
  • 2020-12-14 07:03

    The keyword qualified means that symbols in the imported modules are not imported into the unqualified (prefixless) namespace. They are only available with their full qualified name. For instance, foldr' has the unqualified name foldr' and the qualified name Data.List.foldr'.

    One uses qualified imports to prevent namespace pollution. It is also possible to use import qualified Foo as Bar, which imports from Foo but with names as if the import stems from Bar. For instance, if you type import qualified Data.List as L, you can use foldr' as L.foldr'.

    0 讨论(0)
提交回复
热议问题