slist icon indicating copy to clipboard operation
slist copied to clipboard

Create patterns for smooth pattern-matching

Open vrom911 opened this issue 6 years ago • 2 comments

vrom911 avatar Mar 28 '19 05:03 vrom911

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

BlackCapCoder avatar Apr 08 '19 03:04 BlackCapCoder

@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.

vrom911 avatar Apr 09 '19 02:04 vrom911