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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 12, 20262026-06-12T22:54:12+00:00 2026-06-12T22:54:12+00:00

I try to write custom function which take an array and iterate it, i

  • 0

I try to write custom function which take an array and iterate it, i need to print every digit and recursive launch the same function but i get error

parse error on input `main’

code:

firstitem n = if n /= [] n firstitem( tail n )


main = do
    print(firstitem [1,2,3])
  • 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-12T22:54:14+00:00Added an answer on June 12, 2026 at 10:54 pm

    Your first problem is that you have the wrong syntax for an if statement. You should write

    firstItem n = if {- condition-}
        then {- do this -}
        else {- do that -}
    

    Second, it’s not clear what the function firstItem is supposed to do. It sounds like it’s supposed to return the first item of a list, but your description makes it seem as though you want to iterate over all elements of the list.

    You can combine the iteration and printing into a single function like this:

    printAll xs = if null xs        -- If the list is empty
        then return ()              -- then we're done, so we quit.
        else do print (head xs)     -- Otherwise, print the first element
                printAll (tail xs)  -- then print all the other elements.
    
    main = printAll [1,2,3]
    

    The function null returns True if the list is empty, otherwise it returns False. The statement return () you can think of as saying “there’s nothing more to process, so stop the function now and return no result”. The third and fourth lines contain a “do block”. The do block is basically a bit of glue that binds the two actions print (head xs) and printAll (tail xs) together into one action. You can write it with curly braces to make it a bit clearer:

    do {
      print (head xs);
      printAll (tail xs)
    }
    

    although you don’t actually need them – the indentation specifies what you mean.

    This solves your problem, but it’s a bit clunky. Haskell is supposed to be a beautiful language, after all – so let’s make your code beautiful! It would be better to use pattern matching to pull the list apart into its head and tail:

    printAll []     = return ()
    printAll (x:xs) = do print x
                         printAll xs
    

    That’s a lot better. But it could be more modular. Hey, maybe we could make a generic function that takes an action as input, and does that action for every element of the list:

    repeatedly f []     = return ()
    repeatedly f (x:xs) = do f x
                             repeatedly f xs
    
    printAll xs = repeatedly print xs
    

    That’s neat. But actually, there’s already a function that does this. It’s called mapM_ (there is a good reason for it being called this, but I won’t get into it now) and it’s in the Control.Monad module:

    import Control.Monad
    
    printAll xs = mapM_ print xs
    

    Actually, you can leave off the xs argument, and the compiler can infer that it’s supposed to be there:

    printAll = mapM_ print
    

    Finally, your code is beautiful. Hope that helps a bit.

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

Sidebar

Related Questions

I try to write a javascript Self-Executing Anonymous Function window.App = window.App || {}
i try to write a custom list-binding. This is what i have so far:
try to write a composite component that allows mutltiple text inputs. I read that
I try to write Activator action for changing current song rating. I now that
I try to write a Facebook app. This app will communicate with a rfid
I try to write DSL class Warcraft class << self def config unless @instance
I try to write a macro in clojure that sets up a namespace and
I try to write a simple user script to enlarge the picture when you
I try to write the following in latex: \begin{itemize} \item \textbf{insert(element|text)} inserts the element
I try to write :ab in Vim for faster coding but the question is

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.