Browse Source

Problem 10

completed
Craige McWhirter 7 months ago
parent
commit
c2ee66081f
Signed by: craige GPG Key ID: 7DBA9F5689EFB6AA
  1. 16
      encode.hs

16
encode.hs

@ -0,0 +1,16 @@
{-# LANGUAGE StandaloneDeriving #-}
{-# OPTIONS_GHC -Wno-orphans #-}
-- Run-length encoding of a list. Use the result of problem P09 to implement the
-- so-called run-length encoding data compression method. Consecutive duplicates
-- of elements are encoded as lists (N E) where N is the number of duplicates of
-- the element E.
-- map: returns a list constructed by appling a function (the first argument) to
-- all items in a list passed as the second argument
import Data.List (group)
encode :: Eq a => [a] -> [(Int, a)]
encode = map (\x -> (length x, head x)) . group
Loading…
Cancel
Save