Send Beginners mailing list submissions to
        [email protected]

To subscribe or unsubscribe via the World Wide Web, visit
        http://www.haskell.org/mailman/listinfo/beginners
or, via email, send a message with subject or body 'help' to
        [email protected]

You can reach the person managing the list at
        [email protected]

When replying, please edit your Subject line so it is more specific
than "Re: Contents of Beginners digest..."


Today's Topics:

   1. Re:  How to avoid repeating code (Daniel Fischer)
   2.  How to think like a functional programmer? (Costello, Roger L.)
   3. Re:  How to avoid repeating code (Federico Mastellone)
   4. Re:  How to think like a functional programmer? (Christopher Done)
   5. Re:  How to think like a functional programmer? (I?aki Garay)
   6. Re:  How to think like a functional programmer? (Amy de Buitl?ir)


----------------------------------------------------------------------

Message: 1
Date: Thu, 26 May 2011 13:47:40 +0200
From: Daniel Fischer <[email protected]>
Subject: Re: [Haskell-beginners] How to avoid repeating code
To: [email protected]
Message-ID: <[email protected]>
Content-Type: Text/Plain;  charset="utf-8"

On Thursday 26 May 2011 02:23:06, Federico Mastellone wrote:
> Hi,
> 
> I created a Data.MultiMap module based on Data.Map and Data.Set like
> this:
> 
> data MultiMap k v = MultiMap (Map k (Set v))
> 
> and a Data.IntMultiMap module based on Data.IntMap and data.IntSet like
> this:
> 
> data IntMultiMap = IntMultiMap (IntMap IntSet)

Both of these would better be newtypes instead of data, I think.
Using data incurs some run-time overhead (the newtype doesn't exist at run-
time, only during compile-time [type checking phase], so it's a strictly 
controlled type alias in practice, making it easier [or possible at all] to 
apply optimisations available for the underlying type) due to the extra 
indirections via the constructor and introduces the new value
(MultiMap _|_), which complicates strictness analysis and optimisations in 
general.

> 
> For example the functions to add a value I wrote are:
> 
> For MultiMap:
> addValue :: k -> v -> MultiMap k v -> MultiMap k v
> addValue k v (MultiMap m) = MultiMap $ Map.insertWith (\new old ->
> Set.insert v old) k (Set.singleton v) m
> 
> For IntMultiMap:
> addValue :: Int -> Int -> IntMultiMap -> IntMultiMap
> addValue k v (IntMultiMap m) = IntMultiMap $ IntMap.insertWith (\new old
> -> IntSet.insert v old) k (IntSet.singleton v) m
> 
> Both modules look almost the same, with the same
> documentation, same behavior, same function names but with different
> type signatures.

Well, we have the same situation with Map/IntMap and Set/IntSet, so

> 
> Is there a way to make this simpler?

Not a really good one (at least, none I know).

> 
> The same thing happens to the modules that are using MultiMap and
> IntMultiMap, I have to write two versions of each.

You can reduce the code duplication at the use sites with a type class,

