Any way to add templates, sign a signature, to a function in GHCi?

^ - No, it's not really. My question covers ADDING patterns and signature types interactively ... which seems to be impossible.

The simplest things you can try to do from earlier tutorials won't work in GHCi:

foo [] = []
foo (x:xs) = x : foo xs

This works if you put it in foo.hsand at the GHCi type prompt :load foo.hs. Then you can call foo on the list and return the list.

Googleโ€™s early search queries tell you that you need a carrier in GHCi let. But in this case (a function defined with several templates) it will not 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โ€ rewrite the first โ€œletโ€. Leaving, let this not be an option. And it is not pleasant if you enter expressions like foo :: [a] -> [a].

, , . , ? ?

+1
2

. GHCi , , .

"" โ€‹โ€‹ . . , "", :

foo :: [a] -> [a]
foo [] = []
foo (x:xs) = x : foo xs

foo . ... , , :

foo :: [a] -> [a]
foo [] = []

bar = 3

foo (x:xs) = x : foo xs

(: , . , , . Haskell?)

, GHCi. :

let foo :: [a] -> [a] ; foo [] = [] ; foo (x:xs) = x : xs

, , . let.

+3

:

Prelude> :set +m
Prelude> let
Prelude| foo [] = []
Prelude| foo (x:xs) = x : foo xs
Prelude| 
Prelude> foo [1,2,3]
[1,2,3]
+2

All Articles