{"id":1609,"date":"2024-02-12T00:00:00","date_gmt":"2024-02-12T05:00:00","guid":{"rendered":"https:\/\/molecularsciences.org\/content\/?p=1609"},"modified":"2024-02-12T08:05:30","modified_gmt":"2024-02-12T13:05:30","slug":"haskell-cheat-sheet","status":"publish","type":"post","link":"https:\/\/molecularsciences.org\/content\/haskell-cheat-sheet\/","title":{"rendered":"Haskell Cheat Sheet"},"content":{"rendered":"\n<h4 class=\"wp-block-heading\"><strong>Basics:<\/strong><\/h4>\n\n\n\n<ul class=\"wp-block-list\">\n<li><strong>Function Definition:<\/strong><\/li>\n<\/ul>\n\n\n\n<pre class=\"wp-block-code\"><code>add :: Int -> Int -> Int\r\nadd x y = x + y\r<\/code><\/pre>\n\n\n\n<ul class=\"wp-block-list\">\n<li><strong>Type Declarations:<\/strong><\/li>\n<\/ul>\n\n\n\n<pre class=\"wp-block-code\"><code>name :: String\r\nname = \"John\"\r\n\r\nage :: Int\r\nage = 25\r<\/code><\/pre>\n\n\n\n<ul class=\"wp-block-list\">\n<li><strong>Lists:<\/strong><\/li>\n<\/ul>\n\n\n\n<pre class=\"wp-block-code\"><code>numbers :: &#91;Int]\r\nnumbers = &#91;1, 2, 3, 4, 5]\r\n\r\nstrings :: &#91;String]\r\nstrings = &#91;\"apple\", \"banana\", \"cherry\"]\r<\/code><\/pre>\n\n\n\n<h4 class=\"wp-block-heading\"><strong>Pattern Matching:<\/strong><\/h4>\n\n\n\n<ul class=\"wp-block-list\">\n<li><strong>Matching on Values:<\/strong><\/li>\n<\/ul>\n\n\n\n<pre class=\"wp-block-code\"><code>isZero :: Int -> Bool\r\nisZero 0 = True\r\nisZero _ = False\r<\/code><\/pre>\n\n\n\n<ul class=\"wp-block-list\">\n<li><strong>Matching on Lists:<\/strong><\/li>\n<\/ul>\n\n\n\n<pre class=\"wp-block-code\"><code>head' :: &#91;a] -> a\r\nhead' (x:_) = x\r\nhead' &#91;]    = error \"Empty list\"\r<\/code><\/pre>\n\n\n\n<h4 class=\"wp-block-heading\"><strong>Higher-Order Functions:<\/strong><\/h4>\n\n\n\n<ul class=\"wp-block-list\">\n<li><strong>Map Function:<\/strong><\/li>\n<\/ul>\n\n\n\n<pre class=\"wp-block-code\"><code>square :: Int -> Int\r\nsquare x = x * x\r\n\r\nsquares :: &#91;Int] -> &#91;Int]\r\nsquares = map square\r<\/code><\/pre>\n\n\n\n<ul class=\"wp-block-list\">\n<li><strong>Filter Function:<\/strong><\/li>\n<\/ul>\n\n\n\n<pre class=\"wp-block-code\"><code>isEven :: Int -> Bool\r\nisEven x = x `mod` 2 == 0\r\n\r\nevens :: &#91;Int] -> &#91;Int]\r\nevens = filter isEven\r<\/code><\/pre>\n\n\n\n<h4 class=\"wp-block-heading\"><strong>Lambda Expressions:<\/strong><\/h4>\n\n\n\n<ul class=\"wp-block-list\">\n<li><strong>Anonymous Function:<\/strong><\/li>\n<\/ul>\n\n\n\n<pre class=\"wp-block-code\"><code>double :: Int -> Int\r\ndouble = \\x -> x * 2\r<\/code><\/pre>\n\n\n\n<h4 class=\"wp-block-heading\"><strong>Typeclasses:<\/strong><\/h4>\n\n\n\n<ul class=\"wp-block-list\">\n<li><strong>Defining Typeclasses:<\/strong><\/li>\n<\/ul>\n\n\n\n<pre class=\"wp-block-code\"><code>class Eq a where\r\n    (==) :: a -> a -> Bool\r\n    (\/=) :: a -> a -> Bool\r<\/code><\/pre>\n\n\n\n<ul class=\"wp-block-list\">\n<li><strong>Implementing Instances:<\/strong><\/li>\n<\/ul>\n\n\n\n<pre class=\"wp-block-code\"><code>instance Eq Bool where\r\n    True  == True  = True\r\n    False == False = True\r\n    _     == _     = False\r<\/code><\/pre>\n\n\n\n<h4 class=\"wp-block-heading\"><strong>Monads:<\/strong><\/h4>\n\n\n\n<ul class=\"wp-block-list\">\n<li><strong>Maybe Monad:<\/strong><\/li>\n<\/ul>\n\n\n\n<pre class=\"wp-block-code\"><code>divide :: Double -> Double -> Maybe Double\r\ndivide _ 0 = Nothing\r\ndivide x y = Just (x \/ y)\r<\/code><\/pre>\n\n\n\n<h4 class=\"wp-block-heading\"><strong>IO Operations:<\/strong><\/h4>\n\n\n\n<ul class=\"wp-block-list\">\n<li><strong>Printing to Console:<\/strong><\/li>\n<\/ul>\n\n\n\n<pre class=\"wp-block-code\"><code>main :: IO ()\r\nmain = putStrLn \"Hello, Haskell!\"\r<\/code><\/pre>\n\n\n\n<ul class=\"wp-block-list\">\n<li><strong>Reading from Console:<\/strong><\/li>\n<\/ul>\n\n\n\n<pre class=\"wp-block-code\"><code>main :: IO ()\r\nmain = do\r\n    putStrLn \"What's your name?\"\r\n    name &lt;- getLine\r\n    putStrLn $ \"Hello, \" ++ name ++ \"!\"\r<\/code><\/pre>\n\n\n\n<p>This Haskell cheat sheet covers basic syntax, common constructs, and functional programming concepts. Haskell&#8217;s expressive and concise syntax makes it a powerful language for various applications, including mathematical modeling, compiler<\/p>\n","protected":false},"excerpt":{"rendered":"<p>Basics: Pattern Matching: Higher-Order Functions: Lambda Expressions: Typeclasses: Monads: IO Operations: This Haskell cheat sheet covers basic syntax, common constructs, and functional programming concepts. Haskell&#8217;s expressive and concise syntax makes it a powerful language for various applications, including mathematical modeling, compiler<\/p>\n","protected":false},"author":1,"featured_media":1769,"comment_status":"closed","ping_status":"closed","sticky":false,"template":"","format":"standard","meta":{"footnotes":""},"categories":[265],"tags":[],"class_list":["post-1609","post","type-post","status-publish","format-standard","has-post-thumbnail","hentry","category-reference"],"_links":{"self":[{"href":"https:\/\/molecularsciences.org\/content\/wp-json\/wp\/v2\/posts\/1609","targetHints":{"allow":["GET"]}}],"collection":[{"href":"https:\/\/molecularsciences.org\/content\/wp-json\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/molecularsciences.org\/content\/wp-json\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/molecularsciences.org\/content\/wp-json\/wp\/v2\/users\/1"}],"replies":[{"embeddable":true,"href":"https:\/\/molecularsciences.org\/content\/wp-json\/wp\/v2\/comments?post=1609"}],"version-history":[{"count":1,"href":"https:\/\/molecularsciences.org\/content\/wp-json\/wp\/v2\/posts\/1609\/revisions"}],"predecessor-version":[{"id":1611,"href":"https:\/\/molecularsciences.org\/content\/wp-json\/wp\/v2\/posts\/1609\/revisions\/1611"}],"wp:featuredmedia":[{"embeddable":true,"href":"https:\/\/molecularsciences.org\/content\/wp-json\/wp\/v2\/media\/1769"}],"wp:attachment":[{"href":"https:\/\/molecularsciences.org\/content\/wp-json\/wp\/v2\/media?parent=1609"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/molecularsciences.org\/content\/wp-json\/wp\/v2\/categories?post=1609"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/molecularsciences.org\/content\/wp-json\/wp\/v2\/tags?post=1609"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}