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

  • Home
  • SEARCH
  • 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 4119816
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 20, 20262026-05-20T23:08:21+00:00 2026-05-20T23:08:21+00:00

I’m trying to implement the levenshtein distance (or edit distance) in Haskell, but its

  • 0

I’m trying to implement the levenshtein distance (or edit distance) in Haskell, but its performance decreases rapidly when the string lenght increases.

I’m still quite new to Haskell, so it would be nice if you could give me some advice on how I could improve the algorithm. I already tried to “precompute” values (the inits), but since it didn’t change anything I reverted that change.

I know there’s already an editDistance implementation on Hackage, but I need it to work on lists of arbitrary tokens, not necessarily strings. Also, I find it a bit complicated, at least compared to my version.

So, here’s the code:

-- standard levenshtein distance between two lists
editDistance      :: Eq a => [a] -> [a] -> Int
editDistance s1 s2 = editDistance' 1 1 1 s1 s2 

-- weighted levenshtein distance
-- ins, sub and del are the costs for the various operations
editDistance'      :: Eq a => Int -> Int -> Int -> [a] -> [a] -> Int
editDistance' _ _ ins s1 [] = ins * length s1 
editDistance' _ _ ins [] s2 = ins * length s2 
editDistance' del sub ins s1 s2  
    | last s1 == last s2 = editDistance' del sub ins (init s1) (init s2)
    | otherwise          = minimum [ editDistance' del sub ins s1 (init s2)        + del -- deletion 
                                   , editDistance' del sub ins (init s1) (init s2) + sub -- substitution
                                   , editDistance' del sub ins (init s1) s2        + ins -- insertion
                                   ]

It seems to be a correct implementation, at least it gives exactly the same results as this online tool.

Thanks in advance for your help! If you need any additional information, please let me know.

