Haskell - Shuffling a list with different types?

I am very new to Haskell, I started studying Haskell a few days ago for the course that I have. This course has an assignment, or homework that I am stuck at the moment. I would be grateful for the advice and explanations of my problem for more complete, direct answers, since this is homework, and therefore I do not want to take a loan for another job.

The purpose is to write a shuffle function ( skyffla). Actual shuffling is not what I need to understand, but rather one of the tests that the algorithm must pass. I mark the shuffle algorithm with parentheses to make it less important.

( skyfflamoves by taking the first element, third element, fifth element, etc., until all the odd indices have been accepted, and then add them to the list.

The remaining elements in the first list should then be processed in the same way and added to the list that was first processed. And further and further, until there is only one element left, which will then be added to the list.

If I, for example, input:

skyffla [1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12]

he will give me:

1: _[1, 3, 5, 7, 9, 11] ++ skyffla ([2, 4, 6, 8, 10, 12])_
2: _[1, 3, 5, 7, 9, 11] ++ [2, 6, 10] ++ skyffla ([4, 8, 12])_
3: _[1, 3, 5, 7, 9, 11, 2, 6, 10] ++ [4, 12] ++ skyffla([8])_ 
(4): _[1, 3, 5, 7, 9, 11, 2, 6, 10, 4, 12, 8]_

This is exactly what my code does, and the actual shuffle works the way it is intended. But, as I mentioned, there is one test that I cannot pass with my current implementation.)

So, one of the tests is that skyffla should take: [3.4, 2.3, 5, 185, 23]and the return statement: _[3.4, 5, 23, 2.3, 185]_.

With the code that I have, I get: _[3.4, 5.0, 23.0, 2.3, 185.0]_

, ".0", 5, 185 23.

?

, "5,0" "5"?

:

skyffla :: [a] -> [a]
skyffla [] = []
skyffla xs
    | length xs == 1 = xs
    | otherwise = dropUnevenElements xs ++ skyffla newList  where
    newList = takeUnevenElements xs

dropUnevenElements :: [a] -> [a]
dropUnevenElements [] = []
dropUnevenElements (x:y:xs) = x:dropUnevenElements xs
dropUnevenElements x = x


takeUnevenElements :: [a] -> [a]
takeUnevenElements [] = []
takeUnevenElements (x:y:xs) = y:takeUnevenElements xs 
takeUnevenElements x = []
+4
2

: .

, ,

ghci> [3.4, 2.3, 5, 185, 23]
[3.4,2.3,5.0,185.0,23.0]

Haskell get-go. ( 5 fromInteger 5, , )

, . , .

ghci> skyffla [3.4, 2.3, 5, 185, 23] == [3.4, 5, 23, 2.3, 185]
True

, . Data.Decimal, . , , , , .

+4

, Decimal.

λ> import Data.Decimal

λ> [3.4, 5, 23, 2.3, 185] :: [Decimal]
[3.4,5,23,2.3,185]
0

Source: https://habr.com/ru/post/1684953/


All Articles