splicing

Previous Topic Next Topic
 
classic Classic list List threaded Threaded
4 messages Options
Reply | Threaded
Open this post in threaded view
|

splicing

derek riemer
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.

Thanks,
Derek
_______________________________________________
Beginners mailing list
[hidden email]
http://mail.haskell.org/cgi-bin/mailman/listinfo/beginners
Reply | Threaded
Open this post in threaded view
|

Re: splicing

David McBride
You can use the various splitAts in Data.List, Data.Vector to split a list at an index.  You can also use drop and take to get a splice ie. drop 4 (take 2) is a splice from 3:5.

On Sat, Jun 13, 2015 at 9:36 AM, derek riemer <[hidden email]> 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.

Thanks,
Derek
_______________________________________________
Beginners mailing list
[hidden email]
http://mail.haskell.org/cgi-bin/mailman/listinfo/beginners


_______________________________________________
Beginners mailing list
[hidden email]
http://mail.haskell.org/cgi-bin/mailman/listinfo/beginners
Reply | Threaded
Open this post in threaded view
|

Re: splicing

Shrivats
In reply to this post by derek riemer

Hi,

I don't see anything as a Prelude function, but you can do something with a combination of take and drop:

rangeOf:: Int -> Int -> [a]
rangeOf x y = take y . drop x

rangeOf 2 4 [1 .. 5] -- [3,4,5]

I have not included any bounds check. It's up to you. For added benefits, look up dropWhile and takeWhile.

HTH.

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.

Thanks,
Derek
_______________________________________________
Beginners mailing list
[hidden email]
http://mail.haskell.org/cgi-bin/mailman/listinfo/beginners

_______________________________________________
Beginners mailing list
[hidden email]
http://mail.haskell.org/cgi-bin/mailman/listinfo/beginners
Reply | Threaded
Open this post in threaded view
|

Re: splicing

Michael Orlitzky
In reply to this post by derek riemer
On 06/13/2015 09:36 AM, derek riemer 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?

If you REALLY want to slice and aren't worried about index-safety, you
should try using vectors instead of plain lists:

  https://hackage.haskell.org/package/vector/docs/Data-Vector.html

On the right side of that page there's an index; you want the section
called "Extracting subvectors (slicing)." You will need to install the
"vector" package to get that stuff.

_______________________________________________
Beginners mailing list
[hidden email]
http://mail.haskell.org/cgi-bin/mailman/listinfo/beginners