Another super basic question (Syntax)


#1

One thing I am struggling through is I have no programming experience so every symbol confuses me. I am doing a lot of external research but there are a number I still wonder about. I am a fast learner, but I get held up by small stuff like that. I’ve been reading through the syntax lists, but they are not comprehensive for all the syntax.
Thanks Elm Community.

-Samuel


#2

I think perhaps you forgot the question? Which symbols are confusing you?


#3

I suppose I was asking more for resource lists if anyone knows. But yes, you make fair point… :wink: I am looking for explanation in how they function and why.

Symbols:
(bar or pipe): |
(arrow): ->
(colon) :
(meaning) div
(or just this symbol(s)) [ ]


#4

I don’t know of any comprehensive resources for this other than the official syntax reference. I actually started on a syntax guide for Reason once, before promptly forgetting about it, but I think something like that for Elm would be a great idea that someone should do :wink:

I can at least try to explain these symbols though.

Most of these symbols don’t have any deep meaning in themselves, but are just used as separators in various syntactic patterns. The reason they are different are usually to avoid ambiguous syntax where it’s difficult for both humans, but especially for computers to determine what kind of syntactic construct a symbol belongs to without taking into account a bigger context, which is often computationally expensive. The choice of each specific symbol is often historical, but also tend to work very well for humans in the contexts they are used.

  • The pipe, |, is used in a couple of places, with different meanings. Most importantly to separate the variants of custom types: type Foo = A | B. But it’s also used in the record update syntax to separate the name of the record to be updated from the field names: { oldRecord | aField = 2 }

  • The arrow, -> is also used in several places with different meanings. In type annotations it separates the argument of a function from its return type: Int -> Float. In anonymous functions it separates the argument list from its body: \n m -> n + m. And in case expressions it separates the pattern from the body of each branch: case foo of pattern -> expression.

  • colon, :, has only a single use I think: to separate a name from its type annotation: someNumber : Int

  • square brackets, [], are just the delimiters of a list, marking where it begins and ends: [1, 2, 3]. If there’s nothing in between the delimiters, [], it’s just an empty list.

  • Lastly, div is just a name like any other. and you can assign whatever you want to it. But odds are that the example you’re looking at imports it from the Html module where it is a function that returns an Html value representing an HTML element that is also conveniently named div. HTML is what you’d use to create an HTML document and make exciting things happen in your web browser.

I hope that helps, but please let me know if you still have questions.


#5

Thank you so much for your time and help. You are a sir; cheers.