tag:blogger.com,1999:blog-1777990983847811806.post6449788940161894777..comments2024-03-16T16:29:29.582-07:00Comments on Haskell for all: Statements vs ExpressionsGabriella Gonzalezhttp://www.blogger.com/profile/01917800488530923694noreply@blogger.comBlogger19125tag:blogger.com,1999:blog-1777990983847811806.post-74936001811077131962020-10-15T15:51:30.153-07:002020-10-15T15:51:30.153-07:00You can actually use Maybe monad that was introduc...You can actually use Maybe monad that was introduced in this article exactly for that. For example like this (I have to use underscores instead of spaces because of automatic formatting):<br /><br />data Tree a = Nil | Node (Tree a) a (Tree a)<br /><br />treeProductMaybe :: (Eq a, Num a) => Tree a -> Maybe a<br />treeProductMaybe Nil = Just 1<br />treeProductMaybe (Node left value right)<br />____| value == 0 = Nothing<br />____| otherwise = do<br />________leftProduct <- treeProductMaybe left<br />________rightProduct <- treeProductMaybe right<br />________return (leftProduct * value * rightProduct)<br /><br />treeProduct :: (Eq a, Num a) => Tree a -> a<br />treeProduct tree = case treeProductMaybe tree of<br />____Nothing -> 0<br />____Just x -> x<br />Anonymoushttps://www.blogger.com/profile/00580795669570219614noreply@blogger.comtag:blogger.com,1999:blog-1777990983847811806.post-26266665135346283502016-11-26T06:32:31.195-08:002016-11-26T06:32:31.195-08:00This comment has been removed by the author.aliyaahttps://www.blogger.com/profile/06184256288293330921noreply@blogger.comtag:blogger.com,1999:blog-1777990983847811806.post-44477409162693376722016-03-24T05:29:17.325-07:002016-03-24T05:29:17.325-07:00ThanksThanksAliesen Senadohttps://www.blogger.com/profile/16425740465165974813noreply@blogger.comtag:blogger.com,1999:blog-1777990983847811806.post-1258571816238337212015-01-23T20:25:24.140-08:002015-01-23T20:25:24.140-08:00Thank you for share this informative post.Thank you for share this informative post.Anonymoushttps://www.blogger.com/profile/05210877218282157153noreply@blogger.comtag:blogger.com,1999:blog-1777990983847811806.post-9725362099082057832013-07-21T14:15:37.017-07:002013-07-21T14:15:37.017-07:00Yes, you're right. Maybe a better way I could...Yes, you're right. Maybe a better way I could have phrased it is that operational semantics and denotational semantics overlap in the same way that statements and expressions overlap in Haskell.Gabriella Gonzalezhttps://www.blogger.com/profile/01917800488530923694noreply@blogger.comtag:blogger.com,1999:blog-1777990983847811806.post-79188091397022227672013-07-21T13:58:13.728-07:002013-07-21T13:58:13.728-07:00Oops, looks like Blogger has jammed the formatting...Oops, looks like Blogger has jammed the formatting. Here's the rule in monofont: http://lpaste.net/8226532637376774144Anonymousnoreply@blogger.comtag:blogger.com,1999:blog-1777990983847811806.post-74753360038300371572013-07-21T13:55:33.045-07:002013-07-21T13:55:33.045-07:00Nice post, but I don't agree with your analogy...Nice post, but I don't agree with your analogy/distinction between operational semantics vs denotational semantics. For example, I don't think that this rule from operational semantics of simply typed lambda-calculus has anything to do with statements or imperative programming:<br /><br />e1 => (\a -> e2) e3 => v e2[v/a] => v2<br />-----------------------------------------------<br /> (e1 e3) => v2<br /><br />It merely says how can we evaluate an _expression_ from subexpressions. Of course, operational semantics for imperative languages involve some sort of abstract machinery most of the time. But equally you can build denotational semantics for imperative languages: you can interpret statements as functions on the memory stores and then ';' operator as composition of functions.<br /><br />Thanks.<br /><br />- DanAnonymousnoreply@blogger.comtag:blogger.com,1999:blog-1777990983847811806.post-80683378500859779572013-07-15T19:55:18.891-07:002013-07-15T19:55:18.891-07:00I don't know how WordPress works, but Blogger ...I don't know how WordPress works, but Blogger provides really nice statistics that you can use to track how many people visit your site and where they come from. This helps you know how broad your readership is and I also use these kinds of statistics to hone my writing by studying which of my posts are well received and get reshared frequently.<br /><br />Also, when you first start out it helps to pick a specific social network that is appropriate for your blog and submit posts there a few times. Having a thick skin helps, too. :) For example, I began by tailoring my posts to /r/Haskell and regularly submitting there while listening to their suggestions and criticisms (mostly criticisms) in the comments to improve the quality of my posts. That's how I slowly grew a reader base and developed a more personal connection with people who read my blog.<br /><br />Once you build an active and personal connection with your readers like that it greatly improves the joy of blogging and then you don't feel like you are talking into an empty void. Instead it feels like chatting in public with friends.Gabriella Gonzalezhttps://www.blogger.com/profile/01917800488530923694noreply@blogger.comtag:blogger.com,1999:blog-1777990983847811806.post-6746372172439851662013-07-15T18:36:26.124-07:002013-07-15T18:36:26.124-07:00Vindicating to hear you saw my post. Sometimes it ...Vindicating to hear you saw my post. Sometimes it feels like blogging into the void.<br /><br />Reading yours I felt like I should have gone deeper. Thanks for doing the thorough treatment. Richard Minerichhttps://www.blogger.com/profile/06258455990592976757noreply@blogger.comtag:blogger.com,1999:blog-1777990983847811806.post-89089659195397394232013-07-15T18:33:49.757-07:002013-07-15T18:33:49.757-07:00This comment has been removed by the author.Richard Minerichhttps://www.blogger.com/profile/06258455990592976757noreply@blogger.comtag:blogger.com,1999:blog-1777990983847811806.post-2153291037100361112013-07-15T17:17:57.595-07:002013-07-15T17:17:57.595-07:00Yeah, I really like your post. It does a really g...Yeah, I really like your post. It does a really good job illustrating how common imperative features are not first-class in the sense that they do not have a value associated with them.Gabriella Gonzalezhttps://www.blogger.com/profile/01917800488530923694noreply@blogger.comtag:blogger.com,1999:blog-1777990983847811806.post-28389132091853624232013-07-15T17:15:58.907-07:002013-07-15T17:15:58.907-07:00Great post. I wrote about statements vs expression...Great post. I wrote about statements vs expressions in a blog post a while back too, but I think your's was much more illustrative. If you're interested in an F# vs C# perspective on this you can find my post here: <br />http://richardminerich.com/2012/07/functional-programming-is-dead-long-live-expression-oriented-programming/Richard Minerichhttps://www.blogger.com/profile/06258455990592976757noreply@blogger.comtag:blogger.com,1999:blog-1777990983847811806.post-7779749291535523182013-07-15T08:38:00.707-07:002013-07-15T08:38:00.707-07:00Breaking from a loop does not require continuation...Breaking from a loop does not require continuation passing style. Read this other post of mine which shows a simple alternative using the `Either`/`EitherT` monads:<br /><br />http://www.haskellforall.com/2012/07/breaking-from-loop.htmlGabriella Gonzalezhttps://www.blogger.com/profile/01917800488530923694noreply@blogger.comtag:blogger.com,1999:blog-1777990983847811806.post-45805845477086212322013-07-15T03:03:36.055-07:002013-07-15T03:03:36.055-07:00But one day you discover that you want jumps. Like...But one day you discover that you want jumps. Like, imagine you walk around the tree and calculate the product of all its nodes. Apparently, when you encounter a zero, you can return zero right away.<br /><br />And that's where statement-oriented languages and operational semantics of an abstract machine perform better than expression-oriented languages and denotational semantics: in the former case, jumps are extremely straightforward, clear, and intuitive. But try to model them in a lambda-calculus! Introduce a denotational semantics for them! Well, okay, that's not impossible, and people figured it out by the middle of the seventies — you need continuations. But the result is pretty gross. CPS-transformed programs look awful, with all that plumbing lying in plain sight, and continuations themselves are kinda confusing.<br /><br />Sure, with right syntactic sugar you can do this. But reasoning about them is still not pleasant, especially in a lazy language — jumps mess up evaluation order, and there is little you can do about this.Joker_vDhttps://www.blogger.com/profile/15515260210737317416noreply@blogger.comtag:blogger.com,1999:blog-1777990983847811806.post-91963033483072919652013-07-14T09:57:27.018-07:002013-07-14T09:57:27.018-07:00No apologies necessary! I appreciate corrections ...No apologies necessary! I appreciate corrections and I don't mind if you make mistakes. :)Gabriella Gonzalezhttps://www.blogger.com/profile/01917800488530923694noreply@blogger.comtag:blogger.com,1999:blog-1777990983847811806.post-4550623159909834492013-07-14T09:20:16.061-07:002013-07-14T09:20:16.061-07:00my bad :-) thought you need Just k x, but k alread...my bad :-) thought you need Just k x, but k already has codomain Maybe b...Eitanhttps://www.blogger.com/profile/06196826071384009822noreply@blogger.comtag:blogger.com,1999:blog-1777990983847811806.post-64430970140770049692013-07-14T09:18:21.033-07:002013-07-14T09:18:21.033-07:00This comment has been removed by the author.Eitanhttps://www.blogger.com/profile/06196826071384009822noreply@blogger.comtag:blogger.com,1999:blog-1777990983847811806.post-63439655252513008182013-07-14T07:02:01.389-07:002013-07-14T07:02:01.389-07:00I double-checked it and I think it is correct. He...I double-checked it and I think it is correct. Here is the `Monad` instance from `Data.Maybe` for comparison:<br /><br />instance Monad Maybe where<br />....(Just x) >>= k = k x<br />....Nothing >>= _ = Nothing<br />....<br />....return = Just<br /><br />Where do you think the extra `Just` belongs?Gabriella Gonzalezhttps://www.blogger.com/profile/01917800488530923694noreply@blogger.comtag:blogger.com,1999:blog-1777990983847811806.post-63877057877730497302013-07-13T22:24:22.238-07:002013-07-13T22:24:22.238-07:00missing a Just in the def of >>= for Maybemissing a Just in the def of >>= for MaybeEitanhttps://www.blogger.com/profile/06196826071384009822noreply@blogger.com