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:  cascade of if statements (Daniel Trstenjak)
   2. Re:  cascade of if statements (Emmanuel Touzery)
   3.  Pattern guard inside function (Nathan H?sken)
   4. Re:  Pattern guard inside function (Lyndon Maydwell)
   5.  Netwire loop (Nathan H?sken)
   6. Re:  cascade of if statements (Daniel Trstenjak)
   7. Re:  Pattern guard inside function (Bob Hutchison)
   8. Re:  cascade of if statements (Daniel Trstenjak)
   9. Re:  cascade of if statements (Emmanuel Touzery)


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

Message: 1
Date: Wed, 31 Oct 2012 12:05:13 +0100
From: Daniel Trstenjak <[email protected]>
Subject: Re: [Haskell-beginners] cascade of if statements
To: [email protected]
Message-ID: <20121031110513.GB3984@machine>
Content-Type: text/plain; charset=us-ascii


Hi Emmanuel,

On Wed, Oct 31, 2012 at 11:25:05AM +0100, Emmanuel Touzery wrote:
> But this function is already in the IO monad (maybe I'm thinking about
> this in the wrong way though).

A huge step in understanding monads for myself was to realize, that
you can put any monadic computation (IO is the exception) inside an
other monadic computation.

e.g.

ioComputation :: IO ()
ioComputation = do
   let maybe = do maybeComputation1
                  maybeComputation2
                  ...
   return ()


Most monads have some kind of execution function, like the state monad 
(runState, evalState):

ioComputation :: IO ()
   let initalState = ...
       result      = evalState $ do stateComputation1
                                    stateComputation2
                     initalState
   return ()



In your case the Maybe monad doesn't help that much, because the
functions you're calling doesn't return a Maybe value.


In your case, why having a 'needToFetchAgain' and probably a 'fetch'
function instead of just a 'refetch' function, which encapsulates
the whole fetching?


Greetings,
Daniel



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

Message: 2
Date: Wed, 31 Oct 2012 12:48:40 +0100
From: Emmanuel Touzery <[email protected]>
Subject: Re: [Haskell-beginners] cascade of if statements
To: [email protected]
Message-ID:
        <cac42renvx-tu5pqq9cauws9haoyfi9ynp3opnuzek1pd+kb...@mail.gmail.com>
Content-Type: text/plain; charset="iso-8859-1"

Thank you, I didn't realize I can simply put the maybe computation inside
the IO as you've shown. And I must research runState/evalState a bit more.
But yes it doesn't map so well to maybe. The parrallel just came to my mind.

The reason for splitting needToFetchAgain out was to keep function size
small and the code readable.

And in the caller function I can nicely write:

    needToFetchAgain <- needToFetchAgain export_filename
    when needToFetchAgain $ do
       ....

'when' coming from Control.Monad.

Well so I'll leave this function as it is for now and make a note to read
some more about runState/evalState.

Thank you!

Emmanuel

On Wed, Oct 31, 2012 at 12:05 PM, Daniel Trstenjak <
[email protected]> wrote:

>
> Hi Emmanuel,
>
> On Wed, Oct 31, 2012 at 11:25:05AM +0100, Emmanuel Touzery wrote:
> > But this function is already in the IO monad (maybe I'm thinking about
> > this in the wrong way though).
>
> A huge step in understanding monads for myself was to realize, that
> you can put any monadic computation (IO is the exception) inside an
> other monadic computation.
>
> e.g.
>
> ioComputation :: IO ()
> ioComputation = do
>    let maybe = do maybeComputation1
>                   maybeComputation2
>                   ...
>    return ()
>
>
> Most monads have some kind of execution function, like the state monad
> (runState, evalState):
>
> ioComputation :: IO ()
>    let initalState = ...
>        result      = evalState $ do stateComputation1
>                                     stateComputation2
>                      initalState
>    return ()
>
>
>
> In your case the Maybe monad doesn't help that much, because the
> functions you're calling doesn't return a Maybe value.
>
>
> In your case, why having a 'needToFetchAgain' and probably a 'fetch'
> function instead of just a 'refetch' function, which encapsulates
> the whole fetching?
>
>
> Greetings,
> Daniel
>
> _______________________________________________
> Beginners mailing list
> [email protected]
> http://www.haskell.org/mailman/listinfo/beginners
>
-------------- next part --------------
An HTML attachment was scrubbed...
URL: 
<http://www.haskell.org/pipermail/beginners/attachments/20121031/e4da6fdf/attachment-0001.htm>

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

Message: 3
Date: Wed, 31 Oct 2012 15:02:45 +0100
From: Nathan H?sken <[email protected]>
Subject: [Haskell-beginners] Pattern guard inside function
To: [email protected]
Message-ID: <[email protected]>
Content-Type: text/plain; charset=ISO-8859-1

Hey

I have a function which originaly locked like this:

