kowainik / slist

♾️ Sized list
https://kowainik.github.io/projects/slist
Mozilla Public License 2.0
46 stars 6 forks source link

Create patterns for smooth pattern-matching #2

Open vrom911 opened 5 years ago

BlackCapCoder commented 5 years ago

I vote to create one bi-directional pattern for easy construction/deconstruction and also a pattern for the empty list

{-# LANGUAGE PatternSynonyms, ViewPatterns, RecordWildCards #-}

infixr 5 :-
pattern x :- xs <- (uncons->(x,xs)) where
  x :- Slist{..} = Slist (x : sList) (sSize + 1)

pattern Nil = Slist [] 0

Usage:

myList = 1 :- 2 :- 3 :- Nil

head' (x:-_) = x
tail' (_:-x) = x

sum' (x:-xs) = x + sum' xs
sum' Nil     = 0
vrom911 commented 5 years ago

@BlackCapCoder the patterns you're suggesting don't capture the size in any way; however, this issue is created because I was working a lot with pattern-matching on the size.