Skip to content

Instantly share code, notes, and snippets.

@tibbe
Created April 12, 2013 15:54
Show Gist options
  • Save tibbe/5373035 to your computer and use it in GitHub Desktop.
Save tibbe/5373035 to your computer and use it in GitHub Desktop.
{-# LANGUAGE BangPatterns #-}
{-# OPTIONS_GHC -funbox-strict-fields #-}
module Longest where
import qualified Data.ByteString as B
import qualified Data.ByteString.Char8 as BC
import Data.Word (Word8)
-- using custom data type (only works for 2 elements)
data TwoQueue = Empty
| OneIn !Word8
| TwoIn !Word8 !Word8
data PairS = PS !B.ByteString !Int
-- using custom data type (only works for 2 elements)
findSequenceCustom :: BC.ByteString -> Int
findSequenceCustom ss = count Empty (PS ss (B.length ss)) 0
where count !_ (PS _ 0) !res = res
count Empty (PS s n) res = count (OneIn $! B.head s) (PS (B.tail s) (n-1)) (res+1)
count (OneIn a) (PS s n) res
| B.head s == a = count (OneIn a) (PS (B.tail s) (n-1)) (res+1)
| otherwise = count (TwoIn a (B.head s)) (PS (B.tail s) (n-1)) (res+1)
count (TwoIn a b) (PS s n) res
| B.head s == a || B.head s == b = count (TwoIn a b) (PS (B.tail s) (n-1)) (res+1)
| otherwise = res
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment