Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to import an exclamation point (or other operator) from Haskell module

Haskell has a Data.Map module which includes, among other functions, a ! function.

fromList [(5,'a'), (3,'b')] ! 1    Error: element not in the map
fromList [(5,'a'), (3,'b')] ! 5 == 'a'

While I can import other functions from the Data.Map module into my code...

import Data.Map(Map, keys, fromList)

...the following does NOT work...

import Data.Map(Map, keys, fromList, !)

I get the following error:

parse error on input `!'

What is the correct syntax to import items like !?

like image 672
Mark Hildreth Avatar asked Aug 14 '12 02:08

Mark Hildreth


People also ask

How do I import a module in Haskell?

The syntax for importing modules in a Haskell script is import <module name>. This must be done before defining any functions, so imports are usually done at the top of the file. One script can, of course, import several modules. Just put each import statement into a separate line.

What does exclamation mark mean in Haskell?

It's a strictness declaration. Basically, it means that it must be evaluated to what's called "weak head normal form" when the data structure value is created.

What is a qualified import in Haskell?

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

What does module where do in Haskell?

A module in Haskell serves the dual purpose of controlling name-spaces and creating abstract data types.


1 Answers

The correct answer is to wrap the function name (really, it's an operator: a special case of a function) in parentheses, like so...

import Data.Map(Map, keys, fromList, (!))
like image 157
Mark Hildreth Avatar answered Sep 18 '22 00:09

Mark Hildreth