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 7745865
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 1, 20262026-06-01T10:07:51+00:00 2026-06-01T10:07:51+00:00

* Simple question : Why does this function throw an exception when evaluated ?

  • 0

*Simple question : Why does this function throw an exception when evaluated ? *

  • In the case where there is a duplicate in the string , there is a Class cast exception thrown upon finding the duplicate.

  • In the case where the string has no duplicates, a NullPointerException is thrown.

*The Code *

(defn first-duplicate-char [str-in]
      (loop [list-Rem (seq str-in) set-Seen (set [])]
        (print (type list-Rem) " " list-Rem (next list-Rem) "\n")
        (if (= 0 (count str-in))
            nil
            (if (some #(= (first list-Rem) %) set-Seen)
                (first list-Rem)
                (recur  
                    (seq (next list-Rem))
                    (conj set-Seen (first list-Rem)))))))
  • 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-01T10:07:52+00:00Added an answer on June 1, 2026 at 10:07 am

    Your problem is that (= 0 (count str-in)) never changes, so you eventually try to call first on nil. [EDIT: I’m wrong, your code actually works as is — that if statement is just a no-op. I hope you enjoy the rest of this answer anyway.]

    Instead, you should be calling next (not rest) on list-Rem in the recur and using list-Rem directly in the if test, using the property of next that returns nil for an empty seq. Here’s how I would rewrite your code:

    (defn first-duplicate [in]
      (loop [seen #{}
             remain (seq in)]
        (when-let [[head & tail] remain]
          (if (contains? seen head)
            head
            (recur (conj seen head) tail)))))
    

    Changes:

    • lowercasing names
    • no need to call seq on the output of next or rest
    • with sets, contains? (check for existence of key) is faster than some (run a predicate on elements until something yields a truthy value)
    • set literal
    • using when to return nil on test failure
    • using when-let to destructure and bind first char and rest

    Stylistic changes:

    • names changed to be less specific (not restricted to strings, for instance)
    • change order of loop locals so that recur looks more like structural recursion
    • put loop locals on separate lines
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

My question is simple. Why wrong pattern matching does not throw exception in Maybe
Simple question - what does this actually do? var oq = (ObjectQuery<TEntity>)L2EQuery; return ExecuteFirstorDefault<TEntity>(oq,
this is a simple question: does Silverlight (3.0?) support this kind of encoding? Encoding
Simple question: How does this translate to VB.Net Dispatcher.BeginInvoke(() => listening = false); listening
I have a very simple and maybe dumb question: Why does this work? def
Simple question, to repeat the title: Does closing the WinForms application stops all active
Very simple question. What does the term 'seeding' mean in general? I'll put the
Have a rather simple question. Does anyone knows if i can use jparallax both
I'm embarrassed to ask such a simple question. My term does not start for
Simple question here: is there any way to convert from a jagged array to

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.