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. Re:  lens-aeson and error messages (Thomas Koster)
   2.  splicing (derek riemer)
   3.  splicing (derek riemer)
   4. Re:  splicing (David McBride)
   5. Re:  splicing (Shrivats)
   6. Re:  splicing (Michael Orlitzky)
   7. Re:  splicing (Alexey Shmalko)
   8. Re:  splicing (Bob Ippolito)


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

Message: 1
Date: Sun, 14 Jun 2015 19:11:32 +1000
From: Thomas Koster <[email protected]>
To: The Haskell-Beginners Mailing List - Discussion of primarily
        beginner-level topics related to Haskell <[email protected]>
Subject: Re: [Haskell-beginners] lens-aeson and error messages
Message-ID:
        <cag1wh7ai_yffifs5olm3wny+2_ablc69fonweedbsog8cgg...@mail.gmail.com>
Content-Type: text/plain; charset=UTF-8

Hi Alexey,

On Fri, Jun 12, 2015 at 7:11 AM, Thomas Koster <[email protected]> wrote:
> tl;dr - I am using aeson and have an unusual JSON data structure to
> parse, so I have implemented the parseJSON function using the prisms
> and traversals in the lens-aeson package. Unfortunately, the only error
> message I ever get from my parser is "mempty". How can my parser give
> better error messages when using lens-aeson?
>
>   instance FromJSON Row where
>    parseJSON v =
>      Row <$> (v ^. nth 0 . to parseJSON)
>          <*> (v ^. nth 1 . to parseJSON)
>          <*> (v ^. nth 2 . to parseJSON)

On 12 June 2015 at 16:28, Alexey Shmalko <[email protected]> wrote:
> I would come with implementation similar to this:
>
>  instance FromJSON Row where
>     parseJSON = withArray "Row" $ \ a ->
>       case Vector.length a of
>         3 ->
>           Row <$> parseJSON (a ! 0)
>               <*> parseJSON (a ! 1)
>               <*> parseJSON (a ! 2)
>         _ -> fail "Invalid Row."

Thanks for your response.

I was so caught up in trying to do things "the Haskell, functional way",
with pattern matching, that I totally overlooked the plain old (!)
operator!

I have decided that Row is not complex enough to need lens-aeson and
will follow your recommendation.

I might add this (.!) operator to my toolkit though, analogous to (.:), to
remove the need for those "parseJSON"s.

  (.!) :: FromJSON a => Array -> Int -> Parser a

I wonder why this is missing from aeson...

I am still interested if an answer to the original question exists,
should I need lens-aeson at some later time.

Thanks,
--
Thomas Koster


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

Message: 2
Date: Sun, 14 Jun 2015 16:09:16 -0600
From: derek riemer <[email protected]>
To: [email protected]
Subject: [Haskell-beginners] splicing
Message-ID: <[email protected]>
Content-Type: text/plain; charset=utf-8; format=flowed

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


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

Message: 3
Date: Sat, 13 Jun 2015 07:36:28 -0600
From: derek riemer <[email protected]>
To: [email protected]
Subject: [Haskell-beginners] splicing
Message-ID: <[email protected]>
Content-Type: text/plain; charset=utf-8; format=flowed

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


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

Message: 4
Date: Sun, 14 Jun 2015 22:35:04 -0400
From: David McBride <[email protected]>
To: The Haskell-Beginners Mailing List - Discussion of primarily
        beginner-level topics related to Haskell <[email protected]>
Subject: Re: [Haskell-beginners] splicing
Message-ID:
        <can+tr41tft3xmunleeacl6olonqp6qc9_krvgautooj115p...@mail.gmail.com>
Content-Type: text/plain; charset="utf-8"

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 <[email protected]>
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
> [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/20150614/0ea3dcb2/attachment-0001.html>

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

Message: 5
Date: Mon, 15 Jun 2015 08:08:25 +0530
From: Shrivats <[email protected]>
To: The Haskell-Beginners Mailing List - Discussion of primarily
        beginner-level topics related to Haskell <[email protected]>
Subject: Re: [Haskell-beginners] splicing
Message-ID:
        <CAKyHutOHxax86NtEAMb=7eb8sbe7_pgm7rfxa4z1qkmnsxg...@mail.gmail.com>
Content-Type: text/plain; charset="utf-8"

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
[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/20150615/effb2644/attachment-0001.html>

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

Message: 6
Date: Sun, 14 Jun 2015 22:55:11 -0400
From: Michael Orlitzky <[email protected]>
To: [email protected]
Subject: Re: [Haskell-beginners] splicing
Message-ID: <[email protected]>
Content-Type: text/plain; charset=utf-8

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.



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

Message: 7
Date: Mon, 15 Jun 2015 07:12:06 +0300
From: Alexey Shmalko <[email protected]>
To: The Haskell-Beginners Mailing List - Discussion of primarily
        beginner-level topics related to Haskell <[email protected]>
Subject: Re: [Haskell-beginners] splicing
Message-ID:
        <CAFC2PC5UNwN9kr1_hDg5_G0RoegxEJX_Sb5P2ftE2VMv+H=9...@mail.gmail.com>
Content-Type: text/plain; charset=UTF-8

Hi, Derek!

Binary search algorithm requires random access, so you can't implement
it (efficiently) using lists. You'd better try using something as
vector [1]. It has both index and slice operations that work in O(1),
so implementing binary search is a breeze.

[1]: http://hackage.haskell.org/package/vector-0.10.12.3/docs/Data-Vector.html

On Mon, Jun 15, 2015 at 1:09 AM, derek riemer <[email protected]> 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
> [email protected]
> http://mail.haskell.org/cgi-bin/mailman/listinfo/beginners


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

Message: 8
Date: Mon, 15 Jun 2015 06:16:06 +0200
From: Bob Ippolito <[email protected]>
To: The Haskell-Beginners Mailing List - Discussion of primarily
        beginner-level topics related to Haskell <[email protected]>
Subject: Re: [Haskell-beginners] splicing
Message-ID:
        <CACwMPm818PwSu2zBJ49Ch7uv-pxGPD7B=y5yzxsa6l4rdeg...@mail.gmail.com>
Content-Type: text/plain; charset="utf-8"

On Monday, June 15, 2015, derek riemer <[email protected]> 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.html>

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

Subject: Digest Footer

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


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

End of Beginners Digest, Vol 84, Issue 23
*****************************************

Reply via email to