Skip to content
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

Open
wants to merge 1 commit into
base: development
Choose a base branch
from

Conversation

lbseale
Copy link
Contributor

@lbseale lbseale commented Nov 5, 2021

Add types module

Covering (for now)

  • Static Type
  • Type Inference
  • Bools, Ints, Floats, Chars, Text
  • Why we don't use String

This is just the start, I plan to add more details

Submitter checklist

- 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
@lbseale lbseale changed the title Add Basics/TYpes Add Basics/Types Nov 5, 2021
negativeInteger :: Integer
negativeInteger = -37
```

Copy link
Contributor

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.

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

👍

Copy link

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?

Copy link
Contributor

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 to 18446744073709551615
  • In the case of Natural, an arithmetic underflow exception is thrown

```haskell
someText :: Text
someText = "Let's learn Haskell"
```
Copy link
Contributor

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?

Copy link
Contributor Author

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
Copy link

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Suggested change
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
Copy link

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
Copy link

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
Copy link

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`
Copy link

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Suggested change
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 )
```

Copy link

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`.
Copy link

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?

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

3 participants