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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 30, 20262026-05-30T18:43:26+00:00 2026-05-30T18:43:26+00:00

I have a simple program (it was the second question on CCC 2012 )

  • 0

I have a simple program (it was the second question on CCC 2012) that takes a list of numbers and determines if there is any strictly increasing/decreasing/constant sequence going on. For example:

1 2 3 4 7 8 => Increasing
5 1 -2 -100 => Decreasing
9 9 9 9 9 9 => Constant
1 2 3 4 5 0 => Nothing

I was completely blown away by how smart Haskell was when I coded this. For some reason, when I typed in the numbers interactively into stdin, in was giving me the answer before I had even finished! I thought it was a bug, but then I foolishly realized that Haskell’s laziness (I think?) was taking it upon itself to decide that, after I entered 1, 2, 3, 0, no matter what came after, the result would be Nothing, and so it happily outputted that.

Unfortunately, when I changed

let readings = map (read :: (Read a, Num a) => String -> a) $ lines input

to

let readings = parse $ lines input

with parse being a safer method of reading numerical input, implemented as

maybeRead :: (Read a) => String -> Maybe a
maybeRead = fmap fst . listToMaybe . filter (null . dropWhile isSpace . snd) . reads

parse :: (Read a) => [String] -> [a]
parse xs =
    let entries = map maybeRead xs
    in if all isJust entries
        then map fromJust entries
        else []

it no longer does this.

Why?

EDIT: More code

-- | Zip together adjacent list elements as pairs in a new list.
zipPairs :: [a] -> [(a, a)]
zipPairs xs = zip (init xs) (tail xs)

-- | Return True if all elements of a given list are equal.
constant :: (Eq a) => [a] -> Bool
constant xs = all (== head xs) (tail xs)

-- | Return the order that the elements of a list are sorted in, if they form
-- a strictly increasing (Just LT), decreasing (Just GT) or constant (Just EQ)
-- sequence. If there is no pattern, return Nothing.
order :: (Ord a) => [a] -> Maybe Ordering
order xs =
    let orders = map (\(x, y) -> x `compare` y) (zipPairs xs)
    in if constant orders then Just (head orders) else Nothing

and then in mainI have

let readings = parse $ lines input
putStrLn $ if null readings
    then "bad input"
    else case order readings of
        Just EQ -> "Constant"
        Just LT -> "Diving"
        Just GT -> "Rising"
        Nothing -> "Nothing"
  • 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-30T18:43:27+00:00Added an answer on May 30, 2026 at 6:43 pm

    If all entries are justs, all isJust entries checks the entire list of entries, which means that the entire list of entries needs to be read in before parse can return.

    Okay, longer explanation of why orders is lazy — all returns False as soon as it reaches a value for which the predicate returns False. Therefore, constant returns false as soon as it hits a value in the tail that isn’t equal to the head. order returns as soon as constant returns, so order is lazy.

    My first suggestion is stylistic — look at the zipWith function when calculating orders. let orders = zipWith compare xs $ tail xs should work equally well.

    As far as solving your actual problem is concerned, try

    order xs = let orders = zipWith (liftM2 compare) xs $ tail xs
               in if isJust (head orders) && constant orders
                  then head orders
                  else Nothing
    

    Note that you need to import Data.Monad

    liftM2 compare will return Just (compare x y) when passed Just x and Just y and Nothing if either or both of its arguments are Nothing.

    orders is now a [Maybe Ordering]. If orders is constant (note: (==) works on Maybes) and the first element is a Just, return the first element (which is already a Maybe Ordering). Otherwise, just return Nothing. You could do without the isJust (head orders) call, but adding it should make it return as soon as it sees a Nothing (otherwise, if you give it a list of all Nothings, it will check if every one is Nothing).

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

Sidebar

Related Questions

Say I have simple program that emulates a board game with a number of
I have a simple program that creates a thread, loops twenty times and then
I have a simple python program that I'd like to daemonize. Since the point
I have a simple forms program that I have been fighting with for a
I have a simple Delphi program that I'm working on, in which I am
I have a simple Tray icon program that opens a site using System.Diagnostics.Process.Start(URL) And
hey there, just practising and I had a question. I have a program (source
I'm new to Java so I have a simple question that I don't know
I have a simple program to check if a port is open, but I
I have a simple web.py program to load data. In the server I don't

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.