<?xml version="1.0" encoding="UTF-8"?>
<rss version="2.0" xmlns:atom="http://www.w3.org/2005/Atom" xmlns:dc="http://purl.org/dc/elements/1.1/">
  <channel>
    <title>DEV Community: Yosuf</title>
    <description>The latest articles on DEV Community by Yosuf (@yosuf).</description>
    <link>https://dev.to/yosuf</link>
    <image>
      <url>https://media2.dev.to/dynamic/image/width=90,height=90,fit=cover,gravity=auto,format=auto/https:%2F%2Fdev-to-uploads.s3.us-east-2.amazonaws.com%2Fuploads%2Fuser%2Fprofile_image%2F732686%2F063b8ee1-28eb-4c89-81be-460d617d0ac6.jpeg</url>
      <title>DEV Community: Yosuf</title>
      <link>https://dev.to/yosuf</link>
    </image>
    <atom:link rel="self" type="application/rss+xml" href="https://dev.to/feed/yosuf"/>
    <language>en</language>
    <item>
      <title>Setting up your Go environment</title>
      <dc:creator>Yosuf</dc:creator>
      <pubDate>Tue, 02 Nov 2021 20:53:57 +0000</pubDate>
      <link>https://dev.to/yosuf/setting-up-your-go-environment-3icl</link>
      <guid>https://dev.to/yosuf/setting-up-your-go-environment-3icl</guid>
      <description>&lt;p&gt;&lt;a href="https://res.cloudinary.com/practicaldev/image/fetch/s--EeMZx7Ym--/c_limit%2Cf_auto%2Cfl_progressive%2Cq_auto%2Cw_880/https://yosuf.dev/content/images/2021/11/golang.png" class="article-body-image-wrapper"&gt;&lt;img src="https://res.cloudinary.com/practicaldev/image/fetch/s--EeMZx7Ym--/c_limit%2Cf_auto%2Cfl_progressive%2Cq_auto%2Cw_880/https://yosuf.dev/content/images/2021/11/golang.png" alt="" width="880" height="497"&gt;&lt;/a&gt;&lt;/p&gt;

&lt;blockquote&gt;
&lt;p&gt;You can subscribe to my &lt;a href="https://yosuf.substack.com/subscribe"&gt;Substack&lt;/a&gt; to receive these in your email and be notified of future posts.&lt;/p&gt;
&lt;/blockquote&gt;

&lt;p&gt;Installing Go is pretty straightforward, you can just download it for your system from the go &lt;a href="https://golang.org/dl"&gt;website&lt;/a&gt;. Mac OS users can also use brew, and Windows users can also use chocolatey. In both cases, Go should be installed in the correct location and put into the correct executable &lt;code&gt;$PATH&lt;/code&gt;.&lt;/p&gt;

&lt;p&gt;For Linux users, once you've downloaded and expanded the tar files, copy it over to &lt;code&gt;/usr/local&lt;/code&gt; and set &lt;code&gt;/usr/local/go/bin&lt;/code&gt; in your &lt;code&gt;$PATH&lt;/code&gt; so you can run go.&lt;/p&gt;

&lt;p&gt;Programs written in Go compile down into a single executable file that does not need any other software to run. So you only really need to install Go on the computer that will build the Go code, not necessarily on the computer that will run the final executable.&lt;/p&gt;

&lt;p&gt;To test your installation was successful, you can run &lt;code&gt;go version&lt;/code&gt; which should print out the version you've just installed.&lt;/p&gt;

&lt;h3&gt;
  
  
  Your Workspace
&lt;/h3&gt;

&lt;p&gt;You can organise your Go projects as you want, but Go still expects you to have a single place for the libraries you install with &lt;code&gt;go install&lt;/code&gt; . You can set this place yourself but it is &lt;code&gt;$HOME/go&lt;/code&gt; by default. Either way, it's important to explicitly state where this is in your bash profile.&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;export GOPATH=$HOME/go
export PATH=$PATH:/$GOPATH/bin
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;Don't forget to source your &lt;code&gt;.profile&lt;/code&gt; to have your updates take effect.&lt;/p&gt;

&lt;p&gt;To have a look at all of the environment variables go is aware of, you can run &lt;code&gt;go env&lt;/code&gt; from your terminal.&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;$ go env

GOARCH="amd64"
GOENV="/path/to/go/env
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;h3&gt;
  
  
  What's included, running and building code
&lt;/h3&gt;

&lt;p&gt;Go comes with a ton of stuff off the bat, including a code formatter, a compiler, a linter, a test runner, and a dependency manager.&lt;/p&gt;

&lt;p&gt;Here's a simple Hello World program:&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;//hello.go

package main

import "fmt"

func main() {
    fmt.Println("Hello world")
}
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;If you run &lt;code&gt;go run hello.go&lt;/code&gt;, you should see Hello World printed out onto the console, but you won't find a binary next to your &lt;code&gt;hello.go&lt;/code&gt; file. This is because &lt;code&gt;go run&lt;/code&gt; compiles your code into a binary, but in a temporary folder which it then deletes after it's done executing your code. This makes it handy to use it for scripting or testing things out.&lt;/p&gt;

&lt;p&gt;To create an executable, run &lt;code&gt;go build hello.go&lt;/code&gt;. The binary name will by default match the file or package name. You can specify the name or location using the &lt;code&gt;-o&lt;/code&gt; flag: &lt;code&gt;go build -o my_custom_name hello.go&lt;/code&gt;.&lt;/p&gt;

