Hacker Newsnew | comments | show | ask | jobs | submitlogin

>Now, you may think that implies that you could do something like this to print a string one character at a time:

I'm not sure how far you got after this point, but the problem is easily solved with a higher-order function:

    mapM_ print "blahblahblah"
Or if you don't like each character being quoted:

    mapM_ (putStrLn . return) "hello one at a time"
Or did you not want the characters on their own lines? In that case, you can do this:

    mapM_ putChar "horse raddish"
But why bother doing that when you can simply do this?:

    putStr "egg freckles"
>But what happens when, as is frequently the case, someone wants to retrieve the nth element of a list?

If you are frequently wanting to do this, you are probably doing something wrong. Haskell's lists are intended to be used as control structures, not indexed data structures. For those, you'll want an Array, Vector, IntMap etc.




Applications are open for YC Summer 2015

Guidelines | FAQ | Support | Lists | Bookmarklet | DMCA | Y Combinator | Apply | Contact

Search: