Lists defined as "Maybe in Haskell"? Why not?

You will not notice Maybe List, with the exception of error handling, for example, because the lists themselves are few Maybe: they have their own " Nothing": []and their own " Just": (:). I wrote a list type using Maybe functions and functions to convert standard and "experimental" lists. toStd . toExp == id.

data List a = List a (Maybe (List a))
    deriving (Eq, Show, Read)

toExp [] = Nothing
toExp (x:xs) = Just (List x (toExp xs))

toStd Nothing = []
toStd (Just (List x xs)) = x : (toStd xs)

What do you think of this as an attempt to reduce repetition in order to generalize?

Trees can also be defined using these lists:

type Tree a = List (Tree a, Tree a)

I have not tested this last piece of code.

+5
source share
5 answers

ADT ( - . ) (,), Either, (), (->), Void Mu

data Void --using empty data decls or
newtype Void = Void Void

Mu

newtype Mu f = Mu (f (Mu f))

,

data [a] = [] | (a:[a])

data [a] = Mu (ListF a)
data ListF a f = End | Pair a f

newtype ListF a f = ListF (Either () (a,f))

data Maybe a = Nothing | Just a

newtype Maybe a = Maybe (Either () a)

newtype ListF a f = ListF (Maybe (a,f))

mu

data List a = List (Maybe (a,List a))

data List a = List a (Maybe (List a))

- Maybe ( )

...

  • ADT

  • : . GHC.Generic


, . ,

hmm = List (Just undefined)

[a] = [] | (a:[a]). , Haskell . , ( ) "Lazy"

data SPair a b = SPair !a !b
data SEither a b = SLeft !a | SRight !b
data Lazy a = Lazy a --Note, this has no obvious encoding in Pure CBV languages,
--although Laza a = (() -> a) is semantically correct, 
--it is strictly less efficient than Haskell CB-Need 

.

+9

Haskell . , :

{-# LANGUAGE RankNTypes #-}

newtype List a = List { runList :: forall b. (a -> b -> b) -> b -> b }

nil :: List a
nil = List (\_ z -> z )

cons :: a -> List a -> List a
cons x xs = List (\f z -> f x (runList xs f z))

isNil :: List a -> Bool
isNil xs = runList xs (\x xs -> False) True

head :: List a -> a
head xs = runList xs (\x xs -> x) (error "empty list")

tail :: List a -> List a
tail xs | isNil xs = error "empty list"
tail xs = fst (runList xs go (nil, nil))
    where go x (xs, xs') = (xs', cons x xs)

foldr :: (a -> b -> b) -> b -> List a -> b
foldr f z xs = runList xs f z

, , :

fromNative :: [a] -> List a
fromNative xs = List (\f z -> foldr f z xs)

toNative :: List a -> [a]
toNative xs = runList xs (:) []

( ), , . , , , , .

? , , , :

  • head (x:xs) == x
  • tail (x:xs) == xs
  • [] == []
  • [] /= x:xs
  • xs == ys x == y, x:xs == y:ys
  • foldr f z [] == z
  • foldr f z (x:xs) == f x (foldr f z xs)

: :

newtype ExpList a = ExpList (Maybe (a, ExpList a))

toExpList :: List a -> ExpList a
toExpList xs = runList xs (\x xs -> ExpList (Just (x, xs))) (ExpList Nothing)

foldExpList f z (ExpList Nothing) = z
foldExpList f z (ExpList (Just (head, taill))) = f head (foldExpList f z tail)

fromExpList :: ExpList a -> List a
fromExpList xs = List (\f z -> foldExpList f z xs)
+9

Maybe, . List . Maybe (List a) [a]. , , , .

newtype List a = List (Maybe (a, List a))

. , , -, , ( undefined; ).

+7

, Functor, ?

instance Functor List
  where fmap f (List a as) = List (f a) (mapMaybeList f as)

mapMaybeList :: (a -> b) -> Maybe (List a) -> Maybe (List b)
mapMaybeList f as = fmap (fmap f) as

: List Functor, "" : Maybe Functor , ​​, Maybe . List, - ( ).

.


, , Haskell:

instance Comonad List
  where extract (List a _) = a
        duplicate x @ (List _ y) = List x (duplicate y)

, List comonadic, .

+1

Haskell, , , , . ( !) . "" , : " , ?" , .

Darcs:

data UseCache = YesUseCache | NoUseCache
    deriving ( Eq )

data DryRun = YesDryRun | NoDryRun
    deriving ( Eq )

data Compression = NoCompression
                 | GzipCompression
    deriving ( Eq )

, Bool? , , Darcs , ​​ ? , , :

type Slot = Maybe Bool                  -- OLD code
data Slot = InFirst | InMiddle | InLast -- newer code

, , ?

, . newtype, ,

newtype Role = Role { fromRole :: Text }
  deriving (Eq, Ord)

newtype KmClass = KmClass { fromKmClass :: Text }
  deriving (Eq, Ord)

newtype Lemma = Lemma { fromLemma :: Text }
  deriving (Eq, Ord)

Here you will notice that I did a curious thing, taking a perfectly good type Text, and then wrapped it in three different things. Three things have no new features compared to the usual old Text. They just have to be different. Honestly, I'm not quite sure that for me this was a good idea. I preliminarily think that this is because I am manipulating many different pieces of text for many reasons, but time will tell.

Do you see what I'm trying to get to?

+1
source

All Articles