&lt;h3&gt;
  
  
  Installing other peoples code
&lt;/h3&gt;

&lt;p&gt;Go uses &lt;code&gt;go install&lt;/code&gt; to install third-party software. However, the way Go stores and distributes third-party projects is a little different to other languages, like JavaScript with it's NPM registry. There's no central registry with Go. Instead, projects are shared by their source code and compiled on your machine when you run &lt;code&gt;go install&lt;/code&gt;. The command takes an argument which is the location of where the source code's repository is and the version of the code you want. It then downloads, compiles, and installs it for you.&lt;/p&gt;

&lt;p&gt;As an example, there's a tool called Hey for load testing HTTP servers. You can install it as follows:&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;go install github.com/rakyll/hey@latest
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;This will pull the source code down, compile it for you and turn it into an executable, which you can then run as &lt;code&gt;hey&lt;/code&gt;:&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;hey https://golang.org
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;h3&gt;
  
  
  Formatting
&lt;/h3&gt;

&lt;p&gt;Go has a specific way of formatting code that is built-in as part of the language. Unlike most languages that offer flexibility in how you lay your code out, Go has standardised formatting that makes it easier to write, but also easier to manipulate your source code using external tooling.&lt;/p&gt;

&lt;p&gt;It also prevents developers from disagreeing on what the "better" way to write their code is. For example, Go uses tabs to indent and you can't choose to write your function's opening brace on the next line like you can in some other languages, it'd actually give you a syntactical error.&lt;/p&gt;

&lt;p&gt;Go comes with the command &lt;code&gt;fmt&lt;/code&gt; which formats your code for you into this standardised way.&lt;/p&gt;

&lt;h3&gt;
  
  
  The semicolon insertion rule
&lt;/h3&gt;

&lt;p&gt;Go requires a semicolon at the end of each statement, but the developers themselves don't put them in, the compiler does.&lt;/p&gt;

&lt;p&gt;It follows a simple rule, putting a semicolon at the end of a line if the last item is one of the following:&lt;/p&gt;

&lt;ul&gt;
&lt;li&gt;An identifier (e.g. int, float64)&lt;/li&gt;
&lt;li&gt;A basic literal (e.g. 5, "hello")&lt;/li&gt;
&lt;li&gt;One of the tokens: "continue", "break", "fallthrough", "return", "++", --", "}", ")"&lt;/li&gt;
&lt;/ul&gt;

&lt;p&gt;This simple rule makes the Go compiler faster and simple but also enforces a programming style.&lt;/p&gt;

&lt;h3&gt;
  
  
  Linting and scanning your code for bugs
&lt;/h3&gt;

&lt;p&gt;The tool &lt;code&gt;golint&lt;/code&gt; tries to make sure your code matches the Go style guidelines. It highlights things you could change and gives you suggestions, and its a good idea to take them onboard since Go developers expect code to look a certain way. But it's not completely accurate and can give false negatives and positives.&lt;/p&gt;

&lt;p&gt;When it comes to catching errors like passing the wrong number of variables to a function or unused variables, that's what the tool &lt;code&gt;go vet&lt;/code&gt; is used for.&lt;/p&gt;

&lt;p&gt;Since there are many other tools that you can also run to check code style and find bugs, it can be unruly to run them all individually. You can run multiple tools together with &lt;code&gt;golangci-lint&lt;/code&gt;. It runs &lt;code&gt;golint&lt;/code&gt;, &lt;code&gt;go vet&lt;/code&gt;, and a list of other code quality tools. If you don't agree with all of the suggestions it gives you, you can configure which you want to run and what files they should run on by adding a file called &lt;code&gt;.golangci.yml&lt;/code&gt;. Then you can run it as usual with &lt;code&gt;golangci-lint run&lt;/code&gt;.&lt;/p&gt;

&lt;h3&gt;
  
  
  The Go Playground
&lt;/h3&gt;

&lt;p&gt;The &lt;a href="https://play.golang.org/"&gt;Go Playground&lt;/a&gt; is a website that gives you a place to quickly try out Go code, similar to Python and Node command-line environments.&lt;/p&gt;

&lt;blockquote&gt;
&lt;p&gt;This post is some of my notes on Go, the resources I've used can be found below.&lt;/p&gt;
&lt;/blockquote&gt;

&lt;h3&gt;
  
  
  Resources
&lt;/h3&gt;

&lt;p&gt;&lt;a href="https://www.oreilly.com/library/view/learning-go/9781492077206/"&gt;Learning Go - Jon Bodner&lt;/a&gt;&lt;/p&gt;

&lt;p&gt;&lt;a href="https://golang.org/doc/effective_go"&gt;Effective Go - Golang&lt;/a&gt;&lt;/p&gt;

&lt;p&gt;&lt;a href="https://github.com/golang/go/wiki/CodeReviewComments"&gt;Golang Code Review Comments&lt;/a&gt;&lt;/p&gt;

