17
\$\begingroup\$

I tested the waters on this sort of question with python, but now I get to the real question I want to ask.

challenges are uniquely rewarding in Haskell because of Haskell's strict rule set. Type coercion which is the bread and butter for other languages in these sort of challenges is arguably non-existent but when pushed to the limits Haskell provides some weird and interesting solutions.

What are some for solving challenges in Haskell?

Please only include 1 tip per answer. If your tip only works for a specific compiler, or version of Haskell please note that.


What makes a good tip here?

There are a couple of criteria I think a good tip should have:

  1. It should be (somewhat) non obvious.

    Similar to the tips it should be something that someone who has golfed in python a bit and read the tips page would not immediately think of. For example "Replace a + b with a+b to avoid using spaces", is obvious to any golfer since it is already a way to make your code shorter and thus not a good tip for this question.

  2. It should not be too specific.

    Since there are many different types of source restrictions, answers here should be at least somewhat applicable to multiple source restrictions, or one common source restriction. For example tips of the form How to X without using character(s) Y are generally useful since banned characters is a common source restriction, or the byproduct of more unique restrictions. The thing your tip helps to do should also be somewhat general. For example tips of the form How to create numbers with X restriction are useful since many programs utilize numbers regardless of the challenge. Tips of the form How to implement Shor's algorithm with X restriction are basically just answers to a challenge you just invented and not very helpful to people solving other challenges.

\$\endgroup\$
1

3 Answers 3

11
\$\begingroup\$

Break tokens with comments

A neat fact I discovered a little while ago (and the impetus for this sort of tips question in the first place), is that a block comment allows you to split tokens apart without a space.

For example if you want to write

main=interact tail

Without using whitespace, parentheses or $ you can do the following:

main=interact{--}tail

Here {--} is a block comment, which normally would do nothing, however here placed between interact and tail it serves to prevent the two as being parsed as a single token, interacttail.

This could also be used to split other kinds of tokens for example:

main=print${--}-3

Here we split the $ and the - without using spaces or parentheses.

data{--}K=K

Here we split a keyword from a type declaration (which unlike the earlier example cannot be done with $).

Pretty much anywhere you would use a space outside a String or Char literal you can use a block comment instead.

\$\endgroup\$
9
\$\begingroup\$

Anything can be an infix operator

While (&) is usually the "prefix form" of an infix function, it doesn't have to be defined as infix, or even a function. The following definition of a unary function (#) with argument (&), a list of numbers, compiles and runs just fine:

(#)(&)=(&)!!0+(&)!!3

This could be useful if alphabetic characters are forbidden/restricted/penalized.

\$\endgroup\$
1
\$\begingroup\$

Name your variables __ and _'

Before you go naming things as infix operators if you need to avoid letters you can name things __ and _'. These are only 2 bytes long whereas infix operators are 3. And once you run out of 1 character infix operators ___, __', _'_ and _'' are all available.

You can see this used here by Lynn.

\$\endgroup\$

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge that you have read and understand our privacy policy and code of conduct.

Not the answer you're looking for? Browse other questions tagged or ask your own question.