Rewrite without parenthesis

38 views
Skip to first unread message

Carl Petersen

unread,
Aug 18, 2016, 8:03:22 AM8/18/16
to Haskell-cafe
How do you rewrite head (tail [1,2,3]) without parenthesis.

Thanks!

mukesh tiwari

unread,
Aug 18, 2016, 8:11:41 AM8/18/16
to Carl Petersen, Haskell-cafe

head . tail $ [1, 2, 3]


On 18 Aug 2016 22:03, "Carl Petersen" <cepe...@gmail.com> wrote:
How do you rewrite head (tail [1,2,3]) without parenthesis.

Thanks!

_______________________________________________
Haskell-Cafe mailing list
To (un)subscribe, modify options or view archives go to:
http://mail.haskell.org/cgi-bin/mailman/listinfo/haskell-cafe
Only members subscribed via the mailman list are allowed to post.

gansteed

unread,
Aug 18, 2016, 8:15:00 AM8/18/16
to haskel...@googlegroups.com

--
You received this message because you are subscribed to the Google Groups "Haskell-cafe" group.
To unsubscribe from this group and stop receiving emails from it, send an email to haskell-cafe+unsubscribe@googlegroups.com.
To post to this group, send email to haskel...@googlegroups.com.
Visit this group at https://groups.google.com/group/haskell-cafe.
For more options, visit https://groups.google.com/d/optout.

Francesco Ariis

unread,
Aug 18, 2016, 8:19:56 AM8/18/16
to haskel...@haskell.org
On Thu, Aug 18, 2016 at 05:03:21AM -0700, Carl Petersen wrote:
> How do you rewrite head (tail [1,2,3]) without parenthesis.

Hello Carl, simply

λ> tail [1,2,3]
[2,3]

will do! Haskell is not a Lisp, no need to wrap expressions
around ().

Francesco Ariis

unread,
Aug 18, 2016, 8:29:18 AM8/18/16
to haskel...@haskell.org
On Thu, Aug 18, 2016 at 02:14:26PM +0200, Francesco Ariis wrote:
> Hello Carl, simply
>
> λ> tail [1,2,3]
> [2,3]
>
> will do! Haskell is not a Lisp, no need to wrap expressions
> around ().

And you want to do away with pesky square brackets:

λ> tail $ enumFromTo 1 3
[2,3]

Theodore Lief Gannon

unread,
Aug 18, 2016, 8:29:41 AM8/18/16
to haskel...@haskell.org

Francesco, you missed a piece. 'head' is part of the expression. :) What Carl is looking for is one or the other of these:

head $ tail [1,2,3]
-- or --
head . tail $ [1,2,3]

The first one is shorter and fine for inlining, but I generally prefer the second form... it helps intuition about eta reduction, refactoring, and additional composition.

Daniel Berecz

unread,
Aug 18, 2016, 8:32:18 AM8/18/16
to haskel...@haskell.org
Hi,

Do you want to apply tail and then head on the list?
If so, you can write:

head $ tail [1, 2, 3]

Besides the dollar opeartor there is a dot (.) operator. You can read more here:

Regards,
Daniel

Francesco Ariis

unread,
Aug 18, 2016, 8:33:36 AM8/18/16
to haskel...@haskell.org
On Thu, Aug 18, 2016 at 05:29:34AM -0700, Theodore Lief Gannon wrote:
> Francesco, you missed a piece. 'head' is part of the expression. :) What
> Carl is looking for is one or the other of these:
>
> head $ tail [1,2,3]
> -- or --
> head . tail $ [1,2,3]
>
> The first one is shorter and fine for inlining, but I generally prefer the
> second form... it helps intuition about eta reduction, refactoring, and
> additional composition.

Welp, silly me! :P

James Brown

unread,
Aug 18, 2016, 10:27:51 AM8/18/16
to Carl Petersen, Haskell-cafe
head1 = head . tail
head1 [1, 2, 3]

or 

head.tail $ [1, 2, 3]

On Thu, Aug 18, 2016 at 8:03 AM, Carl Petersen <cepe...@gmail.com> wrote:
How do you rewrite head (tail [1,2,3]) without parenthesis.

Thanks!

Carl Petersen

unread,
Aug 18, 2016, 10:53:31 AM8/18/16
to Haskell-cafe
Thanks everyone for the quick responses. Greatly appreciated.

Elliot Cameron

unread,
Aug 18, 2016, 11:20:49 AM8/18/16
to haskel...@googlegroups.com
Don't neglect this option which many prefer "head $ tail [1, 2, 3]"

Ivan Lazar Miljenovic

unread,
Aug 18, 2016, 6:26:42 PM8/18/16
to Theodore Lief Gannon, Haskell Cafe
On 18 August 2016 at 22:29, Theodore Lief Gannon <tan...@gmail.com> wrote:
> Francesco, you missed a piece. 'head' is part of the expression. :) What
> Carl is looking for is one or the other of these:
>
> head $ tail [1,2,3]
> -- or --
> head . tail $ [1,2,3]