</description>
      <category>tech</category>
      <category>technical</category>
      <category>go</category>
    </item>
    <item>
      <title>Learning to code and what not</title>
      <dc:creator>Yosuf</dc:creator>
      <pubDate>Mon, 23 Aug 2021 18:08:39 +0000</pubDate>
      <link>https://dev.to/yosuf/learning-to-code-and-what-not-3o27</link>
      <guid>https://dev.to/yosuf/learning-to-code-and-what-not-3o27</guid>
      <description>&lt;p&gt;&lt;a href="https://res.cloudinary.com/practicaldev/image/fetch/s--P94DxYUp--/c_limit%2Cf_auto%2Cfl_progressive%2Cq_auto%2Cw_880/https://images.unsplash.com/photo-1489533119213-66a5cd877091" class="article-body-image-wrapper"&gt;&lt;img src="https://res.cloudinary.com/practicaldev/image/fetch/s--P94DxYUp--/c_limit%2Cf_auto%2Cfl_progressive%2Cq_auto%2Cw_880/https://images.unsplash.com/photo-1489533119213-66a5cd877091" alt="" width="880" height="587"&gt;&lt;/a&gt;&lt;/p&gt;

&lt;p&gt;As you may or may not know, I took the traditional route of becoming a Software Engineer. I Studied Computer Science at university, built things in my spare time, yada yada, got a job in tech, then a few more and that’s basically it in a nut shell.&lt;/p&gt;

&lt;p&gt;And I’ve spent some time over the years talking to people who have backgrounds outside of technology, who wanted to transition into tech. They’d studied Chemistry, or English, or Civil Engineering, or Sociology, and they felt like they were constantly playing catch up. And what’s more, there were a few who actually saw their background in a non-tech field as a hindrance to their new career.&lt;/p&gt;

&lt;p&gt;It was a specific conversation I had with a friend who was considering transitioning into tech that made me realise something. Even though he enjoyed studying the subject he did at university, he expressed remorse that he hadn’t studied Computer Science.&lt;/p&gt;

&lt;p&gt;And this really blew my mind. Because it really didn’t align with how I viewed learning and problem solving in general. I could totally understand the sentiment and why one would feel that way, but I’m of the opinion that having a background outside of tech, and then transitioning into software development, is actually way more powerful than someone who has only studied software engineering / CS. And that this is becoming more and more true every year.&lt;/p&gt;

&lt;p&gt;Why?&lt;/p&gt;

&lt;p&gt;Because building software is a skill, much like any other, it’s something that you can develop at any point in your life. I don’t think I know of any other field where you can become an expert in a niche and eventually be paid the same amount as someone who has a degree in the subject, without ever having had any academic training in it. The amount of abundant resources that are available to help you learn for free is ginormous. From tech companies themselves no less.&lt;/p&gt;

&lt;p&gt;Writing code is a tool that you use to be able to solve problems. But all of the worlds problems can’t be solved with just code. So as someone who has a background in History or Sociology for example, you have a unique set of skills that someone else may not. In my opinion, you’re likely better able to see problems in society and their potential “technical” solutions than a software developer who hasn’t studied these subjects.&lt;/p&gt;

&lt;p&gt;(I’m a proponent of studying ALL of the things but alas only so many hours in a day and ones life)&lt;/p&gt;

&lt;p&gt;A very beautiful thing about Software Engineering is that it intersects with everything. It compliments every other field or subject that there is. You studied Agriculture? You could apply tech to make yours and others’ lives easier. Studied linguistics? There’s an entire subfield of Computer Science (and linguistics for that matter), Natural Languages Processing, that you’d probably be better suited to than someone who hadn’t studied linguistics.&lt;/p&gt;

&lt;p&gt;Having studied other than Computer Science just means you’ll have a cross section of skills that you can use to solve problems that not many other people are equipped to solve.&lt;/p&gt;

&lt;p&gt;None of this is to say you shouldn’t study Computer Science at university, you totally should if it’s something you’re deeply interested in. But if you’re deeply interested in something else, like Physics, or Mathematics or Sociology, go and get really good at that thing. And then pick up software development along the way or afterwards. It’s the same reason I advocate for people to maintain their skills from their previous studies and jobs. I try to view life and careers less like a single thing that you’re going to learn and do for the rest of your existence. But to accumulate various seemingly unrelated skills that all provide different views of the world, and therefore equip you with different ways of solving varying and complex problems.&lt;/p&gt;

&lt;p&gt;I try to say don’t think of learning to code as much as a career “transition”, and more as a new skill that you’re adding to your toolbox that will enable you to solve more problems.&lt;/p&gt;

&lt;p&gt;If you’re picking up coding and you have a career in something completely different, try not to feel like you’re starting from scratch, and certainly don’t feel regretful for not having done so sooner. Knowledge is knowledge and life experience counts for something. You’re not behind, you’re actually much further along than you think.&lt;/p&gt;




&lt;blockquote&gt;
&lt;p&gt;Subscribe to my &lt;a href="https://yosuf.substack.com/subscribe"&gt;Substack&lt;/a&gt; to be notified of future posts.&lt;/p&gt;
&lt;/blockquote&gt;

</description>
      <category>tech</category>
      <category>career</category>
      <category>advice</category>
    </item>
    <item>
      <title>Notes on Haskell</title>
      <dc:creator>Yosuf</dc:creator>
      <pubDate>Fri, 14 Sep 2018 14:07:00 +0000</pubDate>
      <link>https://dev.to/yosuf/notes-on-haskell-4k51</link>
      <guid>https://dev.to/yosuf/notes-on-haskell-4k51</guid>
      <description>&lt;p&gt;&lt;em&gt;These are notes from my lectures at university augmented with some online and textbook reading. They’re unfinished but they’ve been sitting in my drafts so I thought I’d publish them in case others find them useful.&lt;/em&gt;&lt;/p&gt;

