I want to erase the nth number from the list.

Asked 2 years ago, Updated 2 years ago, 276 views

I'm using Haskell to write a code that removes the nth number from the list as I wrote in the title, but it doesn't work.

Example)

dropvery[0, 1, 2, 3, 4, 5, 6, 7, 8, 9]3
[0,1,3,4,6,7,9] 

* This is the code where 2, 5, 8 and the third number are dropped from the list and the remaining numbers are displayed.

I'm trying various things now, but it doesn't work very well.
The code below is written after trying various things and looking at various sites.There are still some parts that need coding, but I don't know what to write (?)

If anyone has a solution, I would appreciate it if you could answer it.Also, I would appreciate it if you could let me know if there are any corrections.
Thank you for your cooperation.

Current Code

dropvery::[a]->Int->[a]
dropvery[]_=[]
dropvery xs n = go 0xs
    where go_[] =[]
          goi(y:ys)|i==n=0(?)
                      | Otherwise=?
                                                                    
main = putStrLn (show(dropvery) [0, 1, 2, 3, 4, 5, 6, 7, 8, 9] 3)

haskell

2022-10-08 01:00

2 Answers

How about this one:

dropvery::[a]->Int->[a]
dropvery xs n = go xs
 where
  go xs' =
    let(xsNotDropped, xsLeft) = splitAt(n-1)xs'
     in
      case xsLeft of
          [ ] - > xsNotDropped
          (_:xsAfterN) - >
            xsNotDropped++goxsAfterN


main = putStrLn (show (dropvery [0, 1, 2, 3, 4, 5, 6, 7, 8, 9]) 3))

Supplemental Information:

  • I'm afraid if you're doing it for study purposes, but I recommend that you use the function in the standard library (splitAt in the above case) without writing recursive as much as possible.
  • Unlike many other programming languages, it is recommended that functions manufactured by Haskell be followed by "primarily processed arguments."This is because it is more compatible with curries (please ask additional questions if you are interested in more details!), so I think it is better to use the format dropvery::Int->[a]->[a].


2022-10-08 01:00

If you notice the last step in the calculation of the missing part of the first n element, you can simply add it to the list of the first (n-1) elements.

(1) Take (n-1) pieces from the beginning
(2) Throw away the remaining heads
(3) Apply dropEveryn to the rest of it
(4) Connect (1) and (3)

dropEvery::Int->[a]->[a]
dropEvery_[] = [ ]
dropEverynxs=take(n-1)xs++dropEveryn(dropnxs)

Also
(1)Rip (n-1) pieces from the beginning
(2)Throw away the remaining heads
You can also repeat the section to get a list of lists of length n-1 or less first and concatenate them with concat.
This technique is abstracted with a standard function called unfoldr::(a->Maybe(b,a))->a->[b] (located in the Data.List module) such as "Twist the rice cake little by little and roll it up to make a lot of yellow powder."

dropEvery::Int->[a]->[a]
dropEveryn=concat.unfoldrf
    where
        f[ ] = Nothing
        fxs = Just(take(n-1)xs, drop nxs)


2022-10-08 01:00

If you have any answers or tips


© 2024 OneMinuteCode. All rights reserved.