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:  single responsibility principle vs functional
      programming (Joe Van Dyk)
   2.  Getting random values (Tyler Hayes)
   3.  Re: Getting random values (Volkan YAZICI)
   4. Re:  Brainfuck interpreter stack overflow (Edgar Klerks)
   5.  function nesting again (Zbigniew Stanasiuk)
   6. Re:  function nesting again (Magnus Therning)
   7. Re:  function nesting again (Felipe Lessa)
   8.  Re: function nesting again (Zbigniew Stanasiuk)
   9. Re:  single responsibility principle vs functional
      programming (Daniel Fischer)
  10. Re:  function nesting again (Daniel Fischer)


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

Message: 1
Date: Wed, 27 Jan 2010 16:45:30 -0800
From: Joe Van Dyk <[email protected]>
Subject: Re: [Haskell-beginners] single responsibility principle vs
        functional      programming
To: Henk-Jan van Tuyl <[email protected]>
Cc: beginners <[email protected]>
Message-ID:
        <[email protected]>
Content-Type: text/plain; charset=UTF-8

I'm not saying to directly translate a OOP project to Haskell using
the same design, but rather to take the business requirements of the
OOP project and write them in Haskell.

(So, both versions do the same thing, but the design of them will be
completely different)

Joe

On Wed, Jan 27, 2010 at 3:27 PM, Henk-Jan van Tuyl <[email protected]> wrote:
>
> I have the experience that you better start from scratch when learning a new
> programming paradigm, do not try to write object oriented programs in
> Haskell. A great description of how to use the functional programming
> paradigm can be found in "Why Functional Programming Matters" [1]. Maybe
> also interesting is "The Monad.Reader/Issue3/Functional Programming vs
> Object Oriented Programming" [2].
>
> [1] http://www.cs.chalmers.se/~rjmh/Papers/whyfp.html
> [2]
> http://www.haskell.org/haskellwiki/The_Monad.Reader/Issue3/Functional_Programming_vs_Object_Oriented_Programming
>
> Regards,
> Henk-Jan van Tuyl
>
>
> --
> http://Van.Tuyl.eu/
> http://members.chello.nl/hjgtuyl/tourdemonad.html
> --
>
>
> On Wed, 27 Jan 2010 23:43:27 +0100, Joe Van Dyk <[email protected]>
> wrote:
>
>> Thanks, I'm starting to get it.
>>
>> Perhaps what I need to do is take a small standard OOP system and try
>> converting it to FP.
>>
>> Or perhaps there's already examples of such a thing I could look at?
>>
>> Joe
>>
>> On Wed, Jan 27, 2010 at 11:43 AM, Stephen Blackheath [to
>> Haskell-Beginners] <[email protected]>
>> wrote:
>>>
>>> Joe,
>>>
>>> IMHO:
>>>
>>> Classes in OOP have several purposes, one of which is to manage the
>>> mutation
>>> of state so as to put a lid on the complexity that could result.  The
>>> inside
>>> of an OOP class is usually a C program, and the outside is (ideally) a
>>> semi-functional program with interfaces that are simple at the level of
>>> abstraction where they operate.  The single responsibility principle
>>> exists
>>> to stop the C programs getting big and messy, and to help the programmer
>>> to
>>> build neat layers of abstraction on top of each other by having more
>>> abstract classes constructed out of less abstract ones.
>>>
>>> Functional programming essentially takes the C part away and makes the
>>> whole
>>> program into a layering of abstractions, even at the lowest level.
>>>
>>> An FP function more-or-less maps to an OOP class, and the reason why it
>>> can
>>> be a much lighter-weight structure is because the need to manage mutation
>>> has been removed.  I think the same principle applies, though - an FP
>>> function should have only one responsibility.
>>>
>>> Perhaps in FP the need for this principle to be explicitly stated and
>>> carefully observed is not so great, though.  If an FP function does two
>>> things and can be split up, it's not such a big deal, because
>>> re-factoring
>>> is easy in an FP language.  By comparison in OOP, splitting a big messy
>>> class up is a lot of work with a great risk of breakage. This amplifies
>>> the
>>> importance of applying the single responsibility principle before you
>>> start
>>> writing the class.
>>>
>>>
>>> Steve
>>>
>>> Joe Van Dyk wrote:
>>>>
>>>> So I'm still pretty new to functional programming.
>>>>
>>>> In OOP, you have the concept of each class having a
>>>> single-responsibility (or reason for changing).  See
>>>> http://en.wikipedia.org/wiki/Single_responsibility_principle
>>>>
>>>> I'm having problems seeing how that concept maps to functional
>>>> programming.  Perhaps it doesn't?
>>>>
>>>
>>
>>
>>
>
>
> --
>



-- 
Joe Van Dyk
http://fixieconsulting.com


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

Message: 2
Date: Wed, 27 Jan 2010 20:52:49 -0800
From: Tyler Hayes <[email protected]>
Subject: [Haskell-beginners] Getting random values
To: Haskell Beginners <[email protected]>
Message-ID: <[email protected]>
Content-Type: text/plain