Or for this specific use case, [1,2,3] !! 1 ==> (!!1) [1,2,3]

>
> The first one is shorter and fine for inlining, but I generally prefer the
> second form... it helps intuition about eta reduction, refactoring, and
> additional composition.
>
>
> On Aug 18, 2016 5:19 AM, "Francesco Ariis" <fa...@ariis.it> wrote:
>>
>> On Thu, Aug 18, 2016 at 05:03:21AM -0700, Carl Petersen wrote:
>> > How do you rewrite head (tail [1,2,3]) without parenthesis.
>>
>> Hello Carl, simply
>>
>> λ> tail [1,2,3]
>> [2,3]
>>
>> will do! Haskell is not a Lisp, no need to wrap expressions
>> around ().
>> _______________________________________________
>> Haskell-Cafe mailing list
>> To (un)subscribe, modify options or view archives go to:
>> http://mail.haskell.org/cgi-bin/mailman/listinfo/haskell-cafe
>> Only members subscribed via the mailman list are allowed to post.
>
>
> _______________________________________________
> Haskell-Cafe mailing list
> To (un)subscribe, modify options or view archives go to:
> http://mail.haskell.org/cgi-bin/mailman/listinfo/haskell-cafe
> Only members subscribed via the mailman list are allowed to post.

--
Ivan Lazar Miljenovic
Ivan.Mi...@gmail.com
http://IvanMiljenovic.wordpress.com

Richard A. O'Keefe

unread,
Aug 18, 2016, 8:47:03 PM8/18/16
to haskel...@haskell.org

On 19/08/16 12:03 AM, Carl Petersen wrote:
> How do you rewrite head (tail [1,2,3]) without parenthesis.

s/parenthesis/parentheses/

There are several ways to do this, including [1,2,3] !! 1.
The more important question is "why"?

Alexander Berntsen

unread,
Aug 19, 2016, 9:15:49 AM8/19/16
to haskel...@haskell.org
-----BEGIN PGP SIGNED MESSAGE-----
Hash: SHA512

On 19/08/16 02:46, Richard A. O'Keefe wrote:
>>> How do you rewrite head (tail [1,2,3]) without parenthesis.
> s/parenthesis/parentheses/

head . tail $ [1, 2, 3] --)

- --
Alexander
alex...@plaimi.net
https://secure.plaimi.net/~alexander
-----BEGIN PGP SIGNATURE-----
Version: GnuPG v2

iQIcBAEBCgAGBQJXtwZ7AAoJENQqWdRUGk8Bb00P/jhJoUQTh3dCCLqbJHGJm1+x
Cvh+DOXMWoQyQcEvOh9u6DruqpnLRGgzcqDVzUOmqM2rwwkt7kznF7vzof5mCcXx
UPb9W0X7OI6oMFmvg2Xp9gDxTcQinTfaLDyyKRZzRV5xj+On00xtso8SjriVh2HO
ubzQLVs0kxt3irX+7orvz3JmHDiiVnfMVR6bC+TfJE7gnrIjtJH5jxkZP+Os10tC
fHOEa0jhRY8E8BR9nbzOhcw2/3HZeQLSek3MIFxvAtgyYT67JprJwKwLbslGHcKV
YS7kHL8BEOb+rJbIi3RHyVNwSnYi0hc0WkwrQo4CI7yr2HloAwewMuMPwMHjmNRS
CAmWry4lVO+NFAaRnnVCLkXqxUjOKeIs3D5Fin0DTxgfetp245RuHuYiMTiGV/yf
0eSX2QjPf4sNFutOHbUdgwmyLwZJRUFxgdOt2cYQ/keRPSdARjG8vf83rriBqCkB
QNbQgC3M1w8zQ4sSeHvQUtYHAPBjyj8JHpQ1yFfy8g3DNyr7950IXalGPHNwvqZJ
HGILbKJfMb+bB0n0/9rzsFz8gdXKt/9JvysYEg6VP4TWUaNIhSNoKLGXQsNYLFMB
QFYN+byKb4JQVnPT8ojRS7btBcs3m9oAfiu2d+4rK6z9x31TjPMNiQUcNp8Vlmvb
Tb9wd51A4uJARO/eja3O
=UMCW
-----END PGP SIGNATURE-----

Michael Orlitzky

unread,
Aug 21, 2016, 11:48:52 PM8/21/16
to haskel...@haskell.org
On 08/18/2016 08:03 AM, Carl Petersen wrote:
> How do you rewrite head (tail [1,2,3]) without parenthesis.
>

case [1,2,3] of x1:x2:xs -> Just x2; otherwise -> Nothing

Reply all
Reply to author
Forward
0 new messages