&lt;blockquote&gt;
&lt;p&gt;&lt;em&gt;Surely there must be a less primitive way of making big changes in the store than by pushing vast numbers of words back and forth through the von Neumann bottleneck. Not only is this tube a literal bottleneck for the data traffic of a problem, but, more importantly, it is an intellectual bottleneck that has kept us tied to word-at-a-time thinking instead of encouraging us to think in terms of the larger conceptual units of the task at hand. Thus programming is basically planning and detailing the enormous traffic of words through the von Neumann bottleneck, and much of that traffic concerns not significant data itself, but where to find it. — John Backus&lt;/em&gt;&lt;/p&gt;
&lt;/blockquote&gt;

&lt;h1&gt;
  
  
  &lt;strong&gt;Contents&lt;/strong&gt;
&lt;/h1&gt;

&lt;ul&gt;
&lt;li&gt;Thinking functionally&lt;/li&gt;
&lt;li&gt;GHCi&lt;/li&gt;
&lt;li&gt;Basics&lt;/li&gt;
&lt;li&gt;Lists&lt;/li&gt;
&lt;li&gt;Expressions and Types&lt;/li&gt;
&lt;li&gt;Functions&lt;/li&gt;
&lt;li&gt;Constrained Polymorphism&lt;/li&gt;
&lt;li&gt;Language Details&lt;/li&gt;
&lt;/ul&gt;

&lt;h1&gt;
  
  
  &lt;strong&gt;Thinking Functionally&lt;/strong&gt;
&lt;/h1&gt;

&lt;p&gt;Procedural programming closely mirrors the underlying architecture and focuses on &lt;em&gt;how&lt;/em&gt; something is done. Declarative programming (including functional) focuses on &lt;em&gt;what&lt;/em&gt; the answer is and provides powerful abstraction (generalisation) mechanisms, as well as being concise and expressive.&lt;/p&gt;

&lt;p&gt;Functional programming is programming with functions (lol). The most basic component is a function. They determine a unique output for each combination of inputs.&lt;/p&gt;

&lt;p&gt;Instead of asking “How do I change the value of a variable?” or “How do I write a loop?”, ask “What value do I want to produce?”, “How can I make it from the input?” “What are the intermediate values?”.&lt;/p&gt;

&lt;p&gt;Expressions are just their values and should be interchangeable.&lt;/p&gt;

&lt;p&gt;&lt;strong&gt;GHCi&lt;/strong&gt;&lt;/p&gt;

&lt;p&gt;GHC is the compiler for Haskell. GHCi is the interactive environment in which Haskell expressions can be interactively evaluated and programs can be interpreted. It’s what you use on the command line to write Haskell. If you want to follow along, you can either download GHCi or use &lt;a href="http://repl.it/languages/haskell"&gt;repl.it&lt;/a&gt; for a browser-based environment.&lt;/p&gt;

&lt;p&gt;Prelude is just what you get for the command prompt when inside GHCi:&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;Prelude&amp;gt;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;&lt;strong&gt;GHCi Commands&lt;/strong&gt;&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;:load filename … (or :l as a shortcut) — loads modules from specified 
files:reload (or :r) - repeats the last load command
:type exp (or :t) - prints the type of the expression
:quit (or :q) - exits the interpreter
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;h1&gt;
  
  
  &lt;strong&gt;Basics&lt;/strong&gt;
&lt;/h1&gt;

&lt;p&gt;Examples of basic math operations as well as using built-in functions:&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;Prelude&amp;gt; 2+3
5

Prelude&amp;gt; 2+3*5
17

Prelude&amp;gt; (2+3)*5
25

Prelude&amp;gt; 2^3
8

Prelude&amp;gt; sqrt 2
1.4142135623730951

Prelude&amp;gt; sqrt 2 + 3
4.414213562373095

Prelude&amp;gt; sqrt (sqrt 2)
1.189207115002721

Prelude&amp;gt; max 4 8
8

Prelude&amp;gt; div 13 5
2

Prelude&amp;gt; mod 13 5
3

Prelude&amp;gt; 13 `div` 5
2

Prelude&amp;gt; 13 `mod` 5
3
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;The last two are examples of infix functions (take note of the weird quotes).&lt;/p&gt;

&lt;h1&gt;
  
  
  &lt;strong&gt;Lists&lt;/strong&gt;
&lt;/h1&gt;



&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;Prelude&amp;gt; [1, 1+3, 1+3+5]
[1,4,9]
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;The value of [a..b] is a list of integers starting at &lt;strong&gt;a&lt;/strong&gt; and ending at &lt;strong&gt;b&lt;/strong&gt;.&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;Prelude&amp;gt; [1..6]
[1,2,3,4,5,6]

Prelude&amp;gt; [2..2]
[2]

Prelude&amp;gt; [6..1]
[]

Prelude&amp;gt; [6,5..1]
[6,5,4,3,2,1]

Prelude&amp;gt; 1:2:[]
[1,2]
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;Some list related functions:&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;Prelude&amp;gt; product [3,4,5]
60

Prelude&amp;gt; product [1..7]
5040

Prelude&amp;gt; product []
1

Prelude&amp;gt; length [1,3,5]
3

