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:  help with IO guards (Julian Birch)
   2. Re:  help with IO guards (Dimitri DeFigueiredo)
   3. Re:  help with IO guards (Mike Meyer)
   4. Re:  help with IO guards (Miro Karpis)


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

Message: 1
Date: Thu, 15 Jan 2015 12:51:53 +0000
From: Julian Birch <[email protected]>
To: "[email protected]" <[email protected]>,  The
        Haskell-Beginners Mailing List - Discussion of primarily
        beginner-level topics related to Haskell <[email protected]>
Subject: Re: [Haskell-beginners] help with IO guards
Message-ID:
        <cab0tuzbvjdwlz+dumhhk_gg4fvjxyblmsnkz-q7xbusb_5f...@mail.gmail.com>
Content-Type: text/plain; charset="utf-8"

Here's how I would do it:

Write two functions. One (f) takes the int and gets the IO record. One
(g) takes a (non-IO) record and returns the string. Now you can use a bind
(=<<) to combine the two.

Once you've got that working, you can move the bits into the where clause
of your original function.

Hope this helps.

J

On Thursday, January 15, 2015, Miro Karpis <[email protected]>
wrote:

> Hi,
>
> please is there a way to have guards with 'where' that communicates with
> IO? Or is there some other more elegant way? I can do this with classic
> if/else,...but I just find it nicer with guards.
>
>
> I have something like this (just an example):
>
>
> f :: Int -> IO String
> f x
>     | null dbOutput = return "no db record"
>     | otherwise = return "we got some db records"
>     where dbOutput = getDBRecord x
>
>
> getDBRecord :: Int -> IO [Int]
> getDBRecord recordId = do
>     putStrLn $ "checking dbRecord" ++ show recordId
>     --getting data from DB
>     return [1,2]
>
>
> problem is that db dbOutput is IO and the guard check does not like it:
>
> Couldn't match expected type ?[a0]? with actual type ?IO [Int]?
>     In the first argument of ?null?, namely ?dbOutput?
>     In the expression: null dbOutput
>
>
>
> Cheers,
> Miro
>


-- 
Sent from an iPhone, please excuse brevity and typos.
-------------- next part --------------
An HTML attachment was scrubbed...
URL: 
<http://www.haskell.org/pipermail/beginners/attachments/20150115/b8d237eb/attachment-0001.html>

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

Message: 2
Date: Thu, 15 Jan 2015 13:24:30 -0200
From: Dimitri DeFigueiredo <[email protected]>
To: [email protected]
Subject: Re: [Haskell-beginners] help with IO guards
Message-ID: <[email protected]>
Content-Type: text/plain; charset="windows-1252"; Format="flowed"

I would not say that the problem is with the guard check. The problem is 
with 'null'. It's type is

Prelude> :t null
null :: [a] -> Bool

So, it expects a list of something, rather than an IO of something, 
whence the complaint.

Dimitri


On 15/01/15 09:51, Miro Karpis wrote:
> Hi,
>
> please is there a way to have guards with 'where' that communicates 
> with IO? Or is there some other more elegant way? I can do this with 
> classic if/else,...but I just find it nicer with guards.
>
>
> I have something like this (just an example):
>
>
> f :: Int -> IO String
> f x
>     | null dbOutput = return "no db record"
>     | otherwise = return "we got some db records"
>     where dbOutput = getDBRecord x
>
>
> getDBRecord :: Int -> IO [Int]
> getDBRecord recordId = do
>     putStrLn $ "checking dbRecord" ++ show recordId
>     --getting data from DB
>     return [1,2]
>
>
> problem is that db dbOutput is IO and the guard check does not like it:
>
> Couldn't match expected type ?[a0]? with actual type ?IO [Int]?
>     In the first argument of ?null?, namely ?dbOutput?
>     In the expression: null dbOutput
>
>
>
> Cheers,
> Miro
>
>
> _______________________________________________
> 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/20150115/e40a0c92/attachment-0001.html>

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

Message: 3
Date: Thu, 15 Jan 2015 10:42:27 -0600
From: Mike Meyer <[email protected]>
To: The Haskell-Beginners Mailing List - Discussion of primarily
        beginner-level topics related to Haskell <[email protected]>
Subject: Re: [Haskell-beginners] help with IO guards
Message-ID:
        <CAD=7U2BwVB5O=ph-8w8pnk_nvcaydcn+h6_co6+7w84myau...@mail.gmail.com>
Content-Type: text/plain; charset="utf-8"

On Thu, Jan 15, 2015 at 9:24 AM, Dimitri DeFigueiredo <
[email protected]> wrote:

>  I would not say that the problem is with the guard check. The problem is
> with 'null'. It's type is
>
> Prelude> :t null
> null :: [a] -> Bool
>
> So, it expects a list of something, rather than an IO of something, whence
> the complaint.
>


While that's the source of the error, the problem is the combination of the
guard check and where to bind a value in the IO monad.

Guard checks must have a value of Bool. getDBRecord returns something of
type IO [Int]. Where just binds a name, so you either need a way to extract
the [Int] from the return value before binding it in the where, or a
function of type IO [Int] -> Bool for the guard.

Note that this isn't an IO issue but a monad issue. There isn't a monad
method that returns a value not in the monad, so you can't  write either of
the two options above using monad methods. The best solution is the one
already proposed - write a function from [Int] -> IO String, and use bind
(>>=) on that function to handle things. You could also use the do sugaring
of <- to get a less functional version.