Greetings,
bzn

  • 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-05-20T23:08:22+00:00Added an answer on May 20, 2026 at 11:08 pm

    Ignoring that this is a bad algorithm (should be memoizing, I get to that second)…

    Use O(1) Primitives and not O(n)

    One problem is you use a whole bunch calls that are O(n) for lists (haskell lists are singly linked lists). A better data structure would give you O(1) operations, I used Vector:

    import qualified Data.Vector as V
    
    -- standard levenshtein distance between two lists
    editDistance      :: Eq a => [a] -> [a] -> Int
    editDistance s1 s2 = editDistance' 1 1 1 (V.fromList s1) (V.fromList s2)
    
    -- weighted levenshtein distance
    -- ins, sub and del are the costs for the various operations
    editDistance'      :: Eq a => Int -> Int -> Int -> V.Vector a -> V.Vector a -> Int
    editDistance' del sub ins s1 s2
      | V.null s2 = ins * V.length s1
      | V.null s1 = ins * V.length s2
      | V.last s1 == V.last s2 = editDistance' del sub ins (V.init s1) (V.init s2)
      | otherwise            = minimum [ editDistance' del sub ins s1 (V.init s2)        + del -- deletion 
                                       , editDistance' del sub ins (V.init s1) (V.init s2) + sub -- substitution
                                       , editDistance' del sub ins (V.init s1) s2        + ins -- insertion
                                       ]
    

    The operations that are O(n) for lists include init, length, and last (though init is able to be lazy at least). All these operations are O(1) using Vector.

    While real benchmarking should use Criterion, a quick and dirty benchmark:

    str2 = replicate 15 'a' ++ replicate 25 'b'
    str1 = replicate 20 'a' ++ replicate 20 'b'
    main = print $ editDistance str1 str2
    

    shows the vector version takes 0.09 seconds while strings take 1.6 seconds, so we saved about an order of magnitude without even looking at your editDistance algorithm.

    Now what about memoizing results?

    The bigger issue is obviously the need for memoization. I took this as an opportunity to learn the monad-memo package – my god is that awesome! For one extra constraint (you need Ord a), you get a memoization basically for no effort. The code:

    import qualified Data.Vector as V
    import Control.Monad.Memo
    
    -- standard levenshtein distance between two lists
    editDistance      :: (Eq a, Ord a) => [a] -> [a] -> Int
    editDistance s1 s2 = startEvalMemo $ editDistance' (1, 1, 1, (V.fromList s1), (V.fromList s2))
    
    -- weighted levenshtein distance
    -- ins, sub and del are the costs for the various operations
    editDistance' :: (MonadMemo (Int, Int, Int, V.Vector a, V.Vector a) Int m, Eq a) => (Int, Int, Int, V.Vector a, V.Vector a) -> m Int
    editDistance' (del, sub, ins, s1, s2)
      | V.null s2 = return $ ins * V.length s1
      | V.null s1 = return $ ins * V.length s2
      | V.last s1 == V.last s2 = memo editDistance' (del, sub, ins, (V.init s1), (V.init s2))
      | otherwise = do
            r1 <- memo editDistance' (del, sub, ins, s1, (V.init s2))
            r2 <- memo editDistance' (del, sub, ins, (V.init s1), (V.init s2))
            r3 <- memo editDistance' (del, sub, ins, (V.init s1), s2)
            return $ minimum [ r1 + del -- deletion 
                             , r2 + sub -- substitution
                             , r3 + ins -- insertion
                                       ]
    

    You see how the memoization needs a single “key” (see the MonadMemo class)? I packaged all the arguments as a big ugly tuple. It also needs one “value”, which is your resulting Int. Then it’s just plug and play using the “memo” function for the values you want to memoize.

    For benchmarking I used a shorter, but larger-distance, string:

    $ time ./so  # the memoized vector version
    12
    
    real    0m0.003s
    
    $ time ./so3  # the non-memoized vector version
    12
    
    real    1m33.122s
    

    Don’t even think about running the non-memoized string version, I figure it would take around 15 minutes at a minimum. As for me, I now love monad-memo – thanks for the package Eduard!

    EDIT: The difference between String and Vector isn’t as much in the memoized version, but still grows to a factor of 2 when the distance gets to around 200, so still worth while.

    EDIT: Perhaps I should explain why the bigger issue is “obviously” memoizing results. Well, if you look at the heart of the original algorithm:

     [ editDistance' ... s1          (V.init s2)  + del 
     , editDistance' ... (V.init s1) (V.init s2) + sub
     , editDistance' ... (V.init s1) s2          + ins]
    

    It’s quite clear a call of editDistance' s1 s2 results in 3 calls to editDistance'… each of which call editDistance' three more times… and three more time… and AHHH! Exponential explosion! Luckly most the calls are identical! for example (using --> for “calls” and eD for editDistance'):

    eD s1 s2  --> eD s1 (init s2)             -- The parent
                , eD (init s1) s2
                , eD (init s1) (init s2)
    eD (init s1) s2 --> eD (init s1) (init s2)         -- The first "child"
                      , eD (init (init s1)) s2
                      , eD (init (init s1)) (init s2) 
    eD s1 (init s2) --> eD s1 (init (init s2))
                      , eD (init s1) (init s2)
                      , eD (init s1) (init (init s2))
    

    Just by considering the parent and two immediate children we can see the call ed (init s1) (init s2) is done three times. The other child share calls with the parent too and all children share many calls with each other (and their children, cue Monty Python skit).

    It would be a fun, perhaps instructive, exercise to make a runMemo like function that returns the number of cached results used.

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

Sidebar

Related Questions

I'm trying to decode HTML entries from here NYTimes.com and I cannot figure out
I am trying to understand how to use SyndicationItem to display feed which is
Basically, what I'm trying to create is a page of div tags, each has
link Im having trouble converting the html entites into html characters, (&# 8217;) i
Seemingly simple, but I cannot find anything relevant on the web. What is the
Does anyone know how can I replace this 2 symbol below from the string
I'm new to using the Perl treebuilder module for HTML parsing and can't figure
this is what i have right now Drawing an RSS feed into the php,
That's pretty much it. I'm using Nokogiri to scrape a web page what has
I have just tried to save a simple *.rtf file with some websites and

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.