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.  hunit question (Joe Van Dyk)
   2.  define action getInt like getLine ([email protected])
   3. Re:  hunit question (Daniel Fischer)
   4.  Re: hunit question (Joe Van Dyk)
   5. Re:  define action getInt like getLine (Rahul Kapoor)
   6. Re:  define action getInt like getLine (Daniel Fischer)
   7. Re:  hunit question (Joe Van Dyk)
   8. Re:  define action getInt like getLine (legajid)
   9. Re:  hunit question (Daniel Fischer)


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

Message: 1
Date: Tue, 2 Feb 2010 13:18:50 -0800
From: Joe Van Dyk <[email protected]>
Subject: [Haskell-beginners] hunit question
To: beginners <[email protected]>
Message-ID:
        <[email protected]>
Content-Type: text/plain; charset=UTF-8

I have a little binary search function and am trying to write tests
for it in hunit.

The below approach doesn't compile, because I'm attempting to build a
list of tuples of different types, which isn't working for me.

What's the appropriate way to do this test?

http://gist.github.com/293031

import Test.HUnit
import JoeBinarySearch
-- Note:
-- JoeBinarySearch.binary_search :: (Ord a) => [a] -> a -> Maybe Int

assertions = [
  ([1],       1, (Just 0)),
  ([1, 3],    1, (Just 0)),
  ([1, 3, 4], 4, (Just 2)),
  ([1,2,4,6,8,9,12,15,17,20], 17, (Just 8)),
  ([1,2,4,6,8,9,12,15,17,20], 20, (Just 9)),
  ("hello",                 'l', (Just 2)), -- BOOM
  ([0.0, 1.5, 3.0],         3.0, (Just 2)),
  ([],                      1, Nothing),
  ([1,3],                   2, Nothing),
  ([1,4,6,8,9,12,15,17,20], 2, Nothing),
  ([1,4,6,8,9,12,15,17,20], 100, Nothing),
  ([1,4,6,8,9,12,15,17,20], (-100), Nothing)]

test_list = TestList test_cases
  where
    test_cases = map test_func assertions
    test_func (lst, input, expected) = TestCase $ assert_equal' (
binary_search lst  input ) expected
    assert_equal' = assertEqual "should equal"

main :: IO ()
main = do
  runTestTT test_list
  print "DONE"


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


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

Message: 2
Date: Tue, 02 Feb 2010 22:20:03 +0100
From: [email protected]
Subject: [Haskell-beginners] define action getInt like getLine
To: [email protected]
Message-ID: <[email protected]>
Content-Type: text/plain; charset="us-ascii"

Hi,
how can I write an action 
getInt :: IO Int
that works like 
getLine :: IO String
but for Int instead of String. I know that I can read the input with getLine 
and then convert it by using read, but don't know how to write it as an action. 
I tried
getInt :: IO Int
getInt read <- getLine
but that doesn't work.
-- 
Jetzt kostenlos herunterladen: Internet Explorer 8 und Mozilla Firefox 3.5 -
sicherer, schneller und einfacher! http://portal.gmx.net/de/go/atbrowser


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

Message: 3
Date: Tue, 2 Feb 2010 22:34:54 +0100
From: Daniel Fischer <[email protected]>
Subject: Re: [Haskell-beginners] hunit question
To: [email protected]
Message-ID: <[email protected]>
Content-Type: text/plain;  charset="utf-8"

Am Dienstag 02 Februar 2010 22:18:50 schrieb Joe Van Dyk:
> I have a little binary search function and am trying to write tests
> for it in hunit.
>
> The below approach doesn't compile, because I'm attempting to build a
> list of tuples of different types, which isn't working for me.
>
> What's the appropriate way to do this test?

QuickCheck, I'd say. Let that create more testcases than you could bother 
to write out.

Anyway, for testing lists of different types, you'd need separate tests 
(properties/assertions).

*But* you don't need that. Because the algorithm is general (works the same 
way for all types in Ord), the only way it could be correct on one type but 
not on another is if the Ord instance of one (or both) of the types is 
incorrect.

