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:  function not working as expected,    type issues (David McBride)
   2. Re:  function not working as expected,    type issues
      (Nadir Sampaoli)
   3. Re:  function not working as expected,    type issues (Gesh)


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

Message: 1
Date: Wed, 9 Apr 2014 02:17:12 -0400
From: David McBride <[email protected]>
To: The Haskell-Beginners Mailing List - Discussion of primarily
        beginner-level topics related to Haskell <[email protected]>
Subject: Re: [Haskell-beginners] function not working as expected,
        type issues
Message-ID:
        <CAN+Tr43-WNbvxgjW+pvShPwrrimQ0QvUkV58B_ue0sK0=6a...@mail.gmail.com>
Content-Type: text/plain; charset="iso-8859-1"

I didn't look closely enough at what you wrote, you had several issues.
Let me try again:

displayD :: String -> Int -> IO ()
displayD string 0 = return ()
displayD string count = do
  hPutStr stdout string
  displayD string (count - 1)

This is one way to do it.  You use the arguments themselves.  Obviously
this won't work with negative counts.

displayD :: String -> Int -> IO ()
displayD string count
  | count <= 0 = return ()
  | otherwise = do
      hPutStr stdout string
      displayD string (count - 1)

And as for figuring out types.  You should try to load your source file in
ghci, and then use the :t and :i commands liberally to inspect functions
you are using.  You will begin to get the hang of it in no time.


On Wed, Apr 9, 2014 at 2:08 AM, David McBride <[email protected]> wrote:

> Try using guards:
>
> displayD :: IO () -> String -> Int
> displayD string count =
>         | count > 0 = hPutStr stdout string
>         | otherwise = displayD string (count - 1)
>
> Alternatively use if, then, else.
>
>
>
> On Wed, Apr 9, 2014 at 2:03 AM, Alexej Magura <[email protected]> wrote:
>
>> K, so I have a function that I'm writing that takes a *string* and a
>> *count* and prints the *string* to STDOUT *count* times:
>>
>> displayD :: IO () -> String -> Int
>> displayD string count = do
>>         (count > 0) && hPutStr stdout string
>>         (count > 0) && displayD string (count - 1)
>>
>> However, when I try to compile the file, I get several errors; here's a
>> pastebin: http://pastebin.com/DEsuAvfz
>>
>> What I'm trying to do here is check to see if *count* is greater than 0,
>> and then if it is, then I'd like to print *string* to STDOUT until *count*
>> equals 0.  I tried doing it via pattern matching, like so:
>>
>> displayD string (count > 0) = do
>>
>> But I haven't seen any examples of how to do pattern matching in
>> functions using *do*, that is *IO*, so I'm unsure if the above is even
>> anywhere near correct.
>>
>> Still very uncomfortable with declaring function types: I have a hard
>> time figuring out which type is returned and what type is expected as an
>> arg.  My initial guess is that the *first* type specified is the one
>> returned, but I'm not sure.
>>
>> --
>> Alexej Magura
>> Sent with Airmail
>> _______________________________________________
>> 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/20140409/ddc72096/attachment-0001.html>

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

Message: 2
Date: Wed, 9 Apr 2014 08:20:58 +0200
From: Nadir Sampaoli <[email protected]>
To: The Haskell-Beginners Mailing List - Discussion of primarily
        beginner-level topics related to Haskell <[email protected]>
Subject: Re: [Haskell-beginners] function not working as expected,
        type issues
Message-ID:
        <CAFYwTdR-o=7sx6_uvbwhqqa3w3n6mw3dosrruu_xqlwjkdr...@mail.gmail.com>
Content-Type: text/plain; charset="utf-8"

Hello Alexej
Il 09/apr/2014 08:03 "Alexej Magura" <[email protected]> ha scritto:
>
> K, so I have a function that I?m writing that takes a *string* and a
*count* and prints the *string* to STDOUT *count* times:
>
> displayD :: IO () -> String -> Int
> displayD string count = do
>         (count > 0) && hPutStr stdout string
>         (count > 0) && displayD string (count - 1)
>

