Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • SEARCH
  • Home
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 8949487
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 15, 20262026-06-15T13:13:45+00:00 2026-06-15T13:13:45+00:00

I made a implementation of the KMP algorithm’s fail table. kmp s = b

  • 0

I made a implementation of the KMP algorithm’s fail table.

kmp s = b
    where a = listArray (0,length s-1) s
          b = 0:list 0 (tail s)
          list _ [] = [] 
          list n (x:xs) 
            | x==a!n    = (n+1):list (n+1) xs
            | n > 0     = list (b!!(n-1)) (x:xs)
            | otherwise = 0:list 0 xs

b is a list, and b!!(n-1) in the last line is slow, therefore I wish to speed it up and did the following.

kmp s = b
    where a = listArray (0,length s-1) s
          t = listArray (0,length s-1) b
          b = 0:list 0 (tail s)
          list _ [] = [] 
          list n (x:xs) 
            | x==a!n    = (n+1):list (n+1) xs
            | n > 0     = list (t!(n-1)) (x:xs)
            | otherwise = 0:list 0 xs

Note the only difference is to replace b!! by t! and declares t to be the array generated from b.

For the same input, the original code have the correct output, but the new one just outputs <<loop>>.

How can one fix this problem?

  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-06-15T13:13:47+00:00Added an answer on June 15, 2026 at 1:13 pm

    Your problem is that the list b needs the array t to determine its structure (length). But the array t needs the length of the list before it exists:

    listArray :: Ix i => (i,i) -> [e] -> Array i e
    listArray (l,u) es = runST (ST $ \s1# ->
        case safeRangeSize (l,u)            of { n@(I# n#) ->
        case newArray# n# arrEleBottom s1#  of { (# s2#, marr# #) ->
        let fillFromList i# xs s3# | i# ==# n# = s3#
                                   | otherwise = case xs of
                []   -> s3#
                y:ys -> case writeArray# marr# i# y s3# of { s4# ->
                        fillFromList (i# +# 1#) ys s4# } in
        case fillFromList 0# es s2#         of { s3# ->
        done l u n marr# s3# }}})
    

    As you can see, first a raw array of appropriate size is allocated, then it is filled with arrEleBottom (which is an error call with message undefined array element), then the list is traversed and the list elements are written to the array (the list elements can refer to array values without problem). Then, finally, the array is frozen. Before the array is frozen, it cannot be accessed from outside the filling code (it’s basically an ST s computation).

    The simplest way to fix it is IMO to use a mutable array in an ST s monad,

    kmp s = elems b
      where
        l = length s - 1
        a = listArray (0, l) s
        b = runSTArray $ do
               t <- newArray_ (0,l)
               writeArray t 0 0
               let fill _ _ [] = return t
                   fill i n (x:xs)
                     | x == a!n = do
                            writeArray t i (n+1)
                            fill (i+1) (n+1) xs
                     | n > 0    = do
                            k <- readArray t (n-1)
                            fill i k (x:xs)
                     | otherwise = do
                            writeArray t i 0
                            fill (i+1) 0 xs
               fill 1 0 (tail s)
    

    is a very direct (and a bit inefficient) translation of the code.

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I have made a List of Lists implementation of a sparse matrix, and I've
So I have a self-made double-linked-list implementation that is being used as a stand
I just created an implementation of LinkedList(just for self-education purpose.). I made it run,
I made a UITableViewCell and I have 20 rows in that table in which
How are AJAX requests made asynchronous if Javascript is not multi-threaded? Is the implementation
I have made a simple implementation of INotifyDataErrorInfo in a WPF 4.5 project. This
I made a Facebook async implementation where you can like different items of the
I made a custom control called SmartTabItem , currently just the default implementation: using
Edit: I've made my own implementation which is on GitHub I was wondering, is
I have method in Class which is implementation of Interface. When I made it

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.