Skip to main content
We’ve updated our Terms of Service. A new AI Addendum clarifies how Stack Overflow utilizes AI interactions.
Code Golf

Return to Answer

added 734 characters in body
Source Link
ბიმო
  • 17k
  • 3
  • 43
  • 106

Haskell, (削除) 40 38 (削除ここまで) 3837 bytes

-2 bytes by using bind instead of do-notation.

a=0:zipWithscanl(flip($)([1)0$[1..]>>=(:[pred]).(+))a

Generates the sequence indefinetelyReturns an infinite list, try it online! try it online!

Explanation

Thescanl takes three arguments f, init and xs ([x0, x1 ... ]) and builds a new list:

[a0 = init, a1 = f(a0,x0), a2 = f(a1, x1) ... ]

We set init = 0 and use the flipped ($) application operator (thus it applies ai to the function xi), now we only need a list of functions - the list [1..]>>=(:[pred]).(+) is an infinite list ofwith the right functions:

[(+1),(-1),(+2),(-1),(+3),(-1),(+4),...

Interesting alternative, 37 bytes

We now only need to recursively apply those functions toflip having the sequencetype (a -> b -> c) -> b -> a -> c we could also use (whereid :: d -> d instead of ($) is the function-application operator),because of Haskell's type inference the base casetype (0d) is simply prepended: would be unified with a -> b, giving us the same.

0 : zipWith ($) [(+1),(-1),..] a

Try it online!

Edit

-2 bytes by using (>>=) instead of do-notation.

-1 byte by using scanl instead of zipWith.

Haskell, (削除) 40 (削除ここまで) 38 bytes

-2 bytes by using bind instead of do-notation.

a=0:zipWith($)([1..]>>=(:[pred]).(+))a

Generates the sequence indefinetely, try it online!

Explanation

The list [1..]>>=(:[pred]).(+) is an infinite list of functions:

[(+1),(-1),(+2),(-1),(+3),(-1),(+4),...

We now only need to recursively apply those functions to the sequence a (where ($) is the function-application operator), the base case (0) is simply prepended:

0 : zipWith ($) [(+1),(-1),..] a

Haskell, (削除) 40 38 (削除ここまで) 37 bytes

scanl(flip($))0$[1..]>>=(:[pred]).(+)

Returns an infinite list, try it online!

Explanation

scanl takes three arguments f, init and xs ([x0, x1 ... ]) and builds a new list:

[a0 = init, a1 = f(a0,x0), a2 = f(a1, x1) ... ]

We set init = 0 and use the flipped ($) application operator (thus it applies ai to the function xi), now we only need a list of functions - the list [1..]>>=(:[pred]).(+) is an infinite list with the right functions:

[(+1),(-1),(+2),(-1),(+3),(-1),(+4),...

Interesting alternative, 37 bytes

flip having the type (a -> b -> c) -> b -> a -> c we could also use id :: d -> d instead of ($) because of Haskell's type inference the type d would be unified with a -> b, giving us the same.

Try it online!

Edit

-2 bytes by using (>>=) instead of do-notation.

-1 byte by using scanl instead of zipWith.

added 70 characters in body
Source Link
ბიმო
  • 17k
  • 3
  • 43
  • 106

Haskell, 40(削除) 40 (削除ここまで) 38 bytes

-2 bytes by using bind instead of do-notation.

a=0:zipWith($)(do x<-[1..];[]>>=(x+:[pred]),pred].(+))a

Generates the sequence indefinetely, try it online! try it online!

Explanation

The list do x<-[1..];[]>>=(x+:[pred]),pred].(+) is an infinite list of functions:

[(+1),(-1),(+2),(-1),(+3),(-1),(+4),...

We now only need to recursively apply those functions to the sequence a (where ($) is the function-application operator), the base case (0) is simply prepended:

0 : zipWith ($) [(+1),(-1),..] a

Haskell, 40 bytes

a=0:zipWith($)(do x<-[1..];[(x+),pred])a

Generates the sequence indefinetely, try it online!

Explanation

The list do x<-[1..];[(x+),pred] is an infinite list of functions:

[(+1),(-1),(+2),(-1),(+3),(-1),(+4),...

We now only need to recursively apply those functions to the sequence a (where ($) is the function-application operator), the base case (0) is simply prepended:

0 : zipWith ($) [(+1),(-1),..] a

Haskell, (削除) 40 (削除ここまで) 38 bytes

-2 bytes by using bind instead of do-notation.

a=0:zipWith($)([1..]>>=(:[pred]).(+))a

Generates the sequence indefinetely, try it online!

Explanation

The list [1..]>>=(:[pred]).(+) is an infinite list of functions:

[(+1),(-1),(+2),(-1),(+3),(-1),(+4),...

We now only need to recursively apply those functions to the sequence a (where ($) is the function-application operator), the base case (0) is simply prepended:

0 : zipWith ($) [(+1),(-1),..] a
added 387 characters in body
Source Link
ბიმო
  • 17k
  • 3
  • 43
  • 106

Haskell, 40 bytes

a=0:zipWith($)(do x<-[1..];[(x+),pred])a

Generates the sequence indefinetely, Trytry it online!

Explanation

The list do x<-[1..];[(x+),pred] is an infinite list of functions:

[(+1),(-1),(+2),(-1),(+3),(-1),(+4),...

We now only need to recursively apply those functions to the sequence a (where ($) is the function-application operator), the base case (0) is simply prepended:

0 : zipWith ($) [(+1),(-1),..] a

Haskell, 40 bytes

a=0:zipWith($)(do x<-[1..];[(x+),pred])a

Try it online!

Haskell, 40 bytes

a=0:zipWith($)(do x<-[1..];[(x+),pred])a

Generates the sequence indefinetely, try it online!

Explanation

The list do x<-[1..];[(x+),pred] is an infinite list of functions:

[(+1),(-1),(+2),(-1),(+3),(-1),(+4),...

We now only need to recursively apply those functions to the sequence a (where ($) is the function-application operator), the base case (0) is simply prepended:

0 : zipWith ($) [(+1),(-1),..] a
Source Link
ბიმო
  • 17k
  • 3
  • 43
  • 106
Loading

AltStyle によって変換されたページ (->オリジナル) /