Well, people usually refer to the data structure that you have as a kind of tree, not a list. But anyway...
№ 1: Haskell , case . .
№2, : , Maybe. , , nulls , .
, , Java, null - , , . :
public Foo makeAFoo(Bar someBar)
... :
// Way
Bar theBar = getMeABar();
Foo result = makeAFoo(theBar);
// Way
Foo result2 = makeAFoo(null)
theBar null "" , , : , .
Haskell, , "hello" Nothing , , . :
- , :
"hello" :: String - :
Nothing :: Maybe String - :
Just "hello" :: Maybe String
# 1 # 3 - , . Maybe a, , , Just, " a, a Maybe a."
, Just - case, :
-- This still fails to compile!
cListGet :: CList a -> Maybe (a, CList a)
cListGet Nil = Nothing
cListGet xs@(NotNil nxs) =
case nxs of
-- I added 'Just' here and in the next line:
Sing x -> Just (x, Nil)
Append l r -> Just (fst $ cListGet (NotNil l), (Append (snd $ cListGet (NotNil l)), r))
, fst $ cListGet (NotNil l), : cListGet Maybe (a, CList a), fst (a, b), Maybe (a, b). cListGet, , Nothing Just (x, l'). ( snd $ cListGet (NotNil l).)
-, Append. (Append foo, bar), foo bar. Haskell , , , Haskell , " "; Haskell , , Append foo , bar - , , (Append foo, bar) (NNList a -> NNList a, NNList a) > .
: , , , , . , "" "" CList a. ? Haskell [a] [] : : - x x:xs, - xs.
, , "", , . :
cListHead :: CList a -> Maybe a
cListHead Nil = Nothing
-- No need to cram everything together into one definition; deal with
-- the NNList case in an auxiliary function, it easier...
cListGet (NotNil nxs) = Just (nnListHead nxs)
-- Note how much easier this function is to write, because since 'NNList'
-- doesn't have a 'Nil' case, there no need to mess around with 'Maybe'
-- here. Basically, by splitting the problem into two functions, only
-- 'cListHead' needs to care about 'Maybe' and 'Just'.
nnListHead :: NNList a -> a
nnListHead (Sing a) = a
nnListHead (Append l _) = nnListHead l
, , "" - . , " " CList NNList. :
example :: CList Int
example = NotNil (Append (Append (Sing 1) (Sing 2)) (Sing 3))
"" 1. , example, 2 3 1. CList , , . , , .
, "", :
NotNil
|
v
Append
/ \
v v
Sing Append
| / \
v v v
1 Sing Sing
| |
v v
2 3
= .