So it's sufficient to test on [Int].

>
> http://gist.github.com/293031
>
> import Test.HUnit
> import JoeBinarySearch
> -- Note:
> -- JoeBinarySearch.binary_search :: (Ord a) => [a] -> a -> Maybe Int
>
> assertions = [
>   ([1],       1, (Just 0)),
>   ([1, 3],    1, (Just 0)),
>   ([1, 3, 4], 4, (Just 2)),
>   ([1,2,4,6,8,9,12,15,17,20], 17, (Just 8)),
>   ([1,2,4,6,8,9,12,15,17,20], 20, (Just 9)),
>   ("hello",                 'l', (Just 2)), -- BOOM
>   ([0.0, 1.5, 3.0],         3.0, (Just 2)),
>   ([],                      1, Nothing),
>   ([1,3],                   2, Nothing),
>   ([1,4,6,8,9,12,15,17,20], 2, Nothing),
>   ([1,4,6,8,9,12,15,17,20], 100, Nothing),
>   ([1,4,6,8,9,12,15,17,20], (-100), Nothing)]
>
> test_list = TestList test_cases
>   where
>     test_cases = map test_func assertions
>     test_func (lst, input, expected) = TestCase $ assert_equal' (
> binary_search lst  input ) expected
>     assert_equal' = assertEqual "should equal"
>
> main :: IO ()
> main = do
>   runTestTT test_list
>   print "DONE"



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

Message: 4
Date: Tue, 2 Feb 2010 13:36:44 -0800
From: Joe Van Dyk <[email protected]>
Subject: [Haskell-beginners] Re: hunit question
To: beginners <[email protected]>
Message-ID:
        <[email protected]>
Content-Type: text/plain; charset=UTF-8

On Tue, Feb 2, 2010 at 1:18 PM, Joe Van Dyk <[email protected]> wrote:
> I have a little binary search function and am trying to write tests
> for it in hunit.
>
> The below approach doesn't compile, because I'm attempting to build a
> list of tuples of different types, which isn't working for me.
>
> What's the appropriate way to do this test?
>
> http://gist.github.com/293031
>
> import Test.HUnit
> import JoeBinarySearch
> -- Note:
> -- JoeBinarySearch.binary_search :: (Ord a) => [a] -> a -> Maybe Int
>
> assertions = [
>  ([1],       1, (Just 0)),
>  ([1, 3],    1, (Just 0)),
>  ([1, 3, 4], 4, (Just 2)),
>  ([1,2,4,6,8,9,12,15,17,20], 17, (Just 8)),
>  ([1,2,4,6,8,9,12,15,17,20], 20, (Just 9)),
>  ("hello",                 'l', (Just 2)), -- BOOM
>  ([0.0, 1.5, 3.0],         3.0, (Just 2)),
>  ([],                      1, Nothing),
>  ([1,3],                   2, Nothing),
>  ([1,4,6,8,9,12,15,17,20], 2, Nothing),
>  ([1,4,6,8,9,12,15,17,20], 100, Nothing),
>  ([1,4,6,8,9,12,15,17,20], (-100), Nothing)]
>
> test_list = TestList test_cases
>  where
>    test_cases = map test_func assertions
>    test_func (lst, input, expected) = TestCase $ assert_equal' (
> binary_search lst  input ) expected
>    assert_equal' = assertEqual "should equal"
>
> main :: IO ()
> main = do
>  runTestTT test_list
>  print "DONE"


I suppose I can live with this:

build_test_case list element expected = test_case
  where
    test_case = TestCase assertion
    assertion = assertEqual "should equal" (binary_search list element) expected