{-# LANGUAGE TypeFamilies #-}

class MultiMapClass m where
  type Key m
  type Value m
  empty :: m
  singleton :: Key m -> Value m -> m
  addValue :: Key m -> Value m -> m -> m
  ...

instance (Ord k, Ord v) => MultiMapClass (MultiMap k v) where
  type Key (MultiMap k v) = k
  type Value (MultiMap k v) = v
  empty = MultiMap Map.empty
  ...

instance MultiMapClass IntMultiMap where
  type Key IntMultiMap = Int
  type Value IntMultiMap = Int
  empty = IntMultiMap IntMap.empty
  ...

> 
> Thanks!



------------------------------

Message: 2
Date: Thu, 26 May 2011 12:27:01 -0400
From: "Costello, Roger L." <[email protected]>
Subject: [Haskell-beginners] How to think like a functional
        programmer?
To: "[email protected]" <[email protected]>
Message-ID:
        <[email protected]>
Content-Type: text/plain; charset="us-ascii"

Hi Folks,

I am working on a project and am trying hard to write the code in a functional 
style.

Sadly, upon reviewing what I've written, I realize that I am not succeeding. I 
just devised a recipe for solving the problem and then created functions 
corresponding to steps in the recipe.  I am still thinking imperatively, not 
functionally.

Is there a book or article that describes how to approach problems from a 
functional mindset?

How did you "flip the switch" in your brain to the functional mindset?

/Roger
-------------- next part --------------
An HTML attachment was scrubbed...
URL: 
<http://www.haskell.org/pipermail/beginners/attachments/20110526/a56987e4/attachment-0001.htm>

------------------------------

Message: 3
Date: Thu, 26 May 2011 13:47:39 -0300
From: Federico Mastellone <[email protected]>
Subject: Re: [Haskell-beginners] How to avoid repeating code
To: Daniel Fischer <[email protected]>
Cc: [email protected]
Message-ID: <[email protected]>
Content-Type: text/plain; charset="iso-8859-1"

On Thu, May 26, 2011 at 8:47 AM, Daniel Fischer <
[email protected]> wrote:

> On Thursday 26 May 2011 02:23:06, Federico Mastellone wrote:
> > Hi,
> >
> > I created a Data.MultiMap module based on Data.Map and Data.Set like
> > this:
> >
> > data MultiMap k v = MultiMap (Map k (Set v))
> >
> > and a Data.IntMultiMap module based on Data.IntMap and data.IntSet like
> > this:
> >
> > data IntMultiMap = IntMultiMap (IntMap IntSet)
>
> Both of these would better be newtypes instead of data, I think.
> Using data incurs some run-time overhead (the newtype doesn't exist at run-
> time, only during compile-time [type checking phase], so it's a strictly
> controlled type alias in practice, making it easier [or possible at all] to
> apply optimisations available for the underlying type) due to the extra
> indirections via the constructor and introduces the new value
> (MultiMap _|_), which complicates strictness analysis and optimisations in
> general.
>
>
Thanks for the tip, I use newtype where I can but I thought it would be
simpler here to explain my problem using data. Anyway, now I know better why
I should use newtype instead.


> >
> > For example the functions to add a value I wrote are:
> >
> > For MultiMap:
> > addValue :: k -> v -> MultiMap k v -> MultiMap k v
> > addValue k v (MultiMap m) = MultiMap $ Map.insertWith (\new old ->
> > Set.insert v old) k (Set.singleton v) m
> >
> > For IntMultiMap:
> > addValue :: Int -> Int -> IntMultiMap -> IntMultiMap
> > addValue k v (IntMultiMap m) = IntMultiMap $ IntMap.insertWith (\new old
> > -> IntSet.insert v old) k (IntSet.singleton v) m
> >
> > Both modules look almost the same, with the same
> > documentation, same behavior, same function names but with different
> > type signatures.
>
> Well, we have the same situation with Map/IntMap and Set/IntSet, so
>
>
Yes, both pairs, (Set, InSet), and (Map, IntMap), have the exact same
problem as my MultiMap module.

But I think that as your solution involves using non-standard extensions we
could not solve this problem in GHC's libraries. Am I OK?

>
> > Is there a way to make this simpler?
>
> Not a really good one (at least, none I know).
>
> >
> > The same thing happens to the modules that are using MultiMap and
> > IntMultiMap, I have to write two versions of each.
>
> You can reduce the code duplication at the use sites with a type class,
>
> {-# LANGUAGE TypeFamilies #-}
>

Is this a mostly experimental extension or I can use it safely? Is it used
around the GHC packages?


>
> class MultiMapClass m where
>  type Key m
>  type Value m
>  empty :: m
>  singleton :: Key m -> Value m -> m
>  addValue :: Key m -> Value m -> m -> m
>  ...
>
> instance (Ord k, Ord v) => MultiMapClass (MultiMap k v) where
>  type Key (MultiMap k v) = k
>  type Value (MultiMap k v) = v
>  empty = MultiMap Map.empty
>  ...
>
> instance MultiMapClass IntMultiMap where
>  type Key IntMultiMap = Int
>  type Value IntMultiMap = Int
>  empty = IntMultiMap IntMap.empty
>  ...
>
>
I'm going to read about this extension, try it and comment about it here.


> >
> > Thanks!
>

Thank you very much for your complete response, really helpful!

-- 
Federico Mastellone
Computer Science Engineer - ITBA

".. there are two ways of constructing a software design: One way is to make
it so simple that there are obviously no deficiencies, and the other way is
to make it so complicated that there are no obvious deficiencies. The first
method is far more difficult."

Tony Hoare, 1980 ACM Turing Award Lecture.
-------------- next part --------------
An HTML attachment was scrubbed...
URL: 
<http://www.haskell.org/pipermail/beginners/attachments/20110526/45344bee/attachment-0001.htm>

------------------------------

Message: 4
Date: Thu, 26 May 2011 19:14:13 +0200
From: Christopher Done <[email protected]>
Subject: Re: [Haskell-beginners] How to think like a functional
        programmer?
To: "Costello, Roger L." <[email protected]>
Cc: "[email protected]" <[email protected]>
Message-ID: <[email protected]>
Content-Type: text/plain; charset=UTF-8

On 26 May 2011 18:27, Costello, Roger L. <[email protected]> wrote:
> I am working on a project and am trying hard to write the code in a
> functional style.
>
> How did you ?flip the switch? in your brain to the functional mindset?

What's the project? Maybe we can offer some "how we'd do it" bits of
advice. I personally got some insights after some Haskellers rewrote a
couple of my functions in the way they'd do it when I was beginning.



------------------------------

Message: 5
Date: Thu, 26 May 2011 14:14:44 -0300
From: I?aki Garay <[email protected]>
Subject: Re: [Haskell-beginners] How to think like a functional
        programmer?
To: "Costello, Roger L." <[email protected]>
Cc: "[email protected]" <[email protected]>
Message-ID: <[email protected]>
Content-Type: text/plain; charset=UTF-8

> Sadly, upon reviewing what I?ve written, I realize that I am not succeeding.
> I just devised a recipe for solving the problem and then created functions
> corresponding to steps in the recipe. ?I am still thinking imperatively, not
> functionally.
>
>
>
> Is there a book or article that describes how to approach problems from a
> functional mindset?
>
>
>
> How did you ?flip the switch? in your brain to the functional mindset?
Examples.
Read lots of code. Don't worry if you don't understand all of it. But
don't just 'look' at it, run it in your mind.
Programming is like writing in that just like a novel writer, you read
10x the amount you write.
Rewrite the same code as you learn, note the differences.

Others will provide better recommendations than I wtr recommended sources.

Think of your problem not in term of what it does, but what data it
handles, and what it does to it.
Design your program as a set of boxes consuming and producing data,
then implement the boxes as functions.
If your data needs to be transformed in succesive steps, that's
sequential computation and part of the problem's nature, not
necessarily a result of an imperative mindset.
You can implement it with composed functions.

Functional programming can free you from having to deal with certain
'bookkeeping' aspects of imperative programming, but it doesn't free
you from having to design a solution to the program.
e.g mapping over a sequence is better than having to write a loop and
control iteration, but you still had to think "do/apply X to all these
Y".

good luck,
I?aki Garay.

-----BEGIN GEEK CODE BLOCK-----
Version: 3.1
GCS/GMU d- s:- a25 C++ UL+++ P--- E- W+ PS+++ PE- Y+ t++ 5+ tv- b++
DI+ D--- G e h! r y++
------END GEEK CODE BLOCK------



------------------------------

Message: 6
Date: Thu, 26 May 2011 18:20:02 +0100
From: Amy de Buitl?ir <[email protected]>
Subject: Re: [Haskell-beginners] How to think like a functional
        programmer?
To: "Costello, Roger L." <[email protected]>
Cc: "[email protected]" <[email protected]>
Message-ID: <[email protected]>
Content-Type: text/plain; charset="windows-1252"

On 26 May 2011 17:27, Costello, Roger L. <[email protected]> wrote:

> How did you ?flip the switch? in your brain to the functional mindset?
>
>
For me, one thing that changed is that I now tend to focus more on the data
*transformations* rather than on the data itself. I tend now to group
similar transformations into modules. (Before, as an OO programmer, I would
have thought of each module as a class of objects, so I would have grouped
everything that acts on that object or that the object does into one class.)
Eventually you want to build a chain of transformations that takes the input
and produces the output you want.

Another way to visualise it: Instead of thinking of the steps to solve a
problem, think of the solution as a series of definitions. Write a
definition (an equationof of the result you want in terms of simpler things.
Write definitions of those simpler things in terms of things that are even
simpler. And so on, until you can build a pyramid of definitions that
defines the final output in terms of the input.

I found it helpful to post a few small code samples on this forum, and to
ask for suggestions for a better implementation.

Good luck!
-------------- next part --------------
An HTML attachment was scrubbed...
URL: 
<http://www.haskell.org/pipermail/beginners/attachments/20110526/c6bbae4e/attachment.htm>

------------------------------

_______________________________________________
Beginners mailing list
[email protected]
http://www.haskell.org/mailman/listinfo/beginners


End of Beginners Digest, Vol 35, Issue 51
*****************************************

Reply via email to