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:  using "read" (Daniel Fischer)
   2. Re:  using "read" (Rahul Kapoor)
   3.  Re: Haskell, Ray Tracing, Parallel Computation (Jon Harrop)
   4.  Re: Code help requested (Joe Van Dyk)
   5. Re:  Re: Code help requested (Daniel Fischer)
   6. Re:  Re: Code help requested (Tim Perry)
   7. Re:  Re: Code help requested (Joe Van Dyk)
   8.  using "read" (Luca Ciciriello)
   9. Re:  Re: Code help requested (Brandon S. Allbery KF8NH)
  10. Re:  Re: Code help requested (Joe Van Dyk)


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

Message: 1
Date: Fri, 15 Jan 2010 20:12:37 +0100
From: Daniel Fischer <[email protected]>
Subject: Re: [Haskell-beginners] using "read"
To: [email protected]
Message-ID: <[email protected]>
Content-Type: text/plain;  charset="iso-8859-1"

Am Freitag 15 Januar 2010 19:49:40 schrieb Luca Ciciriello:
> Hi All.
>
> in the function:
>
> func :: String -> Int
> func str = read str
>
> An absolute beginner as i am, want to use "read" to convert a String in
> a Int, but I don't know how to protect my function func from an
> exception when str is not a number like "12A". How can I do?
>
> Thanks in advance for any answer to this my simply question.
>

You can use the "reads" function,

Prelude> :i reads
reads :: (Read a) => ReadS a    -- Defined in Text.Read
Prelude> :i ReadS
type ReadS a = String -> [(a, String)]
        -- Defined in Text.ParserCombinators.ReadP

, which gives a list of pairs of 
(successful parse of start, remaining input), e.g.

Prelude> reads "12A" :: [(Int,String)]
[(12,"A")]
Prelude> reads "12    " :: [(Int,String)]
[(12,"    ")]

to write your own safe conversion.

Say

readMaybe :: Read a => String -> Maybe a
readMaybe str
    = case reads str of
        [(res,trail)]
            | all isSpace trail -> Just res
        _ -> Nothing

You can work with that or use a default:

readIntWithDefault0 :: String -> Int
readIntWithDefault0 = maybe 0 id . readMaybe

readWithDefault :: Read a => a -> String -> a
readWithDefault d = maybe d id . readMaybe

Whether supplying a default or using Maybe is the better option depends on 
the use-case.


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

Message: 2
Date: Fri, 15 Jan 2010 14:18:43 -0500
From: Rahul Kapoor <[email protected]>
Subject: Re: [Haskell-beginners] using "read"
To: Luca Ciciriello <[email protected]>
Cc: [email protected]
Message-ID:
        <[email protected]>
Content-Type: text/plain; charset=ISO-8859-1

> An absolute beginner as i am, want to use "read" to convert a String in a 
> Int, but I don't know how to protect my ? > >function func from an exception 
> when str is not a number like "12A". How can I do?

Try reads instead. Something like

readMaybe s = case reads s of
              [(i,"")] -> Just i
              _        -> Nothing

Returns "Just" the value parsed in case all input is consumed and
Nothing otherwise.
A similar version can be made if partial parses are acceptable.

Rahul


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

Message: 3
Date: Sat, 16 Jan 2010 00:00:26 +0000
From: Jon Harrop <[email protected]>
Subject: [Haskell-beginners] Re: Haskell, Ray Tracing, Parallel
        Computation
To: Christian Maeder <[email protected]>
Cc: [email protected]
Message-ID: <[email protected]>
Content-Type: text/plain;  charset="utf-8"

On Friday 15 January 2010 18:13:17 Christian Maeder wrote:
> If you still see an older version (of the Cabal library) you may need to
> bootstrap cabal-install-0.8.0 and manually install parsec-2.1.0.1 and
> network (i.e. version 2.2.1.5) before (as described above for AC-Vector).