collInfo :: Rect -> Rect -> Maybe CollInfo
collInfo (Rect x1 y1 w1 h1) (Rect x2 y2 w2 h2)
  | x1 + w1 < x2 = Nothing
  | x1 > x2 + w2 = Nothing
  | y1 + h1 < y2 = Nothing
  | y1 > y2 + h2 = Nothing
  | otherwiese = Just $ makeCollInfo r1 r2

Now I want to refactor it to take Maybe input values

collInfo' :: Maybe Rect -> Maybe Rect -> Maybe CollInfo
collInfo' mr1 mr2 =
  r1 <- mr1
  r2 <- mr2
  collInfo r1 r2

This is fine, but I would like to write it using only one function. Is
there any possibility I can remove collInfo and merge its body into
collInfo' and still somehow use the guards (and not a bunch of ifs)?

Thanks,
Nathan




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

Message: 4
Date: Wed, 31 Oct 2012 22:16:18 +0800
From: Lyndon Maydwell <[email protected]>
Subject: Re: [Haskell-beginners] Pattern guard inside function
To: Nathan H?sken <[email protected]>
Cc: [email protected]
Message-ID:
        <CAM5QZtwboKf16fULTA-A2=h0skot7dxylhw-s4ldn_c9ecp...@mail.gmail.com>
Content-Type: text/plain; charset=UTF-8

If you want to try some higher order operations on your existing
function, take a look at 'liftM2' and 'join' in Control.Monad. They
won't disappoint!

On Wed, Oct 31, 2012 at 10:02 PM, Nathan H?sken
<[email protected]> wrote:
> Hey
>
> I have a function which originaly locked like this:
>
> collInfo :: Rect -> Rect -> Maybe CollInfo
> collInfo (Rect x1 y1 w1 h1) (Rect x2 y2 w2 h2)
>   | x1 + w1 < x2 = Nothing
>   | x1 > x2 + w2 = Nothing
>   | y1 + h1 < y2 = Nothing
>   | y1 > y2 + h2 = Nothing
>   | otherwiese = Just $ makeCollInfo r1 r2
>
> Now I want to refactor it to take Maybe input values
>
> collInfo' :: Maybe Rect -> Maybe Rect -> Maybe CollInfo
> collInfo' mr1 mr2 =
>   r1 <- mr1
>   r2 <- mr2
>   collInfo r1 r2
>
> This is fine, but I would like to write it using only one function. Is
> there any possibility I can remove collInfo and merge its body into
> collInfo' and still somehow use the guards (and not a bunch of ifs)?
>
> Thanks,
> Nathan
>
>
> _______________________________________________
> Beginners mailing list
> [email protected]
> http://www.haskell.org/mailman/listinfo/beginners



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

Message: 5
Date: Wed, 31 Oct 2012 17:21:05 +0100
From: Nathan H?sken <[email protected]>
Subject: [Haskell-beginners] Netwire loop
To: [email protected]
Message-ID: <[email protected]>
Content-Type: text/plain; charset=ISO-8859-1

Hi,
When testing this netwire sample program

