haskell - Defining function signature in GHCi -
defining function signature in haskell's interpreter ghci doesn't work. copying example this page:
prelude> square :: int -> int <interactive>:60:1: error: • no instance (show (int -> int)) arising use of ‘print’ (maybe haven't applied function enough arguments?) • in stmt of interactive ghci command: print prelude> square x = x * x
how can declare function signature , give function definition in haskell interactively? also: why can't evaluate function , see type (e.g. prelude> square
) once has been defined?
you can define function signature in ghc
interactive shell. problem need define functions in single command.
you can use semicolon (;
) split between 2 parts:
prelude> square :: int -> int; square x = x * x
note same holds function multiple clauses. if write:
prelude> is_empty [] = true prelude> is_empty (_:_) = false
you have overwritten previous is_empty
function second statement. if query empty list, get:
prelude> is_empty [] *** exception: <interactive>:4:1-22: non-exhaustive patterns in function is_empty
so ghci
took last definition single clause function definition.
again have write like:
prelude> is_empty[] = true; is_empty (_:_) = false
Comments
Post a Comment