Ok, I finally managed to get everything working this way. Turns out the cabal 
update wasn't even attempting to use the latest version. Once I'd 
bootstrapped the latest cabal-install I had to tell it to install for a 
specific version of GHC using the -w flag. Now I've got GHC 6.12.1 up and 
running!

Thanks!

-- 
Dr Jon Harrop, Flying Frog Consultancy Ltd.
http://www.ffconsultancy.com/?e


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

Message: 4
Date: Fri, 15 Jan 2010 16:17:55 -0800
From: Joe Van Dyk <[email protected]>
Subject: [Haskell-beginners] Re: Code help requested
To: beginners <[email protected]>
Message-ID:
        <[email protected]>
Content-Type: text/plain; charset=UTF-8

Thanks all for your help.

Here's another one.  Seems like I could use a fold here, but am unsure
how that would work.  Also, if I pass in a search value that's too
big, then the function blows up.

(source at 
http://github.com/joevandyk/haskell/raw/master/pearls/binary_search/binary_search.hs
--  Feel free to fork it.)

-- A translation of
http://en.wikipedia.org/wiki/Binary_search_algorithm#Recursive
binary_find :: Ord a => [a] -> a -> Maybe Int
binary_find [] elem   = Nothing

binary_find list elem =
  do_search list elem 0 (length list)
  where
    do_search list elem low high =
      if high < low then Nothing
      else
        if list !! mid > elem
        then do_search list elem low (mid - 1)
        else
          if list !! mid < elem
          then do_search list elem (mid + 1) high
          else Just mid
      where
        mid = low + (high - low) `div` 2

main = do
  print $ binary_find [1] 1
  print $ binary_find [1,3] 1
  print $ binary_find [1,3,4] 3
  print $ binary_find [1,3,4] 4
  print $ binary_find [1,2,4,6,8,9,12,15,17,20] 17
  print $ binary_find "hello" 'l'
  print $ binary_find [0.0, 1.5, 3.0] 3.0

  print $ binary_find [] 1
  print $ binary_find [1,3] 2
  print $ binary_find [1,4,6,8,9,12,15,17,20] 2

  -- boom?
  print $ binary_find [1,4,6,8,9,12,15,17,20] 100


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

Message: 5
Date: Sat, 16 Jan 2010 02:00:10 +0100
From: Daniel Fischer <[email protected]>
Subject: Re: [Haskell-beginners] Re: Code help requested
To: [email protected]
Message-ID: <[email protected]>
Content-Type: text/plain;  charset="utf-8"

Am Samstag 16 Januar 2010 01:17:55 schrieb Joe Van Dyk:
> Thanks all for your help.
>
> Here's another one.  Seems like I could use a fold here, but am unsure
> how that would work.  Also, if I pass in a search value that's too
> big, then the function blows up.
>
> (source at
> http://github.com/joevandyk/haskell/raw/master/pearls/binary_search/bina
>ry_search.hs --  Feel free to fork it.)
>
> -- A translation of
> http://en.wikipedia.org/wiki/Binary_search_algorithm#Recursive
> binary_find :: Ord a => [a] -> a -> Maybe Int
> binary_find [] elem   = Nothing
>
> binary_find list elem =
>   do_search list elem 0 (length list)

That should be (length list - 1).

binary_find [1] 2
~> do_search [1] 2 0 1
~> mid = 0 + 1 `div` 2 = 0
~> [1] !! mid < 2
~> do_search [1] 2 (0+1) 1
~> mid = 1 + 0 `div` 2 = 1
~> [1] !! 1 => boom

>   where
>     do_search list elem low high =
>       if high < low then Nothing
>       else
>         if list !! mid > elem
>         then do_search list elem low (mid - 1)
>         else
>           if list !! mid < elem
>           then do_search list elem (mid + 1) high
>           else Just mid
>       where
>         mid = low + (high - low) `div` 2

I'd prefer
      mid = (low + high) `div` 2
here.