{-# LANGUAGE Arrows #-}

import Control.Wire

mainWire :: WireP () Double
mainWire = proc i -> do
  rec
    value <- integral_ 0.0 -< oldValue
    oldValue <- delay 1.0 -< value
  returnA -< value

main = do
  wireLoop mainWire

wireLoop :: WireP () Double -> IO ()
wireLoop w' = do
    let (mx, w) = stepWireP w' 1.0 ()
    case mx of
      Left ex -> putStrLn $ "Inhibited:" ++ (show ex)
      Right x -> putStrLn $ "Produced:" ++ (show x)
    wireLoop w

I get this output:
NetwireTest: <<loop>>

But there should be not infinite loop, should there?
Regards,
Nathan



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

Message: 6
Date: Wed, 31 Oct 2012 18:14:52 +0100
From: Daniel Trstenjak <[email protected]>
Subject: Re: [Haskell-beginners] cascade of if statements
To: [email protected]
Message-ID: <20121031171452.GA21461@machine>
Content-Type: text/plain; charset=us-ascii


Hi Emmanuel,

you could write monadic boolean operators (<&&>, <||>) and a "monadic"
when (whenM) - I think both are in some package - to be able to write somwthing 
like:

whenM (not <$> doesFileExist file <||> olderThan twoHours file) $ do
   ...


import Control.Applicative ((<$>))

(<&&>) :: Monad m => m Bool -> m Bool -> m Bool
(<&&>) m1 m2 = do
   r1 <- m1
   if r1 then m2 else return False


(<||>) :: Monad m => m Bool -> m Bool -> m Bool
(<||>) m1 m2 = do
   r1 <- m1
   if r1 then return True else m2


whenM :: Monad m => m Bool -> m () -> m ()
whenM p m = do
   r <- p
   if r then m else return ()


olderThan :: Int -> FilePath -> IO Bool
olderThan secs file = do
   modif   <- getModificationTime file
   curTime <- getClockTime
   let diff = diffClockTimes curTime modif
   return $ tdSec diff >= secs


twoHours = 3600 * 2


'not <$> doesFileExist file' is a shorcut for:
r <- doesFileExist file
return $ not r


But all of this would mostly only really pay off, if you have some file
operations heavy code. Otherwise, all of these helper functions (operators) are 
quite generic.

Obviously, I'm currently a bit bored ...


Greetings,
Daniel



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

Message: 7
Date: Wed, 31 Oct 2012 13:22:52 -0400
From: Bob Hutchison <[email protected]>
Subject: Re: [Haskell-beginners] Pattern guard inside function
To: Nathan H?sken <[email protected]>
Cc: [email protected]
Message-ID: <[email protected]>
Content-Type: text/plain; charset=iso-8859-1

Hi,

On 2012-10-31, at 10:02 AM, Nathan H?sken <[email protected]> wrote:

> Hey
> 
> I have a function which originaly locked like this:
> 
> collInfo :: Rect -> Rect -> Maybe CollInfo
> collInfo (Rect x1 y1 w1 h1) (Rect x2 y2 w2 h2)
>  | x1 + w1 < x2 = Nothing
>  | x1 > x2 + w2 = Nothing
>  | y1 + h1 < y2 = Nothing
>  | y1 > y2 + h2 = Nothing
>  | otherwiese = Just $ makeCollInfo r1 r2
> 
> Now I want to refactor it to take Maybe input values
> 
> collInfo' :: Maybe Rect -> Maybe Rect -> Maybe CollInfo
> collInfo' mr1 mr2 =
>  r1 <- mr1
>  r2 <- mr2
>  collInfo r1 r2
> 
> This is fine, but I would like to write it using only one function. Is
> there any possibility I can remove collInfo and merge its body into
> collInfo' and still somehow use the guards (and not a bunch of ifs)?

maybe like:

collInfo'' :: Maybe Rect -> Maybe Rect -> Maybe CollInfo
collInfo'' (Just r1@(Rect x1 y1 w1 h1)) (Just r2@(Rect x2 y2 w2 h2))
  | x1 + w1 < x2 = Nothing
  | x1 > x2 + w2 = Nothing
  | y1 + h1 < y2 = Nothing
  | y1 > y2 + h2 = Nothing
  | otherwise = Just $ makeCollInfo r1 r2
collInfo'' _ _ = Nothing

Cheers,
Bob

> 

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




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

Message: 8
Date: Wed, 31 Oct 2012 18:26:14 +0100
From: Daniel Trstenjak <[email protected]>
Subject: Re: [Haskell-beginners] cascade of if statements
To: [email protected]
Message-ID: <20121031172614.GB21461@machine>
Content-Type: text/plain; charset=us-ascii


On Wed, Oct 31, 2012 at 06:14:52PM +0100, Daniel Trstenjak wrote:
> (<&&>) :: Monad m => m Bool -> m Bool -> m Bool
> (<&&>) m1 m2 = do
>    r1 <- m1
>    if r1 then m2 else return False
> 
> 
> (<||>) :: Monad m => m Bool -> m Bool -> m Bool
> (<||>) m1 m2 = do
>    r1 <- m1
>    if r1 then return True else m2

That the operators behave like the boolean ones we should have the same fixity 
declarations:

infixr 3 (<&&>)
infixr 2 (<||>)


Greetings,
Daniel



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

Message: 9
Date: Wed, 31 Oct 2012 23:03:30 +0100
From: Emmanuel Touzery <[email protected]>
Subject: Re: [Haskell-beginners] cascade of if statements
To: [email protected]
Message-ID:
        <cac42reka+klvrhfd-ctb2nsuog2daaatsrfufbpacss3bsh...@mail.gmail.com>
Content-Type: text/plain; charset="iso-8859-1"

Thank you that is very interesting... this is really impressive with
haskell that it's so "easy" to add such operators, that they don't need to
be builtin.

Thanks for your time, it was not wasted!

On Wed, Oct 31, 2012 at 6:26 PM, Daniel Trstenjak <
[email protected]> wrote:

>
> On Wed, Oct 31, 2012 at 06:14:52PM +0100, Daniel Trstenjak wrote:
> > (<&&>) :: Monad m => m Bool -> m Bool -> m Bool
> > (<&&>) m1 m2 = do
> >    r1 <- m1
> >    if r1 then m2 else return False
> >
> >
> > (<||>) :: Monad m => m Bool -> m Bool -> m Bool
> > (<||>) m1 m2 = do
> >    r1 <- m1
> >    if r1 then return True else m2
>
> That the operators behave like the boolean ones we should have the same
> fixity declarations:
>
> infixr 3 (<&&>)
> infixr 2 (<||>)
>
>
> Greetings,
> Daniel
>
> _______________________________________________
> Beginners mailing list
> [email protected]
> http://www.haskell.org/mailman/listinfo/beginners
>
-------------- next part --------------
An HTML attachment was scrubbed...
URL: 
<http://www.haskell.org/pipermail/beginners/attachments/20121031/2b578f5a/attachment.htm>

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

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


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

Reply via email to