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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 27, 20262026-05-27T23:28:11+00:00 2026-05-27T23:28:11+00:00

I should write function that takes list, element and return positions in with such

  • 0

I should write function that takes list, element and return positions in with such element occurs. Like,

pos 2 [1, 2, 3, 2] -> [2, 4]
pos 1 [1, 2, 3, 2] -> [1]
pos 8 [1, 2, 3, 2] -> []

This what I’ve done.

--findFirstPosition :: Eq a => a -> [a] -> Maybe a
findFirstPosition val xs = case f of 
    Nothing -> Nothing 
    Just (v, i) -> Just(i)
  where f = (find (\ (v, i) -> val == v) (zip xs [1..]))

--pos :: Eq a => a -> [a] -> [Int]
pos _ [] = []
pos val xs = if (finded) 
    then concat[
           [fromJust res],
            map (\a -> a + (fromJust res)) 
            (pos val (drop  (fromJust res) xs))]
    else []  
  where 
    res = findFirstPosition val xs
    finded = (isJust res)

It’s works quite good. But when I’m trying to use functions type (like shown in comments) error occurs

Could not deduce (a ~ Int)
from the context (Eq a)
  bound by the type signature for pos :: Eq a => a -> [a] -> [Int]
  at \test.hs:(63,1)-(72,29)
  `a' is a rigid type variable bound by
      the type signature for pos :: Eq a => a -> [a] -> [Int]
      at \test.hs:63:1
Expected type: Maybe Int
  Actual type: Maybe a
In the first argument of `fromJust', namely `res'
In the first argument of `drop', namely `(fromJust res)'

How should I deal with it? Also any additional code review comments are highly appreciated.

Upd
I should implement it using find function.

  • 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-27T23:28:12+00:00Added an answer on May 27, 2026 at 11:28 pm

    The type of findFirstPosition should be

    findFirstPosition :: Eq a => a -> [a] -> Maybe Int
    

    The purpose of this function is to find a position, or index. So the return type should wrap something appropriate for indexing, but independent of the argument type(s).

    Unrelated: are you sure that indexing should start with 1? Usual is 0-based indexing.

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

Sidebar

Related Questions

I'd like to write a function that takes both a value constructor for a
I am trying to write a list function that takes a simple list and
I need to write a Scheme function that takes a list as an argument
I want to write a function that takes an array of letters as an
I need to write a function that takes 4 bytes as input, performs a
I want to write a function in lisp that takes two lists of pairs
I have to write a library which contains a function that takes two strings
I'm trying to write a haskell function that takes in two lists of integers
I have been looking for an elegant way to write a function that takes
Can PHP dissect its own syntax? For example, I'd like to write a function

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.