For that newtype is treated as a whole different type in the type system, I'm wondering if there is any way to use pattern matching or iterate a list with newtype, as follow.
newtype Foo = Foo [Int]
bar :: Foo -> Int
bar (x : xs) = x + bar xs
bar [] = 0
There are multiple options.
Just manually wrap/unwrap the newtype right in place.
Implement the function on lists, and unwrap it once before passing to the function. In this case the list version is just
sum, so we can useMake an interface that allows to manipulate
Foovalues as if they were lists.Abstract. You don't really need the particular list deconstructors, you just need some way to implement a fold over the contained data. There is a standard
Foldableclass inbasefor just this, but it would require you container to be parametric over the contained type. Since it's not parametric, you need to useMonoFoldableclass from themono-traversablepackage instead.Note that generally, this sort of function should be implemented with a strict left fold instead of a right fold.