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

3

58

I understand import Data.List.

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

Smelser answered 30/6, 2012 at 14:28 Comment(0)
S
65

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.

Synsepalous answered 30/6, 2012 at 14:30 Comment(0)
E
48

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.

Eisenberg answered 30/6, 2012 at 14:32 Comment(0)
A
17

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'.

Apposition answered 30/6, 2012 at 14:32 Comment(0)

© 2022 - 2024 — McMap. All rights reserved.