[Haskell-beginners] splicing

Bob Ippolito bob at redivi.com
Mon Jun 15 04:16:06 UTC 2015


On Monday, June 15, 2015, derek riemer <driemer.riemer at gmail.com> wrote:

> Hi guys,
> As a newby to haskell, I was curious, what is the best way to splice an
> array or do things in the middle?
> For example, binary search requires i do in sudocode
> define binary search array target.
> Find middle element.
> If target is middle element then return target
> else if target < middle element then
>     binary search array[0:target]
> else
>     binary search array[target:end]
>
> How can I get this splicing with haskell?
> I can't just use head here.
> I can't do array!!n: where n is some number.


You probably shouldn't use lists for binary search, since indexing a list
is linear time. Binary searching a list is slower than a linear
search. However, if you must, you can use splitAt for that purpose.

Where you should really be looking for Array-like uses are Data.Vector or
Data.Array. The former is probably better suited for this use case.

You should also consider adding arguments to the search function for start
and end indexes, rather than slicing the array itself. That's the more
traditional way to implement it.

-bob
-------------- next part --------------
An HTML attachment was scrubbed...
URL: <http://mail.haskell.org/pipermail/beginners/attachments/20150615/7af6ebfe/attachment-0001.html>


More information about the Beginners mailing list