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

Ch3 type annotation operator #214

Open
Ding-Fan opened this issue Sep 14, 2020 · 2 comments
Open

Ch3 type annotation operator #214

Ding-Fan opened this issue Sep 14, 2020 · 2 comments

Comments

@Ding-Fan
Copy link

When I read this paragraph:

...
If we try to incorrectly define a value of type List (by using the type annotation operator :: ), we will see a new type of error:

> import Data.List
> Nil :: List
In a type-annotated expression x :: t, the type t must have kind Type

...

I don't know the usage of "type annotation operator". So I search the internet and found this:

A type signature is formed by the name of the function, two colons and then a type
-- http://www.cse.chalmers.se/edu/year/2015/course/TDA452_Functional_Programming/haskell-syntax.html#Type%20signatures

In the code example, Nil confused me. I don't know what is Nil in purescript. It doesn't look like a random function name. Is it a function?

In JavaScript, null is a value:

The value null represents the intentional absence of any object value. It is one of JavaScript's primitive values and is treated as falsy for boolean operations.
-- https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/null

In Ruby, nil is a value:

Well, nil is a special Ruby object used to represent an “empty” or “default” value.
-- https://www.rubyguides.com/2018/01/ruby-nil/

@Ding-Fan
Copy link
Author

Ding-Fan commented Sep 15, 2020

About where and indentation, in this chapter:

example x y z = foo + bar
  where
    foo = x * y
    bar = y * z

But in test/no-peeking/Solutions.purs

...
  where
  filterEntry :: Entry -> Boolean
  filterEntry e = e.address.street == streetName
...

Is it wrong?

@milesfrain
Copy link
Member

Thanks for the feedback.

I don't know the usage of "type annotation operator"

We could should clarify this more by starting of with some examples of types and type annotations. Note that these type annotations are essentially the same as what you get in Elm and TypeScript (but we use two colons :: instead of one :).

I don't know what is Nil in purescript. It doesn't look like a random function name. Is it a function?

It's a data constructor, so you can think of it as a function (but it also allows pattern matching too). In this case Nil behaves like a function of zero arguments, and when you call it, you get an empty list. It's similar to Maybe's Nothing. We should explain Maybe earlier in the book to clear this up. Some additional links:

https://egghead.io/lessons/purescript-basic-data-constructors-in-purescript

(examples would be helpful here):
https://pursuit.purescript.org/packages/purescript-lists/5.4.1/docs/Data.List.Types#v:Nil
https://pursuit.purescript.org/packages/purescript-maybe/4.0.1/docs/Data.Maybe#v:Nothing

About where and indentation

Wow, good catch! That section on indentation needs to be updated. Indentation of where doesn't matter, and purty does not indent these (although I wish it did). purty is the autoformatter.

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

No branches or pull requests

2 participants