lohasupplies.blogg.se

Haskell stack
Haskell stack







haskell stack haskell stack

And if you have f pred x somewhere, you should probably define f as an infix function anyway.

  • If e is preceded by = or an infix operator of fixity less than 6, you need a space: f= -2 defines f and k2 instead of kx and iterate pred x by.
  • If e is even moderately complex, you will need parentheses around e, but you can usually save a byte by moving them around: -length(take 3 x) is shorter than -(length$take 3 x).
  • To negate an expression e, just do -e.
  • Please let me know if I've missed something. This tip lists some short ways of expressing negation and subtraction in Haskell. The minus sign - is an annoying exception to many syntax rules. This will probably not be useful often, for it needs at least \$3-11\$ functions (depending on the need of parentheses, see comments), but it might be in case you have some list Monoid a => already.įurthermore mempty can be useful since it is shorter than Nothing or even pure Nothing, (,) etc.Īnother useful instance is Monoid a => Monoid (IO a) which has successfully been used for the currently shortest quine as (putStr print) "foo" ≡ putStr "foo" print "foo" It might be worth noting that there is mconcat too: mconcat ≡ f1 f2 … fN Ord k => Monoid (Map k v) - from Data.Map (Monoid a, Monoid b) => Monoid (a,b) - (also 3-,4- and 5-tuples) Instances that might be useful not covered above: () Which is shorter than f n=n*(n-1) or even (*)=succ.ģ: Note that we need to use (+1) and cannot use succ since GHC can't know that there is an instance of Enum. Using this we can for example write \$n\$th oblong number as id(+1) Given that the consensus is to allow implicitly typed functions, we are probably allowed to do so if the challenge does not talk about a specific implementation for integers. It can not decide for us which one it should use, therefore there are two newtypes which allow this - Sum and Product. Where are the \$(\mathbb)\$ monoid, we need to tell GHC that. Using this trick we have these (not exhaustive) helpers, note that we can chain them: duplicate = id idĢ: This could also be (>) for the same bytecount be shorter (see comment by Ørjan Johansen). However, this is not the only useful case: Knowing that -> is an instance of Monoid, we also have a -> -> an instance of Monoid (you can iterate this as many times as you want): (drop take) 2 ≡ (drop 2 take 2) ġ: Note that Monoid a \$\implies\$ Semigroup a.

    haskell stack

    So we have for example: (init reverse) ≡ init reverse This can be quite useful with for example lists, knowing that () is the same as (++) for lists. Now these might seem useless at first glance, but looking closer we notice that Monoid a => (x -> a) is an instance of Monoid 1, in which case we have the implementation: (f g) xs = f xs g xs This gives us: () :: Semigroup a => a -> a -> a With the Semigroup Monoid Proposal and the release of GHC 8.4.1, Semigroup has been moved to the Prelude. Not often a life-saver, but if you can infer the type, mempty is one byte shorter than Nothing, so there's that. It's almost completely useless, but or :: (a, Bool) -> Bool is one character shorter than snd, and elem b is shorter than (=b).snd. Also, is useful in the Applicative instance for lists: Prelude> (,)"abcd" These useful operators from Data.Applicative made it in! is just fmap, so you can replace map f x and fmap f x with fx everywhere and win back bytes. It's the latest version, and Prelude got some new additions that are useful for golfing: The () and () operators

    haskell stack

    The first version of GHC that contained this stuff was released on March 27, 2015.









    Haskell stack