test_list = TestList [
  build_test_case [1] 1       (Just 0),
  build_test_case [1, 3]    1 (Just 0),
  build_test_case [1, 3, 4] 4 (Just 2),
  build_test_case [1,2,4,6,8,9,12,15,17,20] 17 (Just 8),
  build_test_case [1,2,4,6,8,9,12,15,17,20] 20 (Just 9),
  build_test_case "hello"                   'l' (Just 2),
  build_test_case [0.0, 1.5, 3.0]           3.0 (Just 2),
  build_test_case []                        1 Nothing,
  build_test_case [1,3]                     2 Nothing,
  build_test_case [1,4,6,8,9,12,15,17,20]   2 Nothing,
  build_test_case [1,4,6,8,9,12,15,17,20]   100 Nothing,
  build_test_case [1,4,6,8,9,12,15,17,20]   (-100) Nothing
  ]


main :: IO ()
main = do
  runTestTT test_list
  print "DONE"



But I assume there's a better way.


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

Message: 5
Date: Tue, 2 Feb 2010 16:39:34 -0500
From: Rahul Kapoor <[email protected]>
Subject: Re: [Haskell-beginners] define action getInt like getLine
To: [email protected]
Cc: [email protected]
Message-ID:
        <[email protected]>
Content-Type: text/plain; charset=ISO-8859-1

> Hi,
> how can I write an action
> getInt :: IO Int
> that works like
> getLine :: IO String

The most literal way to write getInt would be:

getInt :: IO Int
getInt = do
  s <- getLine
  return (read s)

which is just a more verbose version of:

getInt' =   read `fmap` getLine

The above versions don't do any error checking, so you might
prefer a getInt :: IO Maybe Int which returns Nothing in case the input
is not an integer.

You should read up on Monadic IO in Haskell. The Real World Haskell book
is probably a good starting point.

Rahul


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

Message: 6
Date: Tue, 2 Feb 2010 22:39:07 +0100
From: Daniel Fischer <[email protected]>
Subject: Re: [Haskell-beginners] define action getInt like getLine
To: [email protected]
Message-ID: <[email protected]>
Content-Type: text/plain;  charset="iso-8859-1"

Am Dienstag 02 Februar 2010 22:20:03 schrieb [email protected]:
> Hi,
> how can I write an action
> getInt :: IO Int
> that works like
> getLine :: IO String
> but for Int instead of String. I know that I can read the input with
> getLine and then convert it by using read, but don't know how to write
> it as an action. I tried getInt :: IO Int
> getInt read <- getLine
> but that doesn't work.

There are many possibilities.

The shortest is

getInt :: IO Int
getInt = readLn

another short and sweet is

getInt :: IO Int
getInt = fmap read getLine  -- or liftM read getLine

But neither of these deals well with malformed input, if that's a 
possibility to reckon with, use e.g. the reads function

getInt :: IO Int
getInt = do
    inp <- getLine
    case reads inp of
      ((a,tl):_) | all isSpace tl -> return a
      _ -> handle malformed input


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

Message: 7
Date: Tue, 2 Feb 2010 13:46:20 -0800
From: Joe Van Dyk <[email protected]>
Subject: Re: [Haskell-beginners] hunit question
To: Daniel Fischer <[email protected]>
Cc: [email protected]
Message-ID:
        <[email protected]>
Content-Type: text/plain; charset=UTF-8

On Tue, Feb 2, 2010 at 1:34 PM, Daniel Fischer <[email protected]> wrote:
> Am Dienstag 02 Februar 2010 22:18:50 schrieb Joe Van Dyk:
>> I have a little binary search function and am trying to write tests
>> for it in hunit.
>>
>> The below approach doesn't compile, because I'm attempting to build a
>> list of tuples of different types, which isn't working for me.
>>
>> What's the appropriate way to do this test?
>
> QuickCheck, I'd say. Let that create more testcases than you could bother
> to write out.
>
> Anyway, for testing lists of different types, you'd need separate tests
> (properties/assertions).
>
> *But* you don't need that. Because the algorithm is general (works the same
> way for all types in Ord), the only way it could be correct on one type but
> not on another is if the Ord instance of one (or both) of the types is
> incorrect.
>
> So it's sufficient to test on [Int].

Good point, but I like to prove that to myself.  :D

Any chance anyone can show me how to write a QuickCheck test for this?

Joe


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