At glance at least one of your errors comes from your type signature being
incorrect. Your function should takes first a String then an Int and should
return an IO () because you're printing stuff. So:

    displayD :: IO () -> String -> Int

should become:

    displayD :: String -> Int -> IO ()

Regards,
Nadir
-------------- next part --------------
An HTML attachment was scrubbed...
URL: 
<http://www.haskell.org/pipermail/beginners/attachments/20140409/094f8b86/attachment-0001.html>

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

Message: 3
Date: Wed, 09 Apr 2014 11:45:09 +0300
From: Gesh <[email protected]>
To: The Haskell-Beginners Mailing List - Discussion of primarily
        beginner-level topics related to Haskell <[email protected]>
Subject: Re: [Haskell-beginners] function not working as expected,
        type issues
Message-ID: <[email protected]>
Content-Type: text/plain; charset=UTF-8

On April 9, 2014 9:03:54 AM GMT+03:00, Alexej Magura <[email protected]> 
wrote:
>K, so I have a function that I?m writing that takes a *string* and a
>*count* and prints the *string* to STDOUT *count* times:
>
>displayD :: IO () -> String -> Int
>displayD string count = do
>       (count > 0) && hPutStr stdout string
>       (count > 0) && displayD string (count - 1)
>
>However, when I try to compile the file, I get several errors; here?s a
>pastebin:?http://pastebin.com/DEsuAvfz
>
The errors you see here are caused by GHC trying to make sense of what you 
wrote and failing. This is because the code you wrote isn't well-typed - that 
is, you're using values of types differing from the types GHC is expecting you 
to use. More on this below.
>What I?m trying to do here is check to see if *count* is greater than
>0, and then if it is, then I?d like to print *string* to STDOUT until
>*count* equals 0. ?I tried doing it via pattern matching, like so:
>
Thank you for explaining the problem you're trying to solve instead of Gesh
problem you're facing. Most people don't have the decency to do that, leading 
to hours of pointless discussion. [0]
>displayD string (count > 0) = do
>
>But I haven?t seen any examples of how to do pattern matching in
>functions using *do*, that is *IO*, so I?m unsure if the above is even
>anywhere near correct.
>
do-blocks are no different from any other expression in Haskell when it comes 
to pattern-matching. I'd suggest starting out with a pure (no IO) version of 
your code, loading it into GHCi and playing around with it, then adding the IO 
once you feel comfortable with writing pure code. In general, it's considered 
good practice to separate impure code from pure code. In this case, that would 
mean creating a function returning the n-time self a concatenation of a string, 
then printing the result of that function elsewhere.
>Still very uncomfortable with declaring function types: I have a hard
>time figuring out which type is returned and what type is expected as
>an arg. ?My initial guess is that the *first* type specified is the one
>returned, but I?m not sure.
>
I'm curious as to why you thought that. The directionality of the arrows in a 
type signature is quite clear, at least to me. a -> b is the type of functions 
*from* a *to* b.  This is important to understand, especially once you start 
treading into higher-order-function-land, using functions such as map :: (a -> 
b) -> [a] -> [b] (can you guess what it does and how it does it? Can you do 
that just by looking at the type?).
One further note. The errors you saw in your output were caused by this 
misunderstanding of function types, as well as a misunderstanding of the types 
do-blocks expect. From your coding style, it appears that you think of && in 
the way Bash does - as the function:
> p && x = if p then x else *nothing*
For some suitable *nothing*. However, this is not the case. Firstly, && is just 
a function that takes two values of type Book and returns True if both of them 
are True. Second, a function such as the one I described above exists for 
Monads, by the name of guard. But until you grasp pure Haskell, there is no 
point in confusing yourself with them yet.
Anyway, all of the above means that passing a non-Bool value to && is not 
acceptable, and since hPutStr stdout string is not a value of type Bool, GHC 
complains.
So, in brief, follow the arrows, values are not automatically cast as in other 
languages, && is not the guard operator from Bash, and get away from IO for 
your first couple of attempts with Haskell.
Hoping to help,
Gesh
[0] - http://xyproblem.info/
Alexej,
You have several things going on here.


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

Subject: Digest Footer

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


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

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

Reply via email to