First of all, how do you get a simple random integer in Haskell?

Second, How does one get a random value from a list of non-integer
values?  For example, I would like to pull a random musical note from a
list of notes using the Haskore library.

Thanks,
Tyler



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

Message: 3
Date: Thu, 28 Jan 2010 10:56:03 +0200
From: Volkan YAZICI <[email protected]>
Subject: [Haskell-beginners] Re: Getting random values
To: Tyler Hayes <[email protected]>
Cc: Haskell Beginners <[email protected]>
Message-ID: <[email protected]>
Content-Type: text/plain; charset=us-ascii

On Wed, 27 Jan 2010, Tyler Hayes <[email protected]> writes:
> First of all, how do you get a simple random integer in Haskell?

  Prelude Random> getStdGen >>= \g -> let (a, g') = random g in return a
  1627750401

Pay attention to reusing new generator state pointed by g'.

> Second, How does one get a random value from a list of non-integer
> values?

  list !! (length list `mod` random...)


Regards.


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

Message: 4
Date: Thu, 28 Jan 2010 10:17:37 +0100
From: Edgar Klerks <[email protected]>
Subject: Re: [Haskell-beginners] Brainfuck interpreter stack overflow
To: [email protected], [email protected]
Message-ID:
        <[email protected]>
Content-Type: text/plain; charset="iso-8859-1"

He Felipe,

I think you are right. Now I essentially have 2 data tapes, while I wanted a
grid. The results of up and down are independent of left and right. I have
to redesign it again.

With kind regards,

Edgar
-------------- next part --------------
An HTML attachment was scrubbed...
URL: 
http://www.haskell.org/pipermail/beginners/attachments/20100128/1a65a70b/attachment-0001.html

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

Message: 5
Date: Thu, 28 Jan 2010 11:18:12 +0000 (UTC)
From: Zbigniew Stanasiuk <[email protected]>
Subject: [Haskell-beginners] function nesting again
To: [email protected]
Message-ID: <[email protected]>
Content-Type: text/plain; charset=us-ascii

Hallo all,

I have two functions f and g of one arguments:

f :: (Num a) => [a] -> [a]
g :: (Num a) => [a] -> [a]

I can do composition (f .f .f.) [a] or (g .g .g.) [a] for a few repeated 
functions.
And what about if I want to nest f, say, many many times?

The matter is more complicated (from my prespective :-) ) if I would like to 
get  e.g.:
the composition (g .f .g. g.f .f) [a]
if t == 0 then f else g  
and having the list [1,0,1,1,0,0]  as a pattern to construct above composition.

How to make above, generally for arbitrary length ???



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

Message: 6
Date: Thu, 28 Jan 2010 11:40:16 +0000
From: Magnus Therning <[email protected]>
Subject: Re: [Haskell-beginners] function nesting again
To: Zbigniew Stanasiuk <[email protected]>
Cc: [email protected]
Message-ID:
        <[email protected]>
Content-Type: text/plain; charset=UTF-8

On Thu, Jan 28, 2010 at 11:18, Zbigniew Stanasiuk <[email protected]> wrote:
> Hallo all,
>
> I have two functions f and g of one arguments:
>
> f :: (Num a) => [a] -> [a]
> g :: (Num a) => [a] -> [a]
>
> I can do composition (f .f .f.) [a] or (g .g .g.) [a] for a few repeated
> functions.
> And what about if I want to nest f, say, many many times?
>
> The matter is more complicated (from my prespective :-) ) if I would like to
> get  e.g.:
> the composition (g .f .g. g.f .f) [a]
> if t == 0 then f else g
> and having the list [1,0,1,1,0,0]  as a pattern to construct above 
> composition.
>
> How to make above, generally for arbitrary length ???

I believe what you want is a fold over a list of functions, where this
list has been created by mapping the if-statement above of a list of
Bools (I know you used 0,1 above, but True,False is a better choice in
my opinion).  So, in other words, something like this:

composeFnG l = foldl (.) id $ map chooseForG l
    where
        chooseForG b = if b then f else g

or a bit shorter

composeFnG = foldl (.) id . map (\ b -> if b then f else g)

/M

-- 
Magnus Therning                        (OpenPGP: 0xAB4DFBA4)
magnus@therning.org          Jabber: magnus@therning.org
http://therning.org/magnus         identi.ca|twitter: magthe


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

Message: 7
Date: Thu, 28 Jan 2010 09:52:39 -0200
From: Felipe Lessa <[email protected]>
Subject: Re: [Haskell-beginners] function nesting again
To: [email protected]
Message-ID: <[email protected]>
Content-Type: text/plain; charset=us-ascii

On Thu, Jan 28, 2010 at 11:18:12AM +0000, Zbigniew Stanasiuk wrote:
> Hallo all,
>
> I have two functions f and g of one arguments:
>
> f :: (Num a) => [a] -> [a]
> g :: (Num a) => [a] -> [a]
>
> I can do composition (f .f .f.) [a] or (g .g .g.) [a] for a few repeated
> functions.
> And what about if I want to nest f, say, many many times?