>
> main = do
>   print $ binary_find [1] 1
>   print $ binary_find [1,3] 1
>   print $ binary_find [1,3,4] 3
>   print $ binary_find [1,3,4] 4
>   print $ binary_find [1,2,4,6,8,9,12,15,17,20] 17
>   print $ binary_find "hello" 'l'
>   print $ binary_find [0.0, 1.5, 3.0] 3.0
>
>   print $ binary_find [] 1
>   print $ binary_find [1,3] 2
>   print $ binary_find [1,4,6,8,9,12,15,17,20] 2
>
>   -- boom?
>   print $ binary_find [1,4,6,8,9,12,15,17,20] 100


However:
Lists are _not_ arrays.

list !! n

is O(n), except n >= length list, then getting the error is O(length list).
And getting the length is O(length list), too.

So the binary search on list is O(l*log l), where l = length list, while 
straightforward linear search is O(l).

You can make the binary search O(l) if you have

binaryFind list e = search list e (length list)
   where
      search _ _ 0 = Nothing
      search lst e len
         | x == e      = Just e
         | x < e        = search front e half
         | otherwise = search back e (len - half - 1)
           where
             half = (len - 1) `div` 2
             (front, x:back) = splitAt half lst

but in general, that is still much worse than straightforward search.

The binary search algorithm is for data structures with constant time 
access (arrays, anything else?), not singly linked lists.


foldSearch list e = foldr f Nothing list
   where
      f x y
         | x == e    = Just x
         | otherwise = y 



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

Message: 6
Date: Fri, 15 Jan 2010 17:17:24 -0800 (PST)
From: Tim Perry <[email protected]>
Subject: Re: [Haskell-beginners] Re: Code help requested
To: beginners <[email protected]>
Message-ID: <[email protected]>
Content-Type: text/plain; charset=us-ascii

Hi Joe,

I think you wanted (length list - 1) where you call do_search.  The version 
below is my rewrite with guards, this change, and "midVal" which keeps "list !! 
mid" from being evaluated twice per recursion.  Unfortunately, I have no idea 
how to work a fold into this.  Good luck!

--Tim


binary_find :: Ord a => [a] -> a -> Maybe Int
binary_find [] elem  = Nothing

binary_find list elem =
  do_search list elem 0 (length list -1)
  where
    do_search list elem low high
      | high < low = Nothing
      | midVal > elem = do_search list elem low (mid - 1)
      | midVal < elem = do_search list elem (mid + 1) high
      | otherwise = Just mid
      where
        midVal = list !! mid
        mid = low + (high - low) `div` 2

main = do
  print $ binary_find [1] 2
  print $ binary_find [1,3] 1
  print $ binary_find [1,3,4] 3
  print $ binary_find [1,3,4] 4
  print $ binary_find [1,2,4,6,8,9,12,15,17,20] 17
  print $ binary_find "hello" 'l'
  print $ binary_find [0.0, 1.5, 3.0] 3.0

  print $ binary_find [] 1
  print $ binary_find [1,3] 2
  print $ binary_find [1,4,6,8,9,12,15,17,20] 2

  -- boom?
  print $ binary_find [1,4,6,8,9,12,15,17,20] 19


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

Message: 7
Date: Fri, 15 Jan 2010 17:48:32 -0800
From: Joe Van Dyk <[email protected]>
Subject: Re: [Haskell-beginners] Re: Code help requested
Cc: beginners <[email protected]>
Message-ID:
        <[email protected]>
Content-Type: text/plain; charset=UTF-8

On Fri, Jan 15, 2010 at 5:17 PM, Tim Perry <[email protected]> wrote:
> Hi Joe,
>
> I think you wanted (length list - 1) where you call do_search.  The version 
> below is my rewrite with guards, this change, and "midVal" which keeps "list 
> !! mid" from being evaluated twice per recursion.  Unfortunately, I have no 
> idea how to work a fold into this.  Good luck!

