Skip to content

@tibbe /Longest.hs
Created

Embed URL

HTTPS clone URL

Subversion checkout URL

You can clone with
or
.
Download ZIP
{-# 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
Something went wrong with that request. Please try again.