Is this overkill?

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

Is this overkill?

Zachary Turner-2
I thought I would try to see if it were possible to write, in point-free
form using no lambda functions and no intermediate functions, a function
that takes 2 lists of Booleans, computes the pairwise logical AND of the two
lists, and returns a list containing the 0 based indices of the elements
where the logical and of the two was true.  I know that at some point it
becomes overkill and for the sake of readability one should know when to
draw the line.  So I want to see if someone with more experience than me can
comment on whether or not this is over the line :P

trueIndices = curry $ map fst . filter snd . zip [0..] . map (uncurry (&&))
. (uncurry zip)

So do all the uncurries and curries make it too hard to understand or is it
pretty easy to read this?  For me it takes me a while to figure out by
looking at it because it's hard to trace all the currying and uncurrying.
And is there a more elegant solution?
-------------- next part --------------
An HTML attachment was scrubbed...
URL: http://www.haskell.org/pipermail/beginners/attachments/20090403/4a7bc302/attachment.htm
Reply | Threaded
Open this post in threaded view
|

Is this overkill?

Felipe Lessa
On Fri, Apr 03, 2009 at 08:58:41PM -0500, Zachary Turner wrote:
> trueIndices = curry $ map fst . filter snd . zip [0..] . map (uncurry (&&))
> . (uncurry zip)

Just some tips: note that

  map (uncurry f) . zip  ===  zipWith f
  curry $ f . uncurry g  ===  (f .) . g

so

  trueIndices = ((map fst . filter snd . zip [0..]) .) . zipWith (&&)

There must be some combinators to avoid the '((...) .) .' thing, though. I just don't remember :).

HTH,

--
Felipe.
Reply | Threaded
Open this post in threaded view
|

Re: Is this overkill?

Heinrich Apfelmus
In reply to this post by Zachary Turner-2
Zachary Turner wrote:

> I thought I would try to see if it were possible to write, in point-free
> form using no lambda functions and no intermediate functions, a function
> that takes 2 lists of Booleans, computes the pairwise logical AND of the two
> lists, and returns a list containing the 0 based indices of the elements
> where the logical and of the two was true.  I know that at some point it
> becomes overkill and for the sake of readability one should know when to
> draw the line.  So I want to see if someone with more experience than me can
> comment on whether or not this is over the line :P
>
> trueIndices = curry $ map fst . filter snd . zip [0..] . map (uncurry (&&))
> .. (uncurry zip)
>
> So do all the uncurries and curries make it too hard to understand or is it
> pretty easy to read this?  For me it takes me a while to figure out by
> looking at it because it's hard to trace all the currying and uncurrying.
> And is there a more elegant solution?

Looks very readable to me, though I'd write it as

  trueIndices = (map fst . filter snd . zip [0..] .) . zipWith (&&)

or even simply as

  trueIndices xs ys =
      map fst . filter snd . zip [0..] $ zipWith (&&) xs ys

because composing functions with more than one argument tends to be a
bit messy.


With Conal's semantic editor combinators

  http://conal.net/blog/posts/semantic-editor-combinators/

it would be written as

  trueIndices =
    (result . result) (map fst . filter snd . zip [0..]) (zipWith (&&))


Regards,
apfelmus

--
http://apfelmus.nfshost.com

Reply | Threaded
Open this post in threaded view
|

Re: Is this overkill?

Zachary Turner-2
On Fri, Apr 3, 2009 at 9:30 PM, Heinrich Apfelmus <[hidden email]
> wrote:

> Zachary Turner wrote:
> > I thought I would try to see if it were possible to write, in point-free
> > form using no lambda functions and no intermediate functions, a function
> > that takes 2 lists of Booleans, computes the pairwise logical AND of the
> two
> > lists, and returns a list containing the 0 based indices of the elements
> > where the logical and of the two was true.  I know that at some point it
> > becomes overkill and for the sake of readability one should know when to
> > draw the line.  So I want to see if someone with more experience than me
> can
> > comment on whether or not this is over the line :P
> >
> > trueIndices = curry $ map fst . filter snd . zip [0..] . map (uncurry
> (&&))
> > .. (uncurry zip)
> >
> > So do all the uncurries and curries make it too hard to understand or is
> it
> > pretty easy to read this?  For me it takes me a while to figure out by
> > looking at it because it's hard to trace all the currying and uncurrying.
> > And is there a more elegant solution?
>
> Looks very readable to me, though I'd write it as
>
>  trueIndices = (map fst . filter snd . zip [0..] .) . zipWith (&&)
>
> or even simply as
>
>  trueIndices xs ys =
>      map fst . filter snd . zip [0..] $ zipWith (&&) xs ys
>
> because composing functions with more than one argument tends to be a
> bit messy.
>
>
> With Conal's semantic editor combinators
>
>  http://conal.net/blog/posts/semantic-editor-combinators/
>
> it would be written as
>
>  trueIndices =
>    (result . result) (map fst . filter snd . zip [0..]) (zipWith (&&))
>


That was a pretty interesting blog post, and easily understandable which is
always nice.  Thanks for the link.  I also had never even used the zipWith
function, so thanks for pointing out that equivalence.
-------------- next part --------------
An HTML attachment was scrubbed...
URL: http://www.haskell.org/pipermail/beginners/attachments/20090403/691113d2/attachment.htm
Reply | Threaded
Open this post in threaded view
|

Re: Is this overkill?

Chaddaï Fouché
On Sat, Apr 4, 2009 at 5:20 AM, Zachary Turner <[hidden email]> wrote:

>> With Conal's semantic editor combinators
>>
>> ?http://conal.net/blog/posts/semantic-editor-combinators/
>>
>> it would be written as
>>
>> ?trueIndices =
>> ? ?(result . result) (map fst . filter snd . zip [0..]) (zipWith (&&))
>
>
> That was a pretty interesting blog post, and easily understandable which is
> always nice.? Thanks for the link.? I also had never even used the zipWith
> function, so thanks for pointing out that equivalence.

I'm not sure you really want to write this pointfree... Generally I
tend to avoid it when there's two arguments of identical standing.
Also the (map fst . filter snd...) is a Data.List function
(findIndices) :

trueIndices xs ys = findindices id $ zipWith (&&) xs ys

I love the pointfree style and it might be a good exercise to try and
transcribe any function to pointfree, but sometimes pointful is just
clearer.

--
Jeda?
Reply | Threaded
Open this post in threaded view
|

Re: Is this overkill?

Magnus Therning
Chadda? Fouch? wrote:

> On Sat, Apr 4, 2009 at 5:20 AM, Zachary Turner <[hidden email]> wrote:
>>> With Conal's semantic editor combinators
>>>
>>>  http://conal.net/blog/posts/semantic-editor-combinators/
>>>
>>> it would be written as
>>>
>>>  trueIndices =
>>>    (result . result) (map fst . filter snd . zip [0..]) (zipWith (&&))
>>
>> That was a pretty interesting blog post, and easily understandable which is
>> always nice.  Thanks for the link.  I also had never even used the zipWith
>> function, so thanks for pointing out that equivalence.
>
> I'm not sure you really want to write this pointfree... Generally I
> tend to avoid it when there's two arguments of identical standing.
> Also the (map fst . filter snd...) is a Data.List function
> (findIndices) :
>
> trueIndices xs ys = findindices id $ zipWith (&&) xs ys
>
> I love the pointfree style and it might be a good exercise to try and
> transcribe any function to pointfree, but sometimes pointful is just
> clearer.

Or to put it differently, sometimes "pointfree is pointless" ;-)

/M

--
Magnus Therning                        (OpenPGP: 0xAB4DFBA4)
magnus?therning?org          Jabber: magnus?therning?org
http://therning.org/magnus         identi.ca|twitter: magthe

Haskell is an even 'redder' pill than Lisp or Scheme.
     -- PaulPotts

-------------- next part --------------
A non-text attachment was scrubbed...
Name: signature.asc
Type: application/pgp-signature
Size: 197 bytes
Desc: OpenPGP digital signature
Url : http://www.haskell.org/pipermail/beginners/attachments/20090405/cdb91aa1/signature.bin