adambard / learnxinyminutes-docs

Code documentation written as code! How novel and totally my idea!
https://learnxinyminutes.com/
Other
11.54k stars 3.36k forks source link

Gripes about Haskell #45

Closed adambard closed 11 years ago

adambard commented 11 years ago

From reddit (http://www.reddit.com/r/programming/comments/1h917l/learn_x_in_y_minutes/casl5so?context=1)

""" There seems to be one for haskell now http://learnxinyminutes.com/docs/haskell/ which I am reading.

-- indexing into a list
[0..] !! 5 -- 4

gives 5 instead of 4

map func [x] = [func x]
map func (x:xs) = func x:(map func xs)

fails to load because of "Ambiguous occurence" of map

(even (double 7))

does not work (maybe because "double" is undefined?)

not :: Bool -> Bool

should probably be a comment?

Then there is some stuff where I don't even have a clue what's wrong.

How to write functions is mentioned at the end instead of at the occurence of the first function.

The book linked at the bottom of the page seems to be good so far (and I should rather have read that instead of messing with this code).

Haskell does have more specialized "looping" constructs, including map, repeat and replicate for pure constructs, their monadic counterparts mapM, replicateM, repeatM and their xxx_ brethren, and things like forever; implementing monadic for and while loops is pretty easy, e.g.:

while :: Monad m => m Bool -> m () -> m () while condition action = do v <- condition if v then (action >> while condition action) else return () -- using in, for example, IO: exitCond <- newIORef False while (readIORef     exitCond) $ do ln <- getLine case ln of "" -> writeIORef exitCond True "foo" -> putStrLn "You said 'foo'!" "bar" -> putStrLn "So a foo walks into a bar..." x -> putStrLn ("Hmm, I don't think I know what '" ++ x ++ "' means...") 

You can actually use it in any monad, but it makes the most sense in -- "stateful" monads like IO, ST, State, and the like.

adambard commented 11 years ago

I'm not really qualified to do anything about this, so I'll just mention @egonSchiele here.

egonSchiele commented 11 years ago

I'll take a look.