Zulip Chat Archive

Stream: general

Topic: Golf explicit list


Martin Dvořák (Nov 17 2022 at 20:25):

What is the shortest way to write [2, 2, 8, 8, 8, 8, 8, 8, 8, 8, 1, 6, 6, 6, 6, 6, 6] in Lean?

Yaël Dillies (Nov 17 2022 at 20:30):

import data.list.defs

def sqrepeat (l : list ) : list  := l >>= λ n, list.repeat n n

#eval sqrepeat [2, 8, 1, 6] -- [2, 2, 8, 8, 8, 8, 8, 8, 8, 8, 1, 6, 6, 6, 6, 6, 6]

Martin Dvořák (Nov 17 2022 at 20:32):

Cool but yours is longer than mine.

Yaël Dillies (Nov 17 2022 at 20:36):

In terms of characters? I guess you can do

import data.list.defs
#eval[2,8,1,6]>>=λn,list.repeat n n

and that's 59 chars for the entire file.

Eric Wieser (Nov 17 2022 at 20:55):

It's one character longer but #eval do l←[2,8,1,6],list.repeat l l is also valid syntax for that, in case you didn't know @Martin Dvořák .

Eric Wieser (Nov 17 2022 at 20:57):

#eval[2,2,8,8,8,8,8,8,8,8,1,6,6,6,6,6,6] is 40 characters...

Yaël Dillies (Nov 17 2022 at 20:58):

I feel like it should rather be #eval do n←[2,8,1,6],list.repeat n n

Eric Wieser (Nov 17 2022 at 20:59):

Thankfully both n and l are one character :upside_down:

Alexandre Rademaker (Nov 17 2022 at 23:06):

What the >>= does?

Yaël Dillies (Nov 17 2022 at 23:07):

It is docs#has_bind.bind, which here unfolds to docs#list.bind.

Andrew Yang (Nov 17 2022 at 23:08):

It is the bind operator for the list monad. See wikipedia for more info.


Last updated: Dec 20 2023 at 11:08 UTC