[Haskell-beginners] Conciseness question

Hein Hundal hundalhh at yahoo.com
Sun Aug 7 18:00:13 CEST 2011


Manfred Wrote:
> Hmm, not quite sure I'm understanding why Data.Map would
> help.
> 
> What I want to have is something like the following without
> the
> verboseness of it:
> 
> -- this is a minimal example. 
> -- Assume I would have some 15 such values.
> a = "bla"
> b = "bla2"
> 
> valList = [ a, b ]
> 
> Now in my program on the one hand I want to do something
> with all
> values, thus:  map doSomething valList
> 
> On the other hand I frequently want use single values in my
> program,
> like e.g.:
>    let x =  "suffix " ++ a
> 
> 
> What I don't want to do is to write a definition like
> valList
> because if I add a new value I have to remind myself not to
> forget to
> add it to valList too.

Hi Manfred,

   I think that the Map type is about the best you are going to get.  (I am a beginner, so take my advice with a few grains of salt.)

   Here is some code that does something like what you want.


--  Start Code

import Data.Map as M

-- Makes a Map 
m1 = M.fromList [('a', "bla"), ('b', "bla2"), ('c', "bla3")]

-- Function for looking up values in m1
look c = M.findWithDefault "fail" c m1

valList = M.keys m1

showSuffix = "suffix " ++ look 'a'

-- Add three !  to the end of every string in m1
m2 = fmap ( ++"!!!") m1

-- Add a new key-value pair
m3 = M.insert 'd' "blabla" m2


--  End Start Code

Cheers,
Hein




More information about the Beginners mailing list