-
Notifications
You must be signed in to change notification settings - Fork 11
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Add Basics/Types #41
base: development
Are you sure you want to change the base?
Add Basics/Types #41
Conversation
- Merge of types-01 commit 5100108 Author: Luke Seale <[email protected]> Date: Fri Nov 5 06:42:35 2021 -0700 Add types module Covering - Static Type - Type Inference - Bools, Ints, Floats, Chars, Text - Why we don't use String
negativeInteger :: Integer | ||
negativeInteger = -37 | ||
``` | ||
|
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Any chance you could add a little something about Word
? I'm especially interested in promoting it as a type that can't be negative, instead of using Int
and hoping for the best. It's also the occasion to evoke the correct by construction paradigm.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
👍
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Consider mentioning Natural
instead of or together with Word
?
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
And the fact that arithmetic underflows are handled differently:
When doing 0 - 1
- Since
Word
is modular, it loops back to18446744073709551615
- In the case of
Natural
, anarithmetic underflow
exception is thrown
```haskell | ||
someText :: Text | ||
someText = "Let's learn Haskell" | ||
``` |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Could you add a couple of examples for Text
? Especially the functions that do not exist in base
for String
?
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Yes, great idea
## Static Typing | ||
|
||
Haskell is a statically-typed programming language. This means that the type | ||
of all data must be known before the program is compiled. Some imperative |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
of all data must be known before the program is compiled. Some imperative | |
of all data must be known before the program is executed. Some |
Instead of declaring the type of every single function and variable, Haskell | ||
can *infer* the type of values based on just a few declarations. | ||
|
||
Typically, you just need to specify the type of a function. The types of all |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Since this isn't actually required, consider saying something like this instead:
while Haskell does not require type annotations in most cases, it is good practice to annotate the types of top-level definitions. This adds an extra layer of documentation, and safety where the Haskell compiler can verify our intent (as expressed in the type annotation) matches the code.
## Type Declarations | ||
|
||
In Haskell, you declare types on a separate line from where they are used. | ||
Even though I said above that you usually only declare types for functions, in |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
You can avoid this line by saying "top-level definitions"
Type inference is a killer feature of Haskell, and once you get used to it, you | ||
won't want to work without it. | ||
|
||
## Type Declarations |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Consider changing this to "type signatures" or "type annotations", so it won't be confused with declaring new types (using data
/newtype
/etc)
``` | ||
|
||
The fact that `Int`'s size is limited means it is subject to overflows. These | ||
occur without warning, so if they are a concern, use `Integer` |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
occur without warning, so if they are a concern, use `Integer` | |
occur without warning, so if they are a concern, use `Integer`. |
|
||
import Data.Text ( Text ) | ||
``` | ||
|
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Maybe add "we'll explain why this is needed later" or something similar?
|
||
`Text` is more convenient, performant and capable than `String`. We want | ||
beginners to avoid the mistakes of the past and start off with good | ||
practices, so we have chosen to teach `Text`. |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Maybe this would be a good place to mention that text lives in a different package/module, what overloadedstrings does, and how to convert String <-> Text?
Add types module
Covering (for now)
This is just the start, I plan to add more details
Submitter checklist