Posts

Showing posts with the label Haskell

Can Someone Explain Where Applicative Instances Arise In This Code?

Answer : This is the Applicative instance for ((->) r) , functions from a common type. It combines functions with the same first argument type into a single function by duplicating a single argument to use for all of them. (<$>) is function composition, pure is const , and here's what (<*>) translates to: s :: (r -> a -> b) -> (r -> a) -> r -> b s f g x = f x (g x) This function is perhaps better known as the S combinator. The ((->) r) functor is also the Reader monad, where the shared argument is the "environment" value, e.g.: newtype Reader r a = Reader (r -> a) I wouldn't say it's common to do this for the sake of making functions point-free, but in some cases it can actually improve clarity once you're used to the idiom. The example you gave, for instance, I can read very easily as meaning "is a character a letter or number". You get instances of what are called static arrows (see "A...

Could Not Find Module `Data.Map' -- It Is A Member Of The Hidden Package

Image
Answer : These general steps were helpful for me to resolve similar issues: Use Hoogle or Stackage to find the package where the module resides Note that Hoogle and Stackage are case-sensitive . Looking up Data.Map in Hoogle yields a list similar to the one below. Stackage has a slightly different style, but the basics are the same (mostly because it also uses Hoogle for lookup). The lines in green under the result headings show the name(s) of the containing (1) package(s) (in small caps) and (2) module(s) (capitalized). Open project-name.cabal in project root and add required package under build-depends: library hs-source-dirs: src build-depends: base >= 4.7 && < 5 , containers exposed-modules: Lib Issue stack build to download and build dependencies (or stack ghci if you plan to use it in the REPL) The reason you can import Data.Char and Data.List is that they are part of the package base , which is included with GHC and is always...