Use this:

  iterate :: (t -> t) -> t -> [t]

For example

  Prelude> take 10 $ iterate (*2) 1
  [1,2,4,8,16,32,64,128,256,512]

That means that if you want to nest your function 'x' times, then
you just take the x-th element of the list.

  Prelude> let x = 10 in iterate (*2) 1 !! x
  1024

> The matter is more complicated (from my prespective :-) ) if I would like to
> get  e.g.:
> the composition (g .f .g. g.f .f) [a]
> if t == 0 then f else g
> and having the list [1,0,1,1,0,0]  as a pattern to construct above 
> composition.
>
> How to make above, generally for arbitrary length ???

You could roll on your own functions, for example

  composition :: [t -> t] -> t -> t
  composition = foldr (.) id

  composition' :: [t -> t] -> [Int] -> t -> t
  composition' fs is = composition [fs !! i | i <- is]

Note that you would them as

  composition [g, f, g, g, f, f]

or

  composition' [f, g] [1, 0, 1, 1, 0, 0]

You could also write 'composition' as

  import Data.Monoid

  composition :: [t -> t] -> t -> t
  composition = appEndo . mconcat . map Endo

'map Endo' just puts everything in the Endo monoid, while
'appEndo' takes the result from the monoid.  In other words,
'composition' is the same as a concatenation in the Endo monoid.
It may be defined as:

  newtype Endo a = Endo {appEndo :: a -> a}

  instance Monoid (Endo a) where
    mempty                    = Endo id
    mappend (Endo f) (Endo g) = Endo (f.g)

Do you see the pattern? :)

HTH,

--
Felipe.


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

Message: 8
Date: Thu, 28 Jan 2010 12:48:03 +0000 (UTC)
From: Zbigniew Stanasiuk <[email protected]>
Subject: [Haskell-beginners] Re: function nesting again
To: [email protected]
Message-ID: <[email protected]>
Content-Type: text/plain; charset=us-ascii

Zbigniew Stanasiuk <zbigergofun <at> gmail.com> writes:

> 
> Hallo all,
> 
> I have two functions f and g of one arguments:
> 
> f :: (Num a) => [a] -> [a]
> g :: (Num a) => [a] -> [a]
> 
Big thanks to Magnus and Filipe for smart and practical solutions of my 
problem. Up to now I didn't touch monoids although intuitively it seems
very elegant.

Best regards,
Zbig






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

Message: 9
Date: Thu, 28 Jan 2010 14:45:29 +0100
From: Daniel Fischer <[email protected]>
Subject: Re: [Haskell-beginners] single responsibility principle vs
        functional      programming
To: [email protected]
Message-ID: <[email protected]>
Content-Type: text/plain;  charset="utf-8"

Am Donnerstag 28 Januar 2010 01:45:30 schrieb Joe Van Dyk:
> I'm not saying to directly translate a OOP project to Haskell using
> the same design, but rather to take the business requirements of the
> OOP project and write them in Haskell.
>
> (So, both versions do the same thing, but the design of them will be
> completely different)
>
> Joe

Ah, okay, that's fine, then. I think

> >> Perhaps what I need to do is take a small standard OOP system and try
> >> converting it to FP.

gave the wrong impression.


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

Message: 10
Date: Thu, 28 Jan 2010 14:49:43 +0100
From: Daniel Fischer <[email protected]>
Subject: Re: [Haskell-beginners] function nesting again
To: [email protected]
Cc: Magnus Therning <[email protected]>
Message-ID: <[email protected]>
Content-Type: text/plain;  charset="utf-8"

Am Donnerstag 28 Januar 2010 12:40:16 schrieb Magnus Therning:
> On Thu, Jan 28, 2010 at 11:18, Zbigniew Stanasiuk <[email protected]> 
wrote:
> > Hallo all,
> >
> > I have two functions f and g of one arguments:
> >
> > f :: (Num a) => [a] -> [a]
> > g :: (Num a) => [a] -> [a]
> >
> > I can do composition (f .f .f.) [a] or (g .g .g.) [a] for a few
> > repeated functions.
> > And what about if I want to nest f, say, many many times?
> >
> > The matter is more complicated (from my prespective :-) ) if I would
> > like to get  e.g.:
> > the composition (g .f .g. g.f .f) [a]
> > if t == 0 then f else g
> > and having the list [1,0,1,1,0,0]  as a pattern to construct above
> > composition.
> >
> > How to make above, generally for arbitrary length ???
>
> I believe what you want is a fold over a list of functions, where this
> list has been created by mapping the if-statement above of a list of
> Bools (I know you used 0,1 above, but True,False is a better choice in
> my opinion).  So, in other words, something like this:
>
> composeFnG l = foldl (.) id $ map chooseForG l
>     where
>         chooseForG b = if b then f else g
>
> or a bit shorter
>
> composeFnG = foldl (.) id . map (\ b -> if b then f else g)
>
> /M

Better use foldr, at least if f and (or) g are lazy.



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

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


End of Beginners Digest, Vol 19, Issue 28
*****************************************

Reply via email to