The last option is to use the IO-specific function unsafePerformIO to write
something like nullIO = null . unsafePerformIO. But it's called UNSAFE and
tucked away in a module of similar operations for a reason. Using bind is
much preferred.


> On 15/01/15 09:51, Miro Karpis wrote:
>
>   Hi,
>
>  please is there a way to have guards with 'where' that communicates with
> IO? Or is there some other more elegant way? I can do this with classic
> if/else,...but I just find it nicer with guards.
>
>
>  I have something like this (just an example):
>
>
> f :: Int -> IO String
> f x
>     | null dbOutput = return "no db record"
>     | otherwise = return "we got some db records"
>     where dbOutput = getDBRecord x
>
>
> getDBRecord :: Int -> IO [Int]
> getDBRecord recordId = do
>     putStrLn $ "checking dbRecord" ++ show recordId
>     --getting data from DB
>     return [1,2]
>
>
>  problem is that db dbOutput is IO and the guard check does not like it:
>
> Couldn't match expected type ?[a0]? with actual type ?IO [Int]?
>     In the first argument of ?null?, namely ?dbOutput?
>     In the expression: null dbOutput
>
>
>
>  Cheers,
>  Miro
>
>
> _______________________________________________
> Beginners mailing 
> [email protected]http://www.haskell.org/mailman/listinfo/beginners
>
>
>
> _______________________________________________
> 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/20150115/ff8a16b9/attachment-0001.html>

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

Message: 4
Date: Thu, 15 Jan 2015 21:44:29 +0100
From: Miro Karpis <[email protected]>
To: The Haskell-Beginners Mailing List - Discussion of primarily
        beginner-level topics related to Haskell <[email protected]>
Subject: Re: [Haskell-beginners] help with IO guards
Message-ID:
        <CAJnnbxGpnuTUXBrQTpriA54YKe8Gp7+e0KMiR3NJir-1=xe...@mail.gmail.com>
Content-Type: text/plain; charset="utf-8"

Many thanks everyone,.. but I'm not so confident with monads. If I
understand I could translate with them for example IO String to String? If
this is true I'm having troubles to achieve this.

Here is my simple test (only to test the logic), which ends with error:

 Couldn't match type ?[]? with ?IO?
    Expected type: IO Char
      Actual type: String
    In the expression: f a
    In the second argument of ?(>>=)?, namely ?(\ a -> f a)?



g = readLn
    >>= (\a -> f a)

f :: String -> String
f s_ = s_ ++ "!"



Cheers, Miro

On Thu, Jan 15, 2015 at 5:42 PM, Mike Meyer <[email protected]> wrote:

> On Thu, Jan 15, 2015 at 9:24 AM, Dimitri DeFigueiredo <
> [email protected]> wrote:
>
>>  I would not say that the problem is with the guard check. The problem is
>> with 'null'. It's type is
>>
>> Prelude> :t null
>> null :: [a] -> Bool
>>
>> So, it expects a list of something, rather than an IO of something,
>> whence the complaint.
>>
>
>
> While that's the source of the error, the problem is the combination of
> the guard check and where to bind a value in the IO monad.
>
> Guard checks must have a value of Bool. getDBRecord returns something of
> type IO [Int]. Where just binds a name, so you either need a way to extract
> the [Int] from the return value before binding it in the where, or a
> function of type IO [Int] -> Bool for the guard.
>
> Note that this isn't an IO issue but a monad issue. There isn't a monad
> method that returns a value not in the monad, so you can't  write either of
> the two options above using monad methods. The best solution is the one
> already proposed - write a function from [Int] -> IO String, and use bind
> (>>=) on that function to handle things. You could also use the do sugaring
> of <- to get a less functional version.
>
> The last option is to use the IO-specific function unsafePerformIO to
> write something like nullIO = null . unsafePerformIO. But it's called
> UNSAFE and tucked away in a module of similar operations for a reason.
> Using bind is much preferred.
>
>
>> On 15/01/15 09:51, Miro Karpis wrote:
>>
>>   Hi,
>>
>>  please is there a way to have guards with 'where' that communicates with
>> IO? Or is there some other more elegant way? I can do this with classic
>> if/else,...but I just find it nicer with guards.
>>
>>
>>  I have something like this (just an example):
>>
>>
>> f :: Int -> IO String
>> f x
>>     | null dbOutput = return "no db record"
>>     | otherwise = return "we got some db records"
>>     where dbOutput = getDBRecord x
>>
>>
>> getDBRecord :: Int -> IO [Int]
>> getDBRecord recordId = do
>>     putStrLn $ "checking dbRecord" ++ show recordId
>>     --getting data from DB
>>     return [1,2]
>>
>>
>>  problem is that db dbOutput is IO and the guard check does not like it:
>>
>> Couldn't match expected type ?[a0]? with actual type ?IO [Int]?
>>     In the first argument of ?null?, namely ?dbOutput?
>>     In the expression: null dbOutput
>>
>>
>>
>>  Cheers,
>>  Miro
>>
>>
>> _______________________________________________
>> Beginners mailing 
>> [email protected]http://www.haskell.org/mailman/listinfo/beginners
>>
>>
>>
>> _______________________________________________
>> Beginners mailing list
>> [email protected]
>> http://www.haskell.org/mailman/listinfo/beginners
>>
>>
>
> _______________________________________________
> 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/20150115/38220903/attachment.html>

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

Subject: Digest Footer

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


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

End of Beginners Digest, Vol 79, Issue 17
*****************************************

Reply via email to