Message: 8
Date: Tue, 02 Feb 2010 23:06:41 +0100
From: legajid <[email protected]>
Subject: Re: [Haskell-beginners] define action getInt like getLine
To: Rahul Kapoor <[email protected]>
Cc: [email protected]
Message-ID: <[email protected]>
Content-Type: text/plain; charset=ISO-8859-1; format=flowed

Hi,

here's  another idea :

saisie_choix :: IO Int
saisie_choix=do
    putStrLn "Type in 1 digit :"
    xchoix <- getLine
    if (length xchoix) /= 1 || head xchoix < '0' || head xchoix > '9'
      then saisie_choix
      else do   
        let nchoix=read xchoix::Int
        return nchoix

or, with maybe :

{-      --------------     MAYBE     ------------------ -}

-- Fonction principale
mainmaybe=do
    x <- lire
        if isNothing x
          then do
            putStrLn "Donnee invalide"   
          else do
            putStr " \b" 

    putStrLn ("Resultat : " ++ show (f x))

-- Définition de la fonction de calcul
f Nothing = -9999
f (Just x) = 2 * x

-- Fonction d'IO qui valide la saisie
lire :: IO (Maybe Integer)
lire = do
    -- saisie
    xn <- getLine
    let x=read xn ::Integer
   
    -- validation
    if x < 5
      then do
        return (Just x)
      else do
        return Nothing

Didier.



Rahul Kapoor a écrit :
>> Hi,
>> how can I write an action
>> getInt :: IO Int
>> that works like
>> getLine :: IO String
>>     
>
> The most literal way to write getInt would be:
>
> getInt :: IO Int
> getInt = do
>   s <- getLine
>   return (read s)
>
> which is just a more verbose version of:
>
> getInt' =   read `fmap` getLine
>
> The above versions don't do any error checking, so you might
> prefer a getInt :: IO Maybe Int which returns Nothing in case the input
> is not an integer.
>
> You should read up on Monadic IO in Haskell. The Real World Haskell book
> is probably a good starting point.
>
> Rahul
> _______________________________________________
> Beginners mailing list
> [email protected]
> http://www.haskell.org/mailman/listinfo/beginners
>
>   


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

Message: 9
Date: Tue, 2 Feb 2010 23:03:48 +0100
From: Daniel Fischer <[email protected]>
Subject: Re: [Haskell-beginners] hunit question
To: Joe Van Dyk <[email protected]>
Cc: [email protected]
Message-ID: <[email protected]>
Content-Type: text/plain;  charset="utf-8"

Am Dienstag 02 Februar 2010 22:46:20 schrieb Joe Van Dyk:
> On Tue, Feb 2, 2010 at 1:34 PM, Daniel Fischer <[email protected]> 
wrote:
> > Am Dienstag 02 Februar 2010 22:18:50 schrieb Joe Van Dyk:
> >> I have a little binary search function and am trying to write tests
> >> for it in hunit.
> >>
> >> The below approach doesn't compile, because I'm attempting to build a
> >> list of tuples of different types, which isn't working for me.
> >>
> >> What's the appropriate way to do this test?
> >
> > QuickCheck, I'd say. Let that create more testcases than you could
> > bother to write out.
> >
> > Anyway, for testing lists of different types, you'd need separate
> > tests (properties/assertions).
> >
> > *But* you don't need that. Because the algorithm is general (works the
> > same way for all types in Ord), the only way it could be correct on
> > one type but not on another is if the Ord instance of one (or both) of
> > the types is incorrect.
> >
> > So it's sufficient to test on [Int].
>
> Good point, but I like to prove that to myself.  :D
>
> Any chance anyone can show me how to write a QuickCheck test for this?
>
> Joe

prop_binary_search :: Int -> [Int] -> Bool
prop_binary_search x xs =
    let ys = sort xs 
    in binary_search x ys == elemIndex x ys


ghci> quickCheck prop_binary_search
+++ OK, passed 100 tests.


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

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


End of Beginners Digest, Vol 20, Issue 6
****************************************

Reply via email to