I'm new with Haskell. I know that Hugs is old and is not supported anymore but my university still uses it.
Here is my code:
gio n = jako n ++ tazo n
jako [] = []
jako (a:b) = if a `mod` 2 == 1 then a:jako (tail b) else jako (tail b)
tazo [] = []
tazo (a:b) = if a `mod` 2 == 0 then a:tazo (tail b) else tazo (tail b)
What I'm trying to do is take an list, and return odd list united with even list. For example gio [1,2,3,4,5,6,7,8,9] = [1,3,5,7,9,2,4,6,8]
But in my case it returns Program error: {tail []}
or if the original lists length is even it returns only odd part list. For example: gio [1,2,3,4,5,6,7,8] = [1,3,5,7]
it seems like when jako n
is called it changes the list, it passes the list with reference or something like that. What can be changed to achieve the goal? P.S I don't need some library function that would do this, It needs to be done by using my functions. thanks.
ctrl-k
to format code blocks. And always add type signatures for any Haskell functions you're asking about, this makes it so much easier to see what's going on (both for us and for you!). – leftaroundabouttail b
and usingb
directly? This is becauseb
already is the tail of the input. – chiodd :: Integral a => a -> Bool
andeven :: Integral a => a -> Bool
already – wizzup