Ah, I was trying to figure out how to use guards here, but I couldn't
figure out the syntax.  Thanks for the help!

What's the best way to unit test this function?  Quick-check?  What
would that look like?

Joe


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

Message: 8
Date: Sat, 16 Jan 2010 09:43:09 +0100
From: Luca Ciciriello <[email protected]>
Subject: [Haskell-beginners] using "read"
To: [email protected]
Message-ID: <[email protected]>
Content-Type: text/plain; charset=us-ascii

Thanks to All.
reads solve my problem.

Luca.


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

Message: 9
Date: Sat, 16 Jan 2010 08:30:04 -0500
From: "Brandon S. Allbery KF8NH" <[email protected]>
Subject: Re: [Haskell-beginners] Re: Code help requested
To: Tim Perry <[email protected]>
Cc: beginners <[email protected]>
Message-ID: <[email protected]>
Content-Type: text/plain; charset="us-ascii"

On Jan 15, 2010, at 20:17 , Tim Perry wrote:
> binary_find list elem =
>  do_search list elem 0 (length list -1)
>  where
>    do_search list elem low high
>      | high < low = Nothing
>      | midVal > elem = do_search list elem low (mid - 1)
>      | midVal < elem = do_search list elem (mid + 1) high
>      | otherwise = Just mid
>      where
>        midVal = list !! mid
>        mid = low + (high - low) `div` 2


Observation:  the first two parameters to do_search never change  
within an invocation of binary_find, and the corresponding arguments  
to binary_find are in scope; depending on the (lack of) cleverness of  
the compiler, you could see a speedup by not passing them around  
unnecessarily.  In addition, it's *conceptually* cleaner because  
passing them around explicitly suggests to someone reading the source  
that they *do* change when that isn't actually the case.

-- 
brandon s. allbery [solaris,freebsd,perl,pugs,haskell] [email protected]
system administrator [openafs,heimdal,too many hats] [email protected]
electrical and computer engineering, carnegie mellon university    KF8NH


-------------- next part --------------
A non-text attachment was scrubbed...
Name: PGP.sig
Type: application/pgp-signature
Size: 195 bytes
Desc: This is a digitally signed message part
Url : 
http://www.haskell.org/pipermail/beginners/attachments/20100116/acd280c2/PGP-0001.bin

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

Message: 10
Date: Sat, 16 Jan 2010 12:09:53 -0800
From: Joe Van Dyk <[email protected]>
Subject: Re: [Haskell-beginners] Re: Code help requested
To: "Brandon S. Allbery KF8NH" <[email protected]>
Cc: beginners <[email protected]>
Message-ID:
        <[email protected]>
Content-Type: text/plain; charset=UTF-8

On Sat, Jan 16, 2010 at 5:30 AM, Brandon S. Allbery KF8NH
<[email protected]> wrote:
> On Jan 15, 2010, at 20:17 , Tim Perry wrote:
>>
>> binary_find list elem =
>>  do_search list elem 0 (length list -1)
>>  where
>>   do_search list elem low high
>>     | high < low = Nothing
>>     | midVal > elem = do_search list elem low (mid - 1)
>>     | midVal < elem = do_search list elem (mid + 1) high
>>     | otherwise = Just mid
>>     where
>>       midVal = list !! mid
>>       mid = low + (high - low) `div` 2
>
>
> Observation:  the first two parameters to do_search never change within an
> invocation of binary_find, and the corresponding arguments to binary_find
> are in scope; depending on the (lack of) cleverness of the compiler, you
> could see a speedup by not passing them around unnecessarily.  In addition,
> it's *conceptually* cleaner because passing them around explicitly suggests
> to someone reading the source that they *do* change when that isn't actually
> the case.

Good one, thanks.  The reason I had them in there originally is
because I was thinking it would be simpler if a function was passed in
all the inputs it was working with -- instead of referring to
variables defined outside its scope.

Joe


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

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


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

Reply via email to