Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Next element in a list

I have a function in Haskell which returns me the successor of a card. I know how to access the card, using list comprehension, but not quite sure how to get the next card, so pos+1. Here is my code:

pCard :: Card->Deck->Card
pCard (K,C) _ = (A,S) --exception for last card
pCard crd pck = head [p | (pos,p) <- (zip [0..51] pck), crd==p]
like image 440
Vlad Balanescu Avatar asked May 29 '26 08:05

Vlad Balanescu


1 Answers

Hint: use dropWhile (/= crd) pck to remove all the cards from the beginning until your card is found. Then, the first card remaining is crd. The one after that is the one you want.

Still, if pck is a deck in a standard order, looping over the whole deck to find the next card seems to be quite inefficient.

like image 198
chi Avatar answered May 31 '26 09:05

chi



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!