Prelude&amp;gt; reverse [1,3,5]
[5,3,1]
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;If you’re wondering why &lt;strong&gt;product [] = 1&lt;/strong&gt;, its because of the &lt;a href="https://stackoverflow.com/questions/33732513/why-does-product-return-1"&gt;identity in the category of multiplication&lt;/a&gt;. A practical explanation:&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;product [1,2,3]
    == product [1] * product 2:3:[]
    == product [1] * product [2] * product 3:[]
    == product [1] * product [2] * product [3] * product []
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;which allows us to implement it with simple recursion:&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;product [] = 1
product (x:xs) = x * product xs
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;In Haskell, strings are just lists of characters:&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;Prelude&amp;gt; ['H', 'e', 'l', 'l', 'o']
"Hello"

Prelude&amp;gt; ['a'..'z']
"abcdefghijklmnopqrstuvwxyz"

Prelude&amp;gt; length "Hello"
5

Prelude&amp;gt; reverse "Hello"
"olleH"
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;&lt;strong&gt;[char]&lt;/strong&gt; is synonymous with &lt;strong&gt;String&lt;/strong&gt;.&lt;/p&gt;

&lt;h1&gt;
  
  
  &lt;strong&gt;Expressions and Types&lt;/strong&gt;
&lt;/h1&gt;

&lt;p&gt;&lt;a href="https://res.cloudinary.com/practicaldev/image/fetch/s--rikltKNx--/c_limit%2Cf_auto%2Cfl_progressive%2Cq_auto%2Cw_880/https://miro.medium.com/max/1400/1%2A2LJzBg86pHVL9UWRda-szQ.png" class="article-body-image-wrapper"&gt;&lt;img src="https://res.cloudinary.com/practicaldev/image/fetch/s--rikltKNx--/c_limit%2Cf_auto%2Cfl_progressive%2Cq_auto%2Cw_880/https://miro.medium.com/max/1400/1%2A2LJzBg86pHVL9UWRda-szQ.png" alt="" width="880" height="350"&gt;&lt;/a&gt;&lt;br&gt;
&lt;em&gt;Basic Types in Haskell&lt;/em&gt;&lt;/p&gt;

&lt;p&gt;&lt;strong&gt;Types&lt;/strong&gt;&lt;/p&gt;

&lt;p&gt;Every expression in Haskell has a type.&lt;/p&gt;

&lt;p&gt;Unlike some languages like Java and Pascal, Haskell has type inference. If we write a number we don’t have to tell Haskell it’s a number. It can &lt;em&gt;infer&lt;/em&gt; that on its own, so we don’t have to explicitly write out the types of our functions and expressions to get things done.&lt;/p&gt;

&lt;p&gt;&lt;strong&gt;Types&lt;/strong&gt; are sets of values, and &lt;strong&gt;Typeclasses&lt;/strong&gt; are sets of Types.&lt;/p&gt;

&lt;p&gt;For example, the type &lt;em&gt;Integer&lt;/em&gt; includes values like 3 and 8, and the Typeclass &lt;em&gt;Num&lt;/em&gt; includes the Types Integer and Double.&lt;/p&gt;

&lt;p&gt;This is somewhat analogous to Java, where Classes are sets of Objects, and Interfaces are sets of Classes:&lt;/p&gt;

&lt;p&gt;&lt;a href="https://res.cloudinary.com/practicaldev/image/fetch/s--dAloISh---/c_limit%2Cf_auto%2Cfl_progressive%2Cq_auto%2Cw_880/https://yosuf.dev/content/images/2021/07/image.png" class="article-body-image-wrapper"&gt;&lt;img src="https://res.cloudinary.com/practicaldev/image/fetch/s--dAloISh---/c_limit%2Cf_auto%2Cfl_progressive%2Cq_auto%2Cw_880/https://yosuf.dev/content/images/2021/07/image.png" alt="" width="880" height="836"&gt;&lt;/a&gt;&lt;/p&gt;

&lt;p&gt;Checking the type of 3 gives us a weird answer:&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;Prelude&amp;gt; :t 3
3 :: Num a =&amp;gt; a
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;Here, &lt;strong&gt;a&lt;/strong&gt; is called a type variable. Everything before the &lt;strong&gt;=&amp;gt;&lt;/strong&gt; is called the class constraint. The above says “&lt;em&gt;there is some type&lt;/em&gt; &lt;strong&gt;&lt;em&gt;a&lt;/em&gt;&lt;/strong&gt; &lt;em&gt;in the typeclass&lt;/em&gt; &lt;strong&gt;&lt;em&gt;Num&lt;/em&gt;&lt;/strong&gt; &lt;em&gt;, 3 is of type&lt;/em&gt; &lt;strong&gt;&lt;em&gt;a&lt;/em&gt;&lt;/strong&gt;.” The value of 3 belongs to every type in the Num typeclass, including Integer and Double. Haskell is just being lazy and not committing to a specific type.&lt;/p&gt;

&lt;p&gt;Functions also have types:&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;Prelude&amp;gt; :t head
head :: [a] -&amp;gt; a
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;This says for any type &lt;strong&gt;a&lt;/strong&gt; , the function &lt;em&gt;head&lt;/em&gt; takes a list of &lt;strong&gt;a&lt;/strong&gt; s and returns an &lt;strong&gt;a&lt;/strong&gt;. (head returns the first element of a list).&lt;/p&gt;

