Skip to main content
Explain the evil trick
Source Link
Ørjan Johansen
  • 7.5k
  • 1
  • 22
  • 38

Haskell, 38 bytes

I feel like PPCG is really improving my Haskell. Strokes white cat.

f n=mapM(["<","~<~"]<$f)[1..n]!!n>>=id

f takes an Int and returns a String.

Try it online!

(I'm referring to that <$f by the way. It saves a character over \_->.)

In the Functor instance for (->) a (functions from type a), we have: x <$ f = fmap (const x) f = const x . f = const x. The only limitation is that f and the final const x must use the same source type a. The instance is completely lazy so this never even evaluates f.

Alternatively, same length but less evil ((l!!) is an anonymous function):

(l!!)
l=(++)<$>"":tail l<*>["<","~<~"]

Try it online!

Both of these use the same representation as @ETHproductions' Japt answer, although especially the first one may give some redundant <s at the beginning.

The first one calculates all combinations of n "<" and "~<~" strings, then indexes into the resulting list.

The second one recursively calculates an infinite list formed by starting with "" and then constructing new elements by appending "<" and "~<~" strings to each element already in the list (actually it was slightly shorter to also let the "" get turned into "<".)

Haskell, 38 bytes

I feel like PPCG is really improving my Haskell. Strokes white cat.

f n=mapM(["<","~<~"]<$f)[1..n]!!n>>=id

f takes an Int and returns a String.

Try it online!

(I'm referring to that <$f by the way. It saves a character over \_->.)

Alternatively, same length but less evil ((l!!) is an anonymous function):

(l!!)
l=(++)<$>"":tail l<*>["<","~<~"]

Try it online!

Both of these use the same representation as @ETHproductions' Japt answer, although especially the first one may give some redundant <s at the beginning.

The first one calculates all combinations of n "<" and "~<~" strings, then indexes into the resulting list.

The second one recursively calculates an infinite list formed by starting with "" and then constructing new elements by appending "<" and "~<~" strings to each element already in the list (actually it was slightly shorter to also let the "" get turned into "<".)

Haskell, 38 bytes

I feel like PPCG is really improving my Haskell. Strokes white cat.

f n=mapM(["<","~<~"]<$f)[1..n]!!n>>=id

f takes an Int and returns a String.

Try it online!

(I'm referring to that <$f by the way. It saves a character over \_->.)

In the Functor instance for (->) a (functions from type a), we have: x <$ f = fmap (const x) f = const x . f = const x. The only limitation is that f and the final const x must use the same source type a. The instance is completely lazy so this never even evaluates f.

Alternatively, same length but less evil ((l!!) is an anonymous function):

(l!!)
l=(++)<$>"":tail l<*>["<","~<~"]

Try it online!

Both of these use the same representation as @ETHproductions' Japt answer, although especially the first one may give some redundant <s at the beginning.

The first one calculates all combinations of n "<" and "~<~" strings, then indexes into the resulting list.

The second one recursively calculates an infinite list formed by starting with "" and then constructing new elements by appending "<" and "~<~" strings to each element already in the list (actually it was slightly shorter to also let the "" get turned into "<".)

Source Link
Ørjan Johansen
  • 7.5k
  • 1
  • 22
  • 38

Haskell, 38 bytes

I feel like PPCG is really improving my Haskell. Strokes white cat.

f n=mapM(["<","~<~"]<$f)[1..n]!!n>>=id

f takes an Int and returns a String.

Try it online!

(I'm referring to that <$f by the way. It saves a character over \_->.)

Alternatively, same length but less evil ((l!!) is an anonymous function):

(l!!)
l=(++)<$>"":tail l<*>["<","~<~"]

Try it online!

Both of these use the same representation as @ETHproductions' Japt answer, although especially the first one may give some redundant <s at the beginning.

The first one calculates all combinations of n "<" and "~<~" strings, then indexes into the resulting list.

The second one recursively calculates an infinite list formed by starting with "" and then constructing new elements by appending "<" and "~<~" strings to each element already in the list (actually it was slightly shorter to also let the "" get turned into "<".)