by Peaker · 13y ago · 0 comments
data Vector length a where
Empty :: Vector 0 a
Cons :: a -> Vector n a -> Vector (1 + n) a
But Haskell isn't very good at such encodings. Type-level computation is often not worth it.That said, even NonEmptyList is sometimes useful. For example, some Prelude functions could use it:
group :: [a] -> [NonEmptyList a]
Making common idioms like: map head . group ...
Safe again.Not sure what you mean by:
> How could you get the last element from your NonEmptyList? How could you tell how many elements in it?) Exactly two.
import Data.Monoid
import Data.Maybe
lastNel :: NonEmptyList a -> a
lastNel (NEL x xs) = fromMaybe x . getLast $ mconcat $ map (Last . Just) xs
lengthNel :: NonEmptyList a -> Int
lengthNel (NEL x xs) = 1 + length xs> Cons :: a -> Vector n a --> Vector (1 + n) a
This makes no sense, you can't specify constants in a type signature, and you certainly can't add constants to a type variable in one.
I may be the one not in the know here, if so please show me this magic.
{-# LANGUAGE DataKinds #-}
{-# LANGUAGE GADTs #-}
data Nat = Z | S Nat
data Vector l a where
Nil :: Vector Z a
Cons :: a -> Vector l a -> Vector (S l) aIs your problem mainly what you call this notion?