&lt;p&gt;We can even ask for the type of a plus operator. We surround it with parentheses since its an infix operator (meaning we usually use it between values: 2 + 2, otherwise GHCi will get confused and think we’re actually using it ):&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;Prelude&amp;gt; :t (+)
(+) :: Num a =&amp;gt; a -&amp;gt; a -&amp;gt; a
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;This says: “There is some type &lt;strong&gt;a&lt;/strong&gt; in the typeclass &lt;strong&gt;Num&lt;/strong&gt; , “+” takes two &lt;strong&gt;a&lt;/strong&gt; s and returns another &lt;strong&gt;a&lt;/strong&gt;.&lt;/p&gt;

&lt;p&gt;How about the function zip:&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;Prelude&amp;gt; :t zip
zip :: [a] -&amp;gt; [b] -&amp;gt; [(a, b)]
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;Zip takes a list of &lt;strong&gt;a&lt;/strong&gt; s and a list of &lt;strong&gt;b&lt;/strong&gt; s and it returns a list of &lt;strong&gt;(a,b)&lt;/strong&gt; pairs.&lt;/p&gt;

&lt;p&gt;We’re allowed to specify the type of a function along with its definition, which is generally considered good practice:&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;add :: Integer -&amp;gt; Integer -&amp;gt; Integer
add x y = x + y
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;On the first line, we explicitly tell Haskell that the add function we’re about to define takes two Integers and returns an Integer. Then we define the function on the second line.&lt;/p&gt;

&lt;p&gt;Being explicit about types provides some documentation for us and anyone who reads our code, but it also helps us find bugs. For example, say we have the following function:&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;dividesEvenly x y = (y / x) * x == y
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;This function is meant to tell us if x evenly divides into y. So if x=2, and y=5, we would expect (5/2) to be 2, since 2 goes into 5 twice, then 2*2 would give us 4. 4==5 is not true so we would expect this function to return False given 2 5 as arguments. However, it returns True.&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;Prelude&amp;gt; dividesEvenly 2 5
True
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;This is because, in Haskell, 5/2 is not 2, but 2.5. One way to address this is to specify that we expect x and y to be Ints:&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;Prelude&amp;gt; dividesEvenly :: Int -&amp;gt; Int -&amp;gt; Bool
Prelude&amp;gt; dividesEvenly x y = (y / x) * x == y
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;However, this gives us an error:&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;&amp;lt;interactive&amp;gt;:3:1: error:    
    • No instance for (Fractional Int)
            arising from a use of ‘dividesEvenly’
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;It is telling us that Int is not a member of the typeclass Fractional. The &lt;strong&gt;/&lt;/strong&gt; operator only works on Fractionals. Turns out the operator we want is &lt;strong&gt;&lt;code&gt;div&lt;/code&gt;&lt;/strong&gt; :&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;Prelude&amp;gt; dividesEvenly :: Int -&amp;gt; Int -&amp;gt; Bool
Prelude&amp;gt; dividesEvenly x y = (y `div` x) * x == y
Prelude&amp;gt; divideEvenly 2 5 False
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;By specifying the type of our function, we were able to find a bug that we may have otherwise missed.&lt;/p&gt;

&lt;h1&gt;
  
  
  Functions
&lt;/h1&gt;

&lt;p&gt;&lt;strong&gt;Infix Operators&lt;/strong&gt;&lt;/p&gt;

&lt;p&gt;Haskell permits the usual infix notation:&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;Prelude&amp;gt; 1 + 2*3
11
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;The infix operators &lt;code&gt;+&lt;/code&gt; and &lt;code&gt;*&lt;/code&gt; refer to functions that take two arguments. Operators also have precedence and associations. To refer to these kinds of functions by themselves, we wrap them in parentheses, writing &lt;code&gt;(+)&lt;/code&gt; and &lt;code&gt;(*)&lt;/code&gt;. So we can use them like this:&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;Prelude&amp;gt; (*) 2 5
10
Prelude&amp;gt; (+) 1 ((*) 2 5)
11
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;If we have ordinary identifiers that refer to binary functions:&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;div :: Int -&amp;gt; Int -&amp;gt; Int
mod :: Int -&amp;gt; Int -&amp;gt; Int
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;we can use them as infix functions by wrapping them in backquotes:&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;Prelude&amp;gt; (1987 `div` 100) `mod` 4
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;which is the same as:&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;Prelude&amp;gt; mod (div 1987 100) 4
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;&lt;strong&gt;Functions on Int&lt;/strong&gt;&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;(+) :: Int -&amp;gt; Int -&amp;gt; Int
(-) :: Int -&amp;gt; Int -&amp;gt; Int
(*) :: Int -&amp;gt; Int -&amp;gt; Int
(^) :: Int -&amp;gt; Int -&amp;gt; Int
div :: Int -&amp;gt; Int -&amp;gt; Int
mod :: Int -&amp;gt; Int -&amp;gt; Int
abs :: Int -&amp;gt; Int
negate :: Int -&amp;gt; Int
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;Examples:&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;Prelude&amp;gt; abs 3
3

Prelude&amp;gt; negate 3
-3

Prelude&amp;gt; abs (negate 3)
3

Prelude&amp;gt; negate (negate 3)
3

Prelude&amp;gt; abs 0
0

