Send Beginners mailing list submissions to
        [email protected]

To subscribe or unsubscribe via the World Wide Web, visit
        http://mail.haskell.org/cgi-bin/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.  Haskell equivalent to Clojure's partition fn (Timothy Washington)
   2. Re:  Haskell equivalent to Clojure's partition fn (Karl Voelker)
   3. Re:  Haskell equivalent to Clojure's partition fn (Dan Serban)
   4. Re:  Haskell equivalent to Clojure's partition fn
      (Timothy Washington)


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

Message: 1
Date: Sat, 25 Jul 2015 10:30:26 -0700
From: Timothy Washington <[email protected]>
To: The Haskell-Beginners Mailing List - Discussion of primarily
        beginner-level topics related to Haskell <[email protected]>
Subject: [Haskell-beginners] Haskell equivalent to Clojure's partition
        fn
Message-ID:
        <CAADtM-Z5cq9OiLP=Ofd=SARNHS5mHyx6JHSX=RF1Jd=3epb...@mail.gmail.com>
Content-Type: text/plain; charset="utf-8"

Is there a Haskell equivalent to Clojure's partition
<http://clojuredocs.org/clojure.core/partition> function? I'm particularly
interested in the stepwise feature, where we can select a certain `n` size
of the list, then step `m` and take again.

What's returned is a list of lists. Each list being a sublist incremented
by `m`. I didn't see anything in the below packages. And I wanted to check
before I go about writing my own.

   - Prelude
   <https://hackage.haskell.org/package/base-4.8.0.0/docs/Prelude.html#g:13>
   - Data.List
   <https://hackage.haskell.org/package/base-4.8.1.0/docs/Data-List.html>
   - Data.Array
   <http://hackage.haskell.org/package/array-0.5.0.0/docs/Data-Array.html>
   - Data.Vector
   <http://hackage.haskell.org/package/vector-0.11.0.0/docs/Data-Vector.html>


Thanks
Tim
-------------- next part --------------
An HTML attachment was scrubbed...
URL: 
<http://mail.haskell.org/pipermail/beginners/attachments/20150725/b4232c35/attachment-0001.html>

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

Message: 2
Date: Sat, 25 Jul 2015 10:42:48 -0700
From: Karl Voelker <[email protected]>
To: [email protected]
Subject: Re: [Haskell-beginners] Haskell equivalent to Clojure's
        partition fn
Message-ID:
        <[email protected]>
Content-Type: text/plain; charset="us-ascii"

On Sat, Jul 25, 2015, at 10:30 AM, Timothy Washington wrote:
> Is there a Haskell equivalent to Clojure's partition[1] function? I'm
> particularly interested in the stepwise feature, where we can select a
> certain `n` size of the list, then step `m` and take again.

This is a great use case for a type-based search engine. Luckily, we
have some of those!

Here is a search that matches your question, I think:

http://hayoo.fh-wedel.de/?query=Int+-%3E+Int+-%3E+%5Ba%5D+-%3E+%5B%5Ba%5D%5D

At this moment, there is one result, which appears to be exactly the
thing you want. Unfortunately, it's an auxiliary function of a music
theory package, which you may not want to install for that one function.

There's a package called "split" that contains a bunch of useful tools
for splitting lists. It doesn't  have exactly what you want, but it does
have a function called "chop" that will get you most of the way there:

http://hackage.haskell.org/package/split-0.2.2/docs/Data-List-Split.html#v:chop

-Karl


Links:

  1. http://clojuredocs.org/clojure.core/partition
-------------- next part --------------
An HTML attachment was scrubbed...
URL: 
<http://mail.haskell.org/pipermail/beginners/attachments/20150725/95db832e/attachment-0001.html>

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

Message: 3
Date: Sat, 25 Jul 2015 20:56:17 +0300
From: Dan Serban <[email protected]>
To: The Haskell-Beginners Mailing List - Discussion of primarily
        beginner-level topics related to Haskell <[email protected]>
Subject: Re: [Haskell-beginners] Haskell equivalent to Clojure's
        partition fn
Message-ID:
        <cahapvsemprjhb0rhkuanznuhk3lycdco0jhxummfy1dfvio...@mail.gmail.com>
Content-Type: text/plain; charset=UTF-8

It looks like chunksOf will take you most of the way there. Here's my
quick and dirty GHCi session output:

?> import Data.List.Split
?>
?> let clojurePartition n m = map (take n) $ chunksOf (n+m) [0..]
?>
?> take 3 $ clojurePartition 4 6
[[0,1,2,3],[10,11,12,13],[20,21,22,23]]


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

Message: 4
Date: Sat, 25 Jul 2015 12:28:59 -0700
From: Timothy Washington <[email protected]>
To: The Haskell-Beginners Mailing List - Discussion of primarily
        beginner-level topics related to Haskell <[email protected]>
Subject: Re: [Haskell-beginners] Haskell equivalent to Clojure's
        partition fn
Message-ID:
        <CAADtM-b1q=eujugep1rvu_ysfad_aoe-thwqj1etu8w3b2y...@mail.gmail.com>
Content-Type: text/plain; charset="utf-8"

While I can say A), what I really need is B)

*A)* > *take 5 $ chunksOf 10 [0..]*
[[0,1,2,3,4,5,6,7,8,9],[10,11,12,13,14,15,16,17,18,19],[20,21,22,23,24,25,26,27,28,29],[30,31,32,33,34,35,36,37,38,39],[40,41,42,43,44,45,46,47,48,49]]

*B)* > take 5 $ someFn 10 1 [0..]
[[0,1,2,3,4,5,6,7,8,9],[1,2,3,4,5,6,7,8,9,10],[2,3,4,5,6,7,8,9,10,11],[3,4,5,6,7,8,9,10,11,12],[4,5,6,7,8,9,10,11,12,13]]


The music theory package indeed has a working partition function (source
here <http://rd.slavepianos.org/sw/hmt/Music/Theory/List.hs>). The
implementation simply *i)* takes `n` from the source list, *ii)* drops by
`m` then recurses.

segments :: Int -> Int -> [a] -> [[a]]
segments n m p =
    let q = take n p
        p' = drop m p
    in if length q /= n then [] else q : segments n m p'



But that's rather manual. So I played around with this using *chop*, and
came up with the *divvy* function. It does exactly what I need.

divvy :: Int -> Int -> [a] -> [[a]]
divvy n m lst =
  chop (\xs -> (take n xs , drop m xs)) lst


> *take 5 $ partitionClojure 10 1 [0..]*
[[0,1,2,3,4,5,6,7,8,9],[1,2,3,4,5,6,7,8,9,10],[2,3,4,5,6,7,8,9,10,11],[3,4,5,6,7,8,9,10,11,12],[4,5,6,7,8,9,10,11,12,13]]


Thanks guys. This helped a lot :)


Tim


On Sat, Jul 25, 2015 at 10:56 AM, Dan Serban <[email protected]> wrote:

> It looks like chunksOf will take you most of the way there. Here's my
> quick and dirty GHCi session output:
>
> ?> import Data.List.Split
> ?>
> ?> let clojurePartition n m = map (take n) $ chunksOf (n+m) [0..]
> ?>
> ?> take 3 $ clojurePartition 4 6
> [[0,1,2,3],[10,11,12,13],[20,21,22,23]]
> _______________________________________________
> Beginners mailing list
> [email protected]
> http://mail.haskell.org/cgi-bin/mailman/listinfo/beginners
>
-------------- next part --------------
An HTML attachment was scrubbed...
URL: 
<http://mail.haskell.org/pipermail/beginners/attachments/20150725/81116063/attachment-0001.html>

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

Subject: Digest Footer

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


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

End of Beginners Digest, Vol 85, Issue 18
*****************************************

Reply via email to