Talk:Haskell/Modules

From Wikibooks, the open-content textbooks collection

Jump to: navigation, search

[edit] encapsulation

there should be something on how not exporting a constructor can hide information. if no expert takes on the job within the next few days, I am happy to do it.

[edit] Needs some clarification

I'm learning from this wikibook, so I can't make the changes myself.. there ought to be a real example of a module definition, not just the syntax. I'm not quite clear on how 'where' works (at first I thought it was a syntactical marker) after reading this. --207.14.29.3 23:22, 19 July 2007 (UTC)

I agree. there should be one toy module completely defined. I'm doing it.

[edit] How to express Main.(++++)?

I define a infix function (++++) in module Main.

Sorry, could you elaborate on your question? If you want to import/export infix functions, you have to wrap them around parentheses, like import Foo.Bar ( foo, (++++), etc ). Were you talking about that? -- Kowey 07:05, 2 August 2007 (UTC)

for example:

module Main where
(+)::[a]->[a]->[a]
a + b = a++b
main = do
    print ([1] + [2])
    return ()

It says "Ambiguous",because + is also define in Prelude.

How to explicitly point it's Main.+?

Infix operators can be qualified
  Hugs> 1 Prelude.+ 2
  3
  Hugs> (Prelude.+) 1 2
  3

Note that you may have to hide the original (+) and import the Prelude qualified in order to redefine (+) in terms of the old one:

  module Main( (+) ) where

  import Prelude hiding ( (+) )
  import Prelude qualified

  (+) :: Num a => [a]->[a]->[a]
  a + b = zipWith (Prelude.+) a b

-- apfeλmus 09:42, 3 August 2007 (UTC)