Skip to content

Instantly share code, notes, and snippets.

@kseo
Created February 2, 2016 15:13
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save kseo/1c781c3c940ff855d569 to your computer and use it in GitHub Desktop.
Save kseo/1c781c3c940ff855d569 to your computer and use it in GitHub Desktop.
An implementation of length function in terms of Const functor
import Prelude hiding (length)
newtype Const a b = Const a
unConst :: Const c a -> c
unConst (Const x) = x
length' :: [a] -> Const Int a
length' [] = Const 0
length' (x:xs) = Const (1 + unConst (length' xs))
length :: [a] -> Int
length xs = unConst (length' xs)
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment