<!-- lang-all: lang-haskell --> #[Haskell](https://www.haskell.org/) ### Basic IO In Haskell, a valid answer is usually a function, unless a full program is required by the challenge. Even if a challenge requires you to output an infinite stream of data (like all prime numbers), you can just write a function that returns an infinite list or string. ### Functions These kinds of functions are valid answers in a code golf challenge by default: 1. A named function (here `f`) with arguments: f x=x++map g x g n=div n 2*div n 3 2. A named function (here `f`) without arguments: f=(++)=<<map g g n=div n 2*div n 3 3. An unnamed function (here, the first line) without arguments: (++)=<<map g g n=div n 2*div n 3 4. Binary functions can be defined as infix operators (here `!`): x!y=x++map g y g n=div n 2*div n 3 5. For challenges where some output should be produced with out taking any input, the object itself counts as function which does not take any argument due to lazy evaluation (see [this meta question](https://codegolf.meta.stackexchange.com/q/12924/56433)): ['a'..'z']++['A'..'Z']++['0'..'9'] In all cases, you can have auxiliary functions and/or constants, here `g`, defined on separate lines or separated by semicolons (the former is preferred for readability, since the byte count is the same). Function definitions that use pattern matching are spread across multiple lines: f 'A'='B' f 'B'='A' f x=x All functions are curried (partially applied) by default, so the following four answers are equivalent: f n x=map(take n)x f n=map(take n) f=map.take map.take Haskell has no concept of mutable values, so all functions must return their result instead of modifying the arguments in place. The type of a function can be more general than the challenge requires, e.g. when the challenge asks to reverse a string, which in Haskell is a list of characters, then a function reversing a list with elements of any type is acceptable. If the type of a function can not be inferred without the function being called with an argument of the correct type, you can assume such a call exists (see [this meta question](https://codegolf.meta.stackexchange.com/q/14281/56433)). Functions are allowed to wrap their return value in the `Maybe` Monad (see [this meta question](https://codegolf.meta.stackexchange.com/questions/14199/is-outputting-a-maybe-acceptable-in-haskell)). ### Full programs Full programs either read their input from the user or take command line arguments, and print the result to the terminal. A full program is defined by `main`, and may of course include auxiliary definitions: main=print.map g=<<readLn g n=div n 2*div n 3 Input format is usually flexible, so you can take lists in the Haskell format `[1,2,3]` etc. ### Hybrid answers You can also use a function that prints its result to the terminal (and has a type like `a -> IO ()`), or a "function" that reads input from the user and computes a result (and has a type like `IO a`). These are rare in practice. ### Libraries and extensions Libraries can be imported with the `import` keyword at the beginning of a file: import Data.List f x=map(++x)$tails x Imports must be included in your byte count. GHC extensions can also be used (but almost always cost too many bytes to be useful): {-# LANGUAGE ParallelListComp #-} f x y=[a-b|a<-x|b<-y,odd b] ### Data types Haskell has a rigid type system, so some specific tasks that are easy in scripting languages are cumbersome in Haskell. The most common issues are inputs/outputs of varying types (like "if input is a number, increment it, if it's a string, append a space") and arbitrarily nested arrays (like `[[1],2]`), neither of which are supported in Haskell. In such cases, you basically have three options: 1. Use a built-in or `import`ed type that implements this behavior: `Either` for varying types, `Data.Tree` for nested lists. 2. Define a custom datatype. The most common approach to nested lists is data N=I Int|L[N] where `Int` can be replaced with whichever base type you need. 3. Use string representations in some reasonable format. All Haskell compilers implement type inference, meaning that you (usually) don't have to specify the types of functions and values, since the compiler infers them for you. It doesn't matter if the inferred type is more general than what is required. ### Truthiness Only values of type `Bool` have truthiness: `True` is truthy and `False` is falsy. ### Compiler If nothing else is stated a submission is assumed to run correctly in the current release of the [Glasgow Haskell Compiler GHC](https://www.haskell.org/ghc/). If your answer needs an other compiler or interpreter, e.g. Lambdabot, or relies on a specific GHC version, it should be submitted accordingly as e.g. **Haskell Lambdabot** or **Haskell GHC 6.10**. Answers assuming a REPL (Read-Eval-Print Loop) environment are allowed though not encouraged. They too should be indicated accordingly, e.g. as **Haskell GHCI**. ###Tips Tips for golfing in Haskell are collected [here](https://codegolf.stackexchange.com/questions/19255/tips-for-golfing-in-haskell).