Prelude&amp;gt; negate 0
0
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;&lt;strong&gt;Relational Operators&lt;/strong&gt;&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;(&amp;lt;) :: Int -&amp;gt; Int -&amp;gt; Bool
(&amp;lt;=) :: Int -&amp;gt; Int -&amp;gt; Bool
(&amp;gt;) :: Int -&amp;gt; Int -&amp;gt; Bool
(&amp;gt;=) :: Int -&amp;gt; Int -&amp;gt; Bool
(==) :: Int -&amp;gt; Int -&amp;gt; Bool
(/=) :: Int -&amp;gt; Int -&amp;gt; Bool
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;Examples:&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;Prelude&amp;gt; 2 &amp;lt; 3
True

Prelude&amp;gt; 2 &amp;lt; 3
True

Prelude&amp;gt; 2 &amp;lt; 2
False
Prelude&amp;gt; 2 /= 2
False

Prelude&amp;gt; 2 /= 3
True
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;&lt;strong&gt;Overloading&lt;/strong&gt;&lt;/p&gt;

&lt;p&gt;The &lt;code&gt;(==)&lt;/code&gt; function has several types:&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;(==) :: Int -&amp;gt; Int -&amp;gt; Bool
(==) :: Bool -&amp;gt; Bool -&amp;gt; Bool
(==) :: Char -&amp;gt; Char -&amp;gt; Bool
(==) :: Float -&amp;gt; Float -&amp;gt; Bool
(==) :: Eq a =&amp;gt; a -&amp;gt; a -&amp;gt; Bool
(&amp;lt;=) :: Ord a =&amp;gt; a -&amp;gt; a -&amp;gt; Bool
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;&lt;strong&gt;Building Boolean expressions&lt;/strong&gt;&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;(&amp;amp;&amp;amp;) :: Bool -&amp;gt; Bool -&amp;gt; Bool
(||) :: Bool -&amp;gt; Bool -&amp;gt; Bool
not :: Bool -&amp;gt; Bool
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;Examples:&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;Prelude&amp;gt; not True
False

Prelude&amp;gt; False || True
True

Prelude&amp;gt; 1 &amp;lt; 2 || 1 &amp;gt; 2
True
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;A function using relational operators:&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;small :: Int -&amp;gt; Bool
small n = 0 &amp;lt;= n &amp;amp;&amp;amp; n &amp;lt; 10
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;&lt;strong&gt;Functions between types&lt;/strong&gt;&lt;/p&gt;

&lt;p&gt;Haskell has no implicit conversion between types. You must use explicit functions:&lt;/p&gt;

&lt;p&gt;&lt;a href="https://res.cloudinary.com/practicaldev/image/fetch/s--cwu66rXI--/c_limit%2Cf_auto%2Cfl_progressive%2Cq_auto%2Cw_880/https://yosuf.dev/content/images/2021/07/image-1.png" class="article-body-image-wrapper"&gt;&lt;img src="https://res.cloudinary.com/practicaldev/image/fetch/s--cwu66rXI--/c_limit%2Cf_auto%2Cfl_progressive%2Cq_auto%2Cw_880/https://yosuf.dev/content/images/2021/07/image-1.png" alt="" width="880" height="189"&gt;&lt;/a&gt;&lt;/p&gt;

&lt;p&gt;To use the &lt;code&gt;ord&lt;/code&gt; and &lt;code&gt;chr&lt;/code&gt; functions, you must import the &lt;code&gt;Data.Char&lt;/code&gt; library module:&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;Prelude&amp;gt; floor 3.7
3

Prelude&amp;gt; ceiling 3.7
4

Prelude&amp;gt; :m + Data.Char

Prelude&amp;gt; ord 'a'
97

Prelude&amp;gt; chr 98
'b'
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;&lt;strong&gt;Functions of Char&lt;/strong&gt;&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;isDigit :: Char -&amp;gt; Bool
isDigit c = ’0’ &amp;lt;= c &amp;amp;&amp;amp; c &amp;lt;= ’9’
isUpper, isLower :: Char -&amp;gt; Bool
isUpper c = ’A’ &amp;lt;= c &amp;amp;&amp;amp; c &amp;lt;= ’Z’
isLower c = ’a’ &amp;lt;= c &amp;amp;&amp;amp; c &amp;lt;= ’z’
isAlpha :: Char -&amp;gt; Bool
isAlpha c = isUpper c || isLower c
ord :: Char -&amp;gt; Int
chr :: Int -&amp;gt; Char
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;Examples:&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;Prelude&amp;gt; :m + Data.Char

Prelude&amp;gt; isDigit '2'
True

Prelude&amp;gt; isLower 'B'
False

Prelude&amp;gt; ord '1'
49

Prelude&amp;gt; chr 65
'A'

Prelude&amp;gt; chr 48
'0'

Prelude&amp;gt; chr 32
' '

Prelude&amp;gt; chr (ord 'b')
'b'

