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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 11, 20262026-05-11T19:50:42+00:00 2026-05-11T19:50:42+00:00

I wrote the code to get the element index elemIndex :: [String] -> [String]

  • 0

I wrote the code to get the element index

elemIndex :: [String] -> [String] -> [Int]
elemIndex [] [] = []
elemIndex x y = elemIndex True [(elem a y) | a <- x ]

is there any alternative way/similer of performing the above logic?

and also i’ve seen that some use

index [] _ = []

to return null lists

could you pls explain the use of underscore?

//edit 1
it is suposed to return the index of the values in the list.

eg: elemIndex [“asde”,”zxc”,”qwe”] [“qwe”,”zxc”]

returns [1,2] as the answer

thanks

  • 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-11T19:50:43+00:00Added an answer on May 11, 2026 at 7:50 pm

    elemIndex takes two arguments (two lists). Right now you recursively call it with an additional argument of type bool (namely True). That will not work. What you probably want to do, is create a helper function, as I showed you an hour ago.

    The _, as used as a formal argument, matches any input. It does not have a name, and as such, you cannot use that which is matched.

    Aside from that, you probably don’t want to use booleans, but integers (to keep track of a counter). The elem function only tells you whether some value is part of a list, not where it is. So, it is of little use to you. Since this appears to be homework I will not provide a solution to your problem, but perhaps you should split your code in two:

    indices :: (Eq t) => [t] -> [t] -> [Integer]
    getIndex :: (Eq t) => [t] -> t -> Integer
    

    (getIndex may use a helper function getIndex' :: (Eq t) => [t] -> t -> Integer -> Integer.)


    Edit: One possible solution (which uses a hack, it’s nicer to use the Maybe monad):

    indices :: (Eq t) => [t] -> [t] -> [Integer]
    indices xs ys = filter (>= 0) $ map (getIndex xs) ys
    
    getIndex :: (Eq t) => [t] -> t -> Integer
    getIndex xs y = getIndex' xs y 0
      where
        getIndex' :: (Eq t) => [t] -> t -> Integer -> Integer
        getIndex' [] _ _                 = -1
        getIndex' (x:xs) y i | x == y    = i
                             | otherwise = getIndex' xs y (i + 1)
    

    A version with the Maybe monad:

    import Data.Maybe
    
    indices :: (Eq t) => [t] -> [t] -> [Integer]
    indices xs ys = mapMaybe (getIndex xs) ys
    
    getIndex :: (Eq t) => [t] -> t -> Maybe Integer
    getIndex xs y = getIndex' xs y 0
      where
        getIndex' :: (Eq t) => [t] -> t -> Integer -> Maybe Integer
        getIndex' [] _ _                 = Nothing
        getIndex' (x:xs) y i | x == y    = Just i
                             | otherwise = getIndex' xs y (i + 1)
    

    And a version that leaves all heavy lifting to the standard library:

    import Data.List
    import Data.Maybe
    
    indices :: (Eq t) => [t] -> [t] -> [Int]
    indices xs ys = mapMaybe (`elemIndex` xs) ys
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Ask A Question

Stats

  • Questions 119k
  • Answers 119k
  • Best Answers 0
  • User 1
  • Popular
  • Answers
  • Editorial Team

    How to approach applying for a job at a company ...

    • 7 Answers
  • Editorial Team

    How to handle personal stress caused by utterly incompetent and ...

    • 5 Answers
  • Editorial Team

    What is a programmer’s life like?

    • 5 Answers
  • Editorial Team
    Editorial Team added an answer Actually, what I'd suggest in this case is Core Animation… May 11, 2026 at 11:46 pm
  • Editorial Team
    Editorial Team added an answer Yes, use the -nosplash option. Place this: <argument>-nosplash</argument> in your… May 11, 2026 at 11:46 pm
  • Editorial Team
    Editorial Team added an answer Could it be that you are rotating around the Y… May 11, 2026 at 11:46 pm

Related Questions

I'm trying to write some code to find a specific XmlNode object based on
1) On a 32-bit CPU is it faster to acccess an array of 32
Can anyone tell me why the following code doesn't work the way I expect?
So, I wrote a bunch of code that accesses elements in an stl vector

Trending Tags

analytics british company computer developers django employee employer english facebook french google interview javascript language life php programmer programs salary

Top Members

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.