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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 13, 20262026-05-13T15:00:05+00:00 2026-05-13T15:00:05+00:00

I have the following code to return the length of a cycle in a

  • 0

I have the following code to return the length of a cycle in a string:

module Main where
import Data.List

detec ys n | 2*n > (length ys) = error "no cycle"
           | t == h = (2*n - n)
           | otherwise = detec ys (n+1)
            where
                t = ys !! n
                h = if n == 0 then ys !! 1 else  ys !! (n*2)
f x = detec (show x) 0
answer = map f [1/x|x<-[1..100]]

But what I don’t know how to do is make it ignore the "no cycle" exception so that the list produced only contains the lengths of the strings which are cyclic.

How can I do this?

  • 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-13T15:00:05+00:00Added an answer on May 13, 2026 at 3:00 pm

    Please don’t use error to implement logic where the “erroneous” result is expected to occur.

    Instead, why not return Maybe n instead of just n, then use catMaybes to filter out the Nothings?

    The changes are easy enough:

    module Main where
    import Data.List
    import Data.Maybe
    
    detec ys n | 2*n > (length ys) = Nothing
               | t == h = Just (2*n - n)
               | otherwise = detec ys (n+1)
                where
                    t = ys !! n
                    h = if n == 0 then ys !! 1 else  ys !! (n*2)
    
    f x = detec (show x) 0
    answer = catMaybes $ map f [1/x|x<-[1..100]]
    

    By the way, you’re indexing past the end of the list; perhaps you meant to check 2*n + 1 > length ys? Drifting slightly off topic, I’d like to mention that !! and length are, for the most part, both inefficient and non-idiomatic when applied to lists, especially in an iterating construct like this. The list type is basically a cons cell list, which is an intrinsically recursive data structure, and is emphatically not an array. Ideally you should avoid doing anything with a list that can’t be easily expressed with pattern matching, e.g., f (x:xs) = ....

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

Sidebar

Related Questions

I have the following code string three() { return three; } void mutate(string& ref)
I have the following code: public ActionResult SomeAction() { return new JsonpResult { Data
I have the following code snippet.. I get the error Expected identifier, string or
I have the following code public static int unknown(String x) { if ((x.length()==1) &&
I have following code return (EseshEntities.Current.Users.Select(u => new { Comunity = u.Apartment.Building.District.City })).ToList(); if
I have this following code which will return all the current semesters. How do
I have following code, my target is going to return GMT+0 time in millisec.
suppose that,we have following code auto_ptr<T> source() { return auto_ptr<T>( new T(1) ); }
I have a following simple code: def get(): return [lambda: i for i in
I have the following code: pub.call_response = function() { return { units: [ {

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.