haskell - Non exhaustive pattern in function in GHCi -
I want to create a function that displays the last element in the list. This is my code:
< Pre> ghci & gt; My last :: [A] - & gt; A GSC & gt; My last [] = error ghci & gt; My real [x] = x ghci & gt; My list (x: xs) = myLast xs
and I get the following error:
*** Exception: The non-complete pattern My latest in the function
I understand that when you recall a case you get this error, but I think I have included all the possibilities . Any ideas?
If you use two
in each line, then each definition A new function will be named myLast
, to hide all the previous definitions, then whatever you are ending with is
GHCI> Is equal to: let alone my last (x: x) = my last x
> You probably want to create a hyscale file,
MyLast.hs
, where module is MyLast, where is myLast :: [a] ] - & gt; A myLast [] = error with myLast [x] = x myLast (x: xs) = myLast xs
then you will get that file with The keyword or , which I want, ghci MyLast.hs Can load in GHCi. Code>.
let
is required only if you are already in GHCI (or, in some modes like IO
, or any other function ) And want to create an local definition but then you should only use twice
, e.g., <. MyLast [] = Error; MyLast [x] = x; MyLast (x: xs) = myLast xs
twice Lost :: [int] - & gt; [EIT] Let me lie = Give my [] = error my list [x] = x my list (x: x) = in my list x \ xs -> 2 * The last xs
likes to write twice as Lust = (2 *). MyLast where myLast [] = Error myLast [x] = x myLast (x: xs) = myLast xs
Comments
Post a Comment