{"id":8272,"date":"2025-08-17T21:39:56","date_gmt":"2025-08-17T21:39:56","guid":{"rendered":"http:\/\/putridparrot.com\/blog\/?p=8272"},"modified":"2025-08-17T21:39:56","modified_gmt":"2025-08-17T21:39:56","slug":"classes-in-haskell-hint-theyre-more-like-interfaces","status":"publish","type":"post","link":"https:\/\/putridparrot.com\/blog\/classes-in-haskell-hint-theyre-more-like-interfaces\/","title":{"rendered":"Classes in Haskell (hint they&#8217;re more like interfaces)"},"content":{"rendered":"<p>A class (also know as typeclass) in Haskell is more analogous to an interfaces in C# and Java.<\/p>\n<p>Let&#8217;s look at a simple class which will supply a toString function<\/p>\n<pre class=\"brush: csharp; title: ; notranslate\" title=\"\">\r\nclass ToString a where \r\n  toString :: a -&gt; String\r\n<\/pre>\n<p>This class allows us to use a toString function on different types. So think of this as an interface where data types that support it will now work with the toString function.<\/p>\n<p>Let&#8217;s just create a simple data type as an example<\/p>\n<pre class=\"brush: csharp; title: ; notranslate\" title=\"\">\r\ndata Point = Point { x :: Integer, y :: Integer }\r\n<\/pre>\n<p>Now it&#8217;d be cool if we could use code such as <em>print (toString pt)<\/em> and have the output a string like this &#8220;X:1, Y:2&#8221;. All we need to do is create an instance of the class. Before we do this, we&#8217;re going to need to also support converting of an Integer (as it&#8217;s used in the Point) to a string, so we may as well create an instance of the ToString class for Integer&#8217;s first and then use this code in our Point instance<\/p>\n<pre class=\"brush: csharp; title: ; notranslate\" title=\"\">\r\ninstance ToString Integer where    \r\n    toString i = show i\r\n<\/pre>\n<p>So now we&#8217;ve got a ToString to handle Integer&#8217;s we can combine to create an instance for a Point, like this<\/p>\n<pre class=\"brush: csharp; title: ; notranslate\" title=\"\">\r\ninstance ToString Point where    \r\n    toString pt = &quot;X:&quot; ++ toString (x pt) ++ &quot;, &quot; ++ &quot;Y:&quot; ++ toString (y pt)\r\n<\/pre>\n","protected":false},"excerpt":{"rendered":"<p>A class (also know as typeclass) in Haskell is more analogous to an interfaces in C# and Java. Let&#8217;s look at a simple class which will supply a toString function class ToString a where toString :: a -&gt; String This class allows us to use a toString function on different types. So think of this [&hellip;]<\/p>\n","protected":false},"author":1,"featured_media":0,"comment_status":"closed","ping_status":"open","sticky":false,"template":"","format":"standard","meta":{"_jetpack_memberships_contains_paid_content":false,"footnotes":""},"categories":[295],"tags":[],"class_list":["post-8272","post","type-post","status-publish","format-standard","hentry","category-haskell"],"jetpack_sharing_enabled":true,"jetpack_featured_media_url":"","_links":{"self":[{"href":"https:\/\/putridparrot.com\/blog\/wp-json\/wp\/v2\/posts\/8272","targetHints":{"allow":["GET"]}}],"collection":[{"href":"https:\/\/putridparrot.com\/blog\/wp-json\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/putridparrot.com\/blog\/wp-json\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/putridparrot.com\/blog\/wp-json\/wp\/v2\/users\/1"}],"replies":[{"embeddable":true,"href":"https:\/\/putridparrot.com\/blog\/wp-json\/wp\/v2\/comments?post=8272"}],"version-history":[{"count":1,"href":"https:\/\/putridparrot.com\/blog\/wp-json\/wp\/v2\/posts\/8272\/revisions"}],"predecessor-version":[{"id":8273,"href":"https:\/\/putridparrot.com\/blog\/wp-json\/wp\/v2\/posts\/8272\/revisions\/8273"}],"wp:attachment":[{"href":"https:\/\/putridparrot.com\/blog\/wp-json\/wp\/v2\/media?parent=8272"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/putridparrot.com\/blog\/wp-json\/wp\/v2\/categories?post=8272"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/putridparrot.com\/blog\/wp-json\/wp\/v2\/tags?post=8272"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}