tag:blogger.com,1999:blog-1777990983847811806.post5956883708154306867..comments2024-03-16T16:29:29.582-07:00Comments on Haskell for all: Haskell for C Programmers - For LoopsGabriella Gonzalezhttp://www.blogger.com/profile/01917800488530923694noreply@blogger.comBlogger6125tag:blogger.com,1999:blog-1777990983847811806.post-88698621775909356542018-05-07T10:50:03.168-07:002018-05-07T10:50:03.168-07:00It took some time for me to realize that this exam...It took some time for me to realize that this examples don't work anymore. Probably because of some old version of lens. Here is my attemp to rewrite them (this is one of my first experiences of using lenses ;) ):<br /><br />i :: Lens' Status Int<br />i = lens _i (\s x -> s { _i = x } )<br /><br />result :: Lens' Status Double<br />result = lens _result (\s x -> s { _result = x } ) <br /><br />-- worst variant (have to use length and index variables)<br />sum' :: [Double] -> Int -> Double<br />sum' xs n = flip evalState def $ do<br /> result .= 0<br /> for (i .= 0) (liftM (< n) (use i)) (i += 1) $ do<br /> i' <- use i<br /> result += (xs !! i')<br /> use result <br /><br />-- without length and index<br />sum'' :: [Double] -> Double<br />sum'' xs = flip evalState def $ do<br /> forM xs (result +=)<br /> use result<br /><br />-- using foldM<br />sum''' :: [Double] -> Double<br />sum''' = flip evalState (Status 0 0.0) . foldM ((return .) . (+)) 0.0Anonymoushttps://www.blogger.com/profile/07454713957381317269noreply@blogger.comtag:blogger.com,1999:blog-1777990983847811806.post-86915610768629932252018-05-07T09:35:15.645-07:002018-05-07T09:35:15.645-07:00This comment has been removed by the author.Anonymoushttps://www.blogger.com/profile/07454713957381317269noreply@blogger.comtag:blogger.com,1999:blog-1777990983847811806.post-88239158337347734922018-05-07T09:24:31.141-07:002018-05-07T09:24:31.141-07:00This comment has been removed by the author.Anonymoushttps://www.blogger.com/profile/07454713957381317269noreply@blogger.comtag:blogger.com,1999:blog-1777990983847811806.post-68361897316271867072013-01-20T16:14:28.389-08:002013-01-20T16:14:28.389-08:00You'd be right if you had made that remark a f...You'd be right if you had made that remark a few years ago, back when Haskell was essentially playing catch-up with imperative languages. Things are different now and Haskell is slowly but surely beating imperative languages at their own game. I can think of at least four killer Haskell features off of the top of my head where Haskell is best-in-class, many of which are virtually inexistent in mainstream languages:<br /><br />* Software Transactional Memory<br />* Modern Lenses (i.e. the `lens` package)<br />* Coroutines (Shameless plug: including my `pipes` library)<br />* Parser combinators<br /><br />All of those are pretty self-explanatory except for "Lenses", which are very poorly documented, so here's a lightning lens example to give you a taste of their power and expressiveness:<br /><br />>>> import Control.Lens<br />>>> sumOf both (1, 3)<br />4<br />>>> sumOf (folded . folded) [[1, 2], [3, 4]]<br />10<br />>>> forMOf_ (folded . _1) [('A', 1), ('B, 2)] $ \c -> print c<br />'A'<br />'B'<br />>>> set (_1 . mapped) 7 ([1, 2, 3], 'A')<br />([7,7,7],'A')<br /><br />I think you would have difficulty writing anything even remotely as powerful as that in an imperative language.<br /><br />Now combine that with a type system that makes programs immensely more bug-free and maintainable. Sure, you can translate a Haskell program back to an imperative one, but you'll wish you hadn't the moment your client requests new features or for you to rearchitect your code, both of which are significantly easier in Haskell.<br /><br />Haskell's strong and expressive type system acts like a silent guardian protecting you from your mistakes, so you feel much braver about adding new features or re-engineering large swaths of code, even more so than if you had constructed a very large and comprehensive test suite in another language. This alone is the greatest contributor to productivity in Haskell, since you spend less time writing tests and fixing bugs and more time actually implementing features, the way programming was meant to be.<br /><br />However, I want to caution you that pure functions are not the solution to everything. Haskell is not an excellent programming language because everything is done purely and functionally. Rather, Haskell is an excellent language because functional programming is the perfect foundation for rigorously building more sophisticated abstractions. Imperative languages can't do this because they can't opt-out of state and side effects, making it incredibly difficult to reason about the correctness of abstractions, whereas Haskell gets to opt-in whenever it wants.<br /><br />If you haven't read my category design pattern post then please do, because there I argue that the key to powerful, easy, and reliable programming is not just functions, but rather categories in general. Of the four killer features I mentioned above, only one actually uses the category of Haskell functions (lenses). I consider the overemphasis on pure functional style to be the biggest cultural weakness of the Haskell community.Gabriella Gonzalezhttps://www.blogger.com/profile/01917800488530923694noreply@blogger.comtag:blogger.com,1999:blog-1777990983847811806.post-30138174611871408572013-01-20T14:27:04.312-08:002013-01-20T14:27:04.312-08:00This is a big problem of Haskell. Unless you compl...This is a big problem of Haskell. Unless you completely scrap everything you know, and learn EVERYTHING in haskell from examples. Only THEN can you look at your old code, spend a few dozen of hours figuring out what the program has to do (as it was initially designed in a testable imperative way, and haskell does not support imperative constructs), then you can, using the big dictionary of expressions defined in Haskell, write your program in a way which makes sense in terms of functional programming. After it works, you can rewrite it back as imperative program (this transition is easier) while knowing that it does exactly what you want it to do, without any distractions arising from data-loss during optimization step which sacrifices expressiveness for efficiency.<br /><br />In other words, it's a pain.Dmitryhttps://www.blogger.com/profile/16188564767213654951noreply@blogger.comtag:blogger.com,1999:blog-1777990983847811806.post-78969446938510832412012-05-17T21:13:24.000-07:002012-05-17T21:13:24.000-07:00For a newcomer to Haskell, this blog is not easy. ...For a newcomer to Haskell, this blog is not easy. Without introducing type system of haskell everything seems hazy. May be if you could write a blog to mark the correct order of studying your other blogs then it could be immensely helpful.<br />Thanks for your efforts!Niket Kumarhttps://www.blogger.com/profile/13052161814273329727noreply@blogger.com