DEV Community

Discussion on: Golfing language extensions

Collapse
 
leonschoorl profile image
Leon Schoorl • Edited

We can eliminate one space by moving the # to another position that doesn't need it, before a

a∷_=id @(_∷_)mdo if|let?_=(0x0.0,'(),)→[0b_0^0e0| !x#←[]|_←[],then\case]

And add two #'s to the TuppleSection so we use UnboxedTuples

b∷_=id @(_∷_)mdo if|let?_=(#0x0.0,'(),#)→[0b_0^0e0| !x#←[]|_←[],then\case]

Since our toplevel binder is now just one character we can get its name
instead of (), saving 1 character

c∷_=id @(_∷_)mdo if|let?_=(#0x0.0,'c,#)→[0b_0^0e0| !x#←[]|_←[],then\case]

Change one of the patterns inside the list comprehension to use ViewPatterns

d∷_=id @(_∷_)mdo if|let?_=(#0x0.0,'d,#)→[0b_0^0e0| !x#←[]|(id→_)←[],then\case]

= 78 characters, requiring 23 language extensions

Use n+1 as pattern inside the ViewPattern with NPlusKPatterns

e∷_=id @(_∷_)mdo if|let?_=(#0x0.0,'e,#)→[0b_0^0e0| !x#←[]|(id->n+1)←[],then\case]

= 81 chars, slightly to long :(

But we can move the BinaryLiteral (with NumericUnderscores) to the NPlusKPattern.
And move the HexFloatLiteral to where the BinaryLiteral used to be.

f∷_=id @(_∷_)mdo if|let?_=(#'f,#)→[0x0.0^0e0| !x#←[]|(id->n+0b_0)←[],then\case]

:) 79 chars, 24 exts

Slightly expanding the challenge to include ghc flags.
We can replace both empty lists with typed holes _, saving to 2 characters.

Typed holes aren't a language feature, and are normally (handy) compile errors.
But we can ask GHC to please defer those errors to runtime
with the flag: -fdefer-typed-holes

g∷_=id @(_∷_)mdo if|let?_=(#'g,#)→[0x0.0^0e0| !x#←_|(id->n+0b_0)←_,then\case]

I think it's important here that those defered errors don't trigger at runtime,
that would be cheating.
But the code runs just as well as before, producing an empty list of integers.

-fdefer-out-of-scope-variables
Allows us to change the id in the ViewPattern to the undefined i,
saving 1 more character.

h∷_=id @(_∷_)mdo if|let?_=(#'h,#)→[0x0.0^0e0| !x#←_|(i->n+0b_0)←_,then\case]

76 chars, 24 language extensions, 2 ghc flags

{-# language UnboxedTuples #-}
{-# language ViewPatterns #-}
{-# language NPlusKPatterns #-}
{-# options_ghc -fdefer-typed-holes #-}
{-# options_ghc -fdefer-out-of-scope-variables #-}
Collapse
 
tfausak profile image
Taylor Fausak

Wonderful!

Many tricks like this in the Reddit thread too: reddit.com/r/haskell/comments/hzz8...

Collapse
 
leonschoorl profile image
Leon Schoorl

Oops, I broke my own rule on not having runtime errors.

The first empty list can't be a hole, or the bangpattern will trigger a deferred type error. +1 char
But I forgot the use the unicode in the view pattern. -1 char

h_=id @(__)mdo if|let?_=(#'h,#)[0x0.0^0e0| !x#[]|(in+0b_0)_,then\case]

So still 76 chars, 24 language extensions, 2 ghc flags