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

Also, among statically typed languages there is a significant difference between languages which make the programmer write out all the types and languages which offer type inference. I think most of the comments about types "getting in the way" come from experience with the former (C, C++, Java, C#). In a strongly- and statically-typed language with type inference (such as Haskell) even moderately complex programs can be written without any type annotations. The result looks a lot like a dynamically-typed program, but static types are still inferred and checked for consistency at compile-time. For example, this is a perfectly valid Haskell program:

    import Control.Monad
    import Control.Monad.Tardis
    import System.Random
    
    -- A solution for the Trapping Rain Water problem employing
    -- the bidirectional state ("Tardis") monad.
    -- https://www.geeksforgeeks.org/trapping-rain-water/
    volume hs = sum . flip evalTardis (0, 0) . flip traverse hs $ \h -> do
       x <- min <$> getPast <*> getFuture
       modifyForwards  (max h)
       modifyBackwards (max h)
       pure (max 0 (x - h)) 
       
    main = replicateM_ 20 $ do
       n  <- randomRIO (3, 10) 
       hs <- replicateM n $ randomRIO (0, 10 :: Integer)
       putStrLn $ show hs ++ " => " ++ show (volume hs) 
Note that the only explicit type in the entire program is the `:: Integer` annotation on the upper bound for randomRIO. Without that annotation the program would be underconstrained, since the input to `volume` can a list of any type with Ord and Num instances.


Guidelines | FAQ | Lists | API | Security | Legal | Apply to YC | Contact

Search: