File: ViewPatterns.hs

package info (click to toggle)
haskell-ghc-exactprint 1.7.1.0-1
  • links: PTS
  • area: main
  • in suites: forky, sid, trixie
  • size: 6,044 kB
  • sloc: haskell: 32,076; makefile: 7
file content (23 lines) | stat: -rw-r--r-- 541 bytes parent folder | download | duplicates (3)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
{-# LANGUAGE ViewPatterns #-}

-- From https://ghc.haskell.org/trac/ghc/wiki/ViewPatterns
import Prelude hiding (length)

data JList a = Empty
             | Single a
             | Join (JList a) (JList a)

data JListView a = Nil
                 | Cons a (JList a)


view :: JList a -> JListView a
view Empty = Nil
view (Single a) = Cons a Empty
view (Join (view -> Cons xh xt) y) = Cons xh $ Join xt y
view (Join (view -> Nil) y) = view y

length :: JList a -> Integer
length (view -> Nil) = 0
length (view -> Cons x xs) = 1 + length xs