I’m trying to translate a radix sort algorithm from Java to Haskell, and I’m really new to Haskell. I’ve been reading tutorials, but they are so lengthy and I’m trying to get my hands dirty now.
I’ll be passing a list of lists to the sort algorithm, and my questions are:
- How would I traverse all elements in a list?
- How would I access a particular element within a list?
So for instance, I’d need to traverse over every list I have, and access particular elements by index (?) in that particular list. How!?
Since you tagged your question as homework, I’m going to give you a few pointers but no solution. A Haskell book that seems to be particularly beginner friendly is “Learn You a Haskell for Great Good!” (LYAH). You can buy it or read it online for free.
Traversing a list requires recursion. See Chapter 5 of LYAH.
For accessing a list element by index you can use the
!!operator. Note, though, that if you give it an invalid index you get an error. Indexing starts at 0. For a few more standard utility functions on lists, see Chapter 2 of LYAH.