public
Created

  • Download Gist
Longest.hs
Haskell
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26
{-# 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

Please sign in to comment on this gist.

Something went wrong with that request. Please try again.