Reference: http://www.cse.unsw.edu.au /~ En1000/Haskell/inbuilt.html
Http://www.cse.unsw.edu.au /~ En1000/Haskell/hof.html
In ghci, you can use: type to view the object type, as described in http://www.cnblogs.com/long123king/p/3837686.html,
In Haskell, a function is also a special object. An object has a type. A function can be passed as a parameter, assigned a value, created, and destroyed.
Prelude> :type (+)(+) :: Num a => a -> a -> a
How can we explain this? ":" is the name of the function, followed by the type or prototype of the function object.
"=>" The preceding num A indicates the parameter type,
Prelude> :type 11 :: Num a => a
The input and output types of the function are declared.
The reason for the existence of multiple-> is that the (+) function object actually contains a simpler function, such as (+) 2, which means "in use (+) when a function object is used, the first parameter is fixed to 2, which is similar to the bind in boost. The type of this function object is num A => A->,
Pass another parameter to this simple function and the result is also a type. Therefore, (+) is a composite function.
Function objects that require multiple parameters can be decomposed into compound functions composed of simple functions in one step.
Prelude> (+) 2<interactive>:35:1: No instance for (Num a0) arising from a use of `+‘ The type variable `a0‘ is ambiguous Possible fix: add a type signature that fixes these type variable(s) Note: there are several potential instances: instance Num Double -- Defined in `GHC.Float‘ instance Num Float -- Defined in `GHC.Float‘ instance Integral a => Num (GHC.Real.Ratio a) -- Defined in `GHC.Real‘ ...plus three others In the expression: (+) 2 In an equation for `it‘: it = (+) 2<interactive>:35:1: No instance for (Show (a0 -> a0)) arising from a use of `print‘ Possible fix: add an instance declaration for (Show (a0 -> a0)) In a stmt of an interactive GHCi command: print itPrelude> :type (+) 2(+) 2 :: Num a => a -> a
Special symbols must explicitly use parentheses to indicate that this is a function object,
For common function objects, brackets can also be used to indicate the identities of function objects.
Prelude> :type +<interactive>:1:1: parse error on input `+‘Prelude> :type (+)(+) :: Num a => a -> a -> aPrelude> :type namesnames :: [Char]Prelude> :type headhead :: [a] -> aPrelude> :type (head)(head) :: [a] -> a
Let's look at a more complex function object.
Prelude> :type mapmap :: (a -> b) -> [a] -> [b]
This function object contains two simple function objects. (A-> B) is a function that converts an object of type A to an object of type B;
(A-> B)-> [a], is another function, which means "fixed the first parameter to [a] when executing the overall function object."
This idea of breaking a composite function object (a function object containing multiple parameters) into several simple function objects is to support the design concept of "function as an object,
In this way, you can pass the simple function decomposed above as a parameter to the composite function object.
For example
Prelude> let nums = [1..100]Prelude> map ((*) 2) nums[2,4,6,8,10,12,14,16,18,20,22,24,26,28,30,32,34,36,38,40,42,44,46,48,50,52,54,56,58,60,62,64,66,68,70,72,74,76,78,80,82,84,86,88,90,92,94,96,98,100,102,104,106,108,110,112,114,116,118,120,122,124,126,128,130,132,134,136,138,140,142,144,146,148,150,152,154,156,158,160,162,164,166,168,170,172,174,176,178,180,182,184,186,188,190,192,194,196,198,200]
Although function objects like (+) 2 do not seem to be a common usage in most cases.
How to get information about the current runtime environment in prelude
Prelude> :help Commands available from the prompt: <statement> evaluate/run <statement> : repeat last command :{\n ..lines.. \n:}\n multiline command :add [*]<module> ... add module(s) to the current target set :browse[!] [[*]<mod>] display the names defined by module <mod> (!: more details; *: all top-level names) :cd <dir> change directory to <dir> :cmd <expr> run the commands returned by <expr>::IO String :ctags[!] [<file>] create tags file for Vi (default: "tags") (!: use regex instead of line number) :def <cmd> <expr> define command :<cmd> (later defined command has precedence, ::<cmd> is always a builtin command) :edit <file> edit file :edit edit last module :etags [<file>] create tags file for Emacs (default: "TAGS") :help, :? display this list of commands :info [<name> ...] display information about the given names :issafe [<mod>] display safe haskell information of module <mod> :kind <type> show the kind of <type> :load [*]<module> ... load module(s) and their dependents :main [<arguments> ...] run the main function with the given arguments :module [+/-] [*]<mod> ... set the context for expression evaluation :quit exit GHCi :reload reload the current module set :run function [<arguments> ...] run the function with the given arguments :script <filename> run the script <filename> :type <expr> show the type of <expr> :undef <cmd> undefine user-defined command :<cmd> :!<command> run the shell command <command> -- Commands for debugging: :abandon at a breakpoint, abandon current computation :back go back in the history (after :trace) :break [<mod>] <l> [<col>] set a breakpoint at the specified location :break <name> set a breakpoint on the specified function :continue resume after a breakpoint :delete <number> delete the specified breakpoint :delete * delete all breakpoints :force <expr> print <expr>, forcing unevaluated parts :forward go forward in the history (after :back) :history [<n>] after :trace, show the execution history :list show the source code around current breakpoint :list identifier show the source code for <identifier> :list [<module>] <line> show the source code around line number <line> :print [<name> ...] prints a value without forcing its computation :sprint [<name> ...] simplifed version of :print :step single-step after stopping at a breakpoint :step <expr> single-step into <expr> :steplocal single-step within the current top-level binding :stepmodule single-step restricted to the current module :trace trace after stopping at a breakpoint :trace <expr> evaluate <expr> with tracing on (see :history) -- Commands for changing settings: :set <option> ... set options :seti <option> ... set options for interactive evaluation only :set args <arg> ... set the arguments returned by System.getArgs :set prog <progname> set the value returned by System.getProgName :set prompt <prompt> set the prompt used in GHCi :set editor <cmd> set the command used for :edit :set stop [<n>] <cmd> set the command to run when a breakpoint is hit :unset <option> ... unset options Options for ‘:set‘ and ‘:unset‘: +m allow multiline commands +r revert top-level expressions after each evaluation +s print timing/memory stats after each evaluation +t print type after evaluation -<flags> most GHC command line flags can also be set here (eg. -v2, -fglasgow-exts, etc.) for GHCi-specific flags, see User‘s Guide, Flag reference, Interactive-mode options -- Commands for displaying information: :show bindings show the current bindings made at the prompt :show breaks show the active breakpoints :show context show the breakpoint context :show imports show the current imports :show modules show the currently loaded modules :show packages show the currently active package flags :show language show the currently active language flags :show <setting> show value of <setting>, which is one of [args, prog, prompt, editor, stop] :showi language show language flags for interactive evaluation
Prelude> :show modulesPrelude> :show contextPrelude> :show bindingsnames :: [Char] = "Daniel King"nums :: [Integer] = 1 : 2 : 3 : 4 : 5 : ....it :: [Integer] = 2 : 4 : 6 : 8 : 10 : ....Prelude> :show importsimport Prelude -- implicitPrelude> :show packagesactive package flags: nonePrelude> :show languagesbase language is: Haskell2010with the following modifiers: -XNoDatatypeContexts -XNondecreasingIndentation
Prelude> foldl ((+)) 0 [1..100]5050
Prelude> :type mapmap :: (a -> b) -> [a] -> [b]Prelude> :type filterfilter :: (a -> Bool) -> [a] -> [a]Prelude> :type foldrfoldr :: (a -> b -> b) -> b -> [a] -> bPrelude> :type foldlfoldl :: (a -> b -> a) -> a -> [b] -> a
Prelude> filter ((>) 50) nums[1,2,3,4,5,6,7,8,9,10,11,12,13,14,15,16,17,18,19,20,21,22,23,24,25,26,27,28,29,30,31,32,33,34,35,36,37,38,39,40,41,42,43,44,45,46,47,48,49]
Prelude> :type foldrfoldr :: (a -> b -> b) -> b -> [a] -> bPrelude> foldr (:) "King" [‘D‘,‘a‘,‘n‘,‘i‘,‘e‘,‘l‘, ‘ ‘]"Daniel King"
But foldl won't work.