Prelude&amp;gt; chr (ord 'b' + 3)
'e'
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;&lt;strong&gt;Functions on Float&lt;/strong&gt;&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;(+) :: Float -&amp;gt; Float -&amp;gt; Float
(-) :: Float -&amp;gt; Float -&amp;gt; Float
(*) :: Float -&amp;gt; Float -&amp;gt; Float
(/) :: Float -&amp;gt; Float -&amp;gt; Float
(^) :: Float -&amp;gt; Int -&amp;gt; Float
abs :: Float -&amp;gt; Float
negate :: Float -&amp;gt; Float
sin :: Float -&amp;gt; Float
asin :: Float -&amp;gt; Float
exp :: Float -&amp;gt; Float
log :: Float -&amp;gt; Float
sqrt :: Float -&amp;gt; Float
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;&lt;strong&gt;Guarded Definitions&lt;/strong&gt;&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;maxTwo :: Int -&amp;gt; Int -&amp;gt; Int
maxTwo x y
    | x &amp;gt;= y = x
    | otherwise = y
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;The Boolean expression &lt;code&gt;x &amp;gt;= y&lt;/code&gt; is called a guard. Guards are tested in order, so if we had:&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;maxThree :: Int -&amp;gt; Int -&amp;gt; Int -&amp;gt; Int
maxThree x y z
    | x &amp;gt;= y &amp;amp;&amp;amp; x &amp;gt;= z = x
    | y &amp;gt;= x &amp;amp;&amp;amp; y &amp;gt;= z = y
    | otherwise = z
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;it could be simplified to:&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;maxThree :: Int -&amp;gt; Int -&amp;gt; Int -&amp;gt; Int
maxThree x y z
    | x &amp;gt;= y &amp;amp;&amp;amp; x &amp;gt;= z = x
    | y &amp;gt;= z = y
    | otherwise = z
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;&lt;strong&gt;Conditional Expressions&lt;/strong&gt;&lt;/p&gt;

&lt;p&gt;Haskell also has an expression form: &lt;code&gt;if boolexp then exp1 esle exp2&lt;/code&gt; &lt;em&gt;.&lt;/em&gt;&lt;/p&gt;

&lt;p&gt;So instead of:&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;maxTwo :: Int -&amp;gt; Int -&amp;gt; Int
maxTwo x y
    | x &amp;gt;= y = x
    | otherwise = y
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;we could write:&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;max :: Int -&amp;gt; Int -&amp;gt; Int
max x y = if x &amp;gt;= y then x else y
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;You can also define functions using existing functions. So you could turn this:&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;maxThree :: Int -&amp;gt; Int -&amp;gt; Int -&amp;gt; Int
maxThree x y z
    | x &amp;gt;= y &amp;amp;&amp;amp; x &amp;gt;= z = x
    | y &amp;gt;= z = y
    | otherwise = z
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;into this:&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;maxThree x y z = maxTwo x (maxTwo y z)
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;&lt;strong&gt;Local Definitions&lt;/strong&gt;&lt;/p&gt;

&lt;p&gt;You can have local definitions which are further indented and must line up:&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;sumSquares :: Int -&amp;gt; Int -&amp;gt; Int
sumSquares n m
    = sqN + sqM
        where
        sqN = n*n
        sqM = m*m
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;You can use local definitions in all guards, as well as the right-hand expressions:&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;maxsq :: Int -&amp;gt; Int -&amp;gt; Int
maxsq x y
    | sqx &amp;gt; sqy = sqx
    | otherwise = sqy
    where
    sqx = sq x
    sqy = sq y

    sq :: Int -&amp;gt; Int
    sq z = z*z
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;h1&gt;
  
  
  &lt;strong&gt;Constrained polymorphism&lt;/strong&gt;
&lt;/h1&gt;

&lt;p&gt;For example, the “element” function takes two arguments and asks “does the first argument occur in the second argument (which is a structure of some kind)? And it works for multiple types:&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;Prelude&amp;gt; elem ‘e’ “Hello”
True

Prelude&amp;gt; elem 3 [1..5]
True
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;This function is almost polymorphic: the types of things being compared must support &lt;em&gt;equality testing&lt;/em&gt;, which is reflected in the constrained type of elem:&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;Prelude&amp;gt; :t elem
elem :: (Eq a, Foldable t) =&amp;gt; a -&amp;gt; t a -&amp;gt; Bool
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;Here, &lt;strong&gt;Eq a&lt;/strong&gt; is a constraint on the type of &lt;strong&gt;a&lt;/strong&gt;. The type &lt;strong&gt;a&lt;/strong&gt; must belong to the typeclass &lt;strong&gt;Eq&lt;/strong&gt; : those that support equality testing such as Char, Int and Integer.&lt;/p&gt;

&lt;h1&gt;
  
  
  &lt;strong&gt;Language Details&lt;/strong&gt;
&lt;/h1&gt;

&lt;p&gt;&lt;strong&gt;Indentation&lt;/strong&gt;&lt;/p&gt;

&lt;p&gt;In files, Haskell uses indentation to decide where another definition starts, so all definitions must have the same indentation. The following would be seen as two definitions and is legal:&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;square
    :: Integer -&amp;gt; Integer
square n = n * n    
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;The following are not legal, Haskell sees the first as one definition and the second as two definitions:&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;square :: Integer -&amp;gt; Integer
    square n = n * n

square
:: Integer -&amp;gt; Integer
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;






&lt;blockquote&gt;
&lt;p&gt;Subscribe to my &lt;a href="https://yosuf.substack.com/subscribe"&gt;Substack&lt;/a&gt; to be notified of future posts.&lt;/p&gt;
&lt;/blockquote&gt;

</description>
      <category>tech</category>
      <category>technical</category>
      <category>haskell</category>
    </item>
  </channel>
</rss>
