Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Preventing incorrect use (Modules, encapsulation and smart constructors)
We made a few changes in these commits. 1. Moved the Html generation library to its own module 2. Added the escape function to escape characters Each Haskell source file is a module. The module name should have the same name as the source file and should start with a capital letter. Sub-directories should also be part of the name and we use `.` do denote a sub-directory. We'll see that in the next commit. The only exception to the rule are entry points to the program - modules with the name 'Main' that define `main` in them. They source file names could have any name they want. A module declaration looks like this: ```hs module <module-name> ( <export-list> ) where ``` The export list can be omitted if you want to export everything defined in the module, but we don't. We will list exactly the functions and type we want to export. Note that we do not export the constructors for our new types, only the types themselves. If we wanted to export the constructors as well we would've written `Html(Html)` or `Html(..)`. Now, anyone importing our module (using the `import` statement which can be used below module declarations but above any other declaration), will only be able to import what we export. We also add a new function, `escape`, which will convert usage of characters that may conflict with our meta language html, with safer options that will not. See https://stackoverflow.com/questions/7381974/which-characters-need-to-be-escaped-in-html In `escape` we see two new things: 1. let expressions - we can define local names using this syntax: ```hs let <name> = <expression> in <expression> ``` This will make <name> available as a variable in the second <expression>. 2. Pattern matching with multiple patterns - we match on different characters and convert them to a string. Note that `_` is a "catch all" pattern that will always succeed. 3. Strings are linked lists of char - String is defined as: `type String = [Char]`, so we can use the same functions we used on lists, namely map and concat. Now we can use our tiny html library safely. But what if the user wants to use our library with something we didn't think about, for example adding unordered lists? We are completely blocking them from extending our library. We'll talk about this next.
- Loading branch information