^-- No, it doesn't entirely. My question covers ADDING patterns and type signatures interactively...which is apparently impossible.
The most basic things you might try do from early tutorials won't work in GHCi:
foo [] = []
foo (x:xs) = x : foo xs
That works if you put it into foo.hs
and at the GHCi prompt type :load foo.hs
. You can then invoke foo on a list and get the list back.
Early Google searches tell you that in GHCi you need a let
statement. But in this case (a function defined with multiple patterns) it won't work:
Prelude> let foo [] = []
Prelude> let foo (x:xs) = x : foo xs
Prelude> foo [1, 2, 3]
[1,2,3*** Exception: <interactive>:3:5-27: Non-exhaustive patterns
in function foo
The second "let" overwrote the first "let". Leaving out the let isn't an option. And it doesn't like it if you type in expressions like foo :: [a] -> [a]
either.
The tutorials seem to sidestep this and send you quickly into putting your code into files. What if you don't want to make a file, and want to work interactively? What are the options?