Currying subtraction
You can use the subtract function instead of – if you want to right-section subtraction: map (subtract 1) [1..5]
You can use the subtract function instead of – if you want to right-section subtraction: map (subtract 1) [1..5]
This can’t be done generically over all MonadIO instances because of the IO type in a negative position. There are some libraries on hackage that do this for specific instances (monad-control, monad-peel), but there’s been some debate over whether they are semantically sound, especially with regards to how they handle exceptions and similar weird IOy … Read more
My preferred implementation for this is fmap . fmap :: (Functor f, Functor f1) => (a -> b) -> f (f1 a) -> f (f1 b) If only because it is fairly easy to remember. When instantiating f and f1 to (->) c and (->) d respectively you get the type (a -> b) -> … Read more
It’s pretty well-known that general function equality is undecidable in general, so you’ll have to pick a subset of the problem that you’re interested in. You might consider some of these partial solutions: Presburger arithmetic is a decidable fragment of first-order logic + arithmetic. The universe package offers function equality tests for total functions with … Read more
It’s a strictness declaration. Basically, it means that it must be evaluated to what’s called “weak head normal form” when the data structure value is created. Let’s look at an example, so that we can see just what this means: data Foo = Foo Int Int !Int !(Maybe Int) f = Foo (2+2) (3+3) (4+4) … Read more
I talk a bit about this in Engineering Large Projects in Haskell and in the Design and Implementation of XMonad. Engineering in the large is about managing complexity. The primary code structuring mechanisms in Haskell for managing complexity are: The type system Use the type system to enforce abstractions, simplifying interactions. Enforce key invariants via … Read more
The basic indentation rules are actually quite simple: after the keywords which start a block (where,let,do,case .. of) note down the column where the next word starts (which might be in the next line) lines indented exactly as that are new entries in the block lines indented more than that continue the previous entry a … Read more
Normally, definition of primes stream in Richard Bird’s formulation of the sieve of Eratosthenes is self-referential: import Data.List.Ordered (minus, union, unionAll) ps = 2 : minus [3..] — `:` is “cons” (foldr (\p r -> p*p : union [p*p+p, p*p+2*p..] r) [] ps) The primes ps produced by this definition are used as the input … Read more
It’s the monomorphism restriction which says that all values, which are defined without parameters and don’t have an explicit type annotation, should have a monomorphic type. This restriction can be disabled in ghc and ghci using -XNoMonomorphismRestriction. The reason for the restriction is that without this restriction long_calculation 42 would be evaluated twice, while most … Read more
Have you heard of div and mod? You’ll probably want to reverse the list of numbers if you want to treat the most significant digit first. Converting the number into a string is an impaired way of doing things. 135 `div` 10 = 13 135 `mod` 10 = 5 Generalize into a function: digs :: … Read more