[Haskell-beginners] Conciseness question

David Virebayre dav.vire+haskell at gmail.com
Mon Aug 8 09:42:03 CEST 2011


2011/8/7 Manfred Lotz <manfred.lotz at arcor.de>:
> Hi David,
>
> On Sun, 7 Aug 2011 11:19:21 -0400
> David Place <d at vidplace.com> wrote:
>
>> On Aug 7, 2011, at 11:07 AM, Manfred Lotz wrote:
>>
>> >
>> > 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 we are having difficulty understanding what you are try to
>> do.  Since, you are worried about conciseness, why not write out the
>> Haskell program that shows what you want to do without worrying about
>> conciseness.  Then we can look for opportunities to improve it.
>>
>
> You may be right. Here is what I want to do.
>
> I have some 15 directories as Strings, where I want to do certain
> actions like to create those directories, or doing some other things
> to all of those directories.
>
> For doing this something like this would be good (for the sake of
> simplicity I do not write down all 15 entries)
>
> dirLst = [ "somedir", "otherdir" ]
>
> main = do
>  map makeDir dirLst
>  ...
>
>
> Later on in the program I would need those directorie names to create
> (depending upon the context) new pathnames. Now something like this
> would be good.
>
> -- In my program instead of d1,...,d15
> -- I use meaningful names of course.
> d1 = "somedir"
> ...
> d15 = "otherdir"
>
>
> doSomething fls = do
>   let pathname = d1 ++ "/bin"
>   copyFiles2bin fls pathname
>
> ...
>

Why not

dirLst = [ ("somedir",doSomething fls),
           ("otherdir",doNothing),
           ("otherdir2",doNothing),
           ("otherdir3",doTheDance),
           ("otherdir4",doNothing)
           ("otherdir4",doDeeDooDa fls2 fls3)
         ]

doSomething fls d = do
  let pathname = d </> "bin"
  copyFiles2bin fls pathname

doTheDance d = do
  .....

doDeeDooDa f g d = do
  .....


doNothing _ = return ()

main = do
  mapM_ makeDir (map fst dirLst)
  mapM_ ((a,b) -> a b) dirLst


or even

main = do
  forM_ dirLst $ \(d,f) -> makeDir d >> f d


David.



More information about the Beginners mailing list