User:Mchinth/sandbox

PureScript is a Functional programming language which compiles to JavaScript. JavaScript does not support powerful abstractions, as a result it cannot utilize full capability of Functional Programming. PureScript addresses this issue by allowing expressive, readable code. PureScript is a statically typed language. Unlike in other dynamically typed languages, in PureScript, types exist only at compile time, and have no representation at runtime. Programs which are correct will be given a type by the compiler, which illustrates its behavior. Conversely, programs which are not given any type are incorrect.

Getting started
Firstly, Setup a working environment for PureScript development. Following are the tools required: Download latest version of PureScript binary from Github and setup the environment variables. Try executing the below command in command line to test: Following command is to install the Pulp command line tool: Now you have all tools required to write first PureScript library.
 * psc - PureScript Compiler
 * npm -Node package Manager
 * Pulp - Command line tool

Simple Hello World!

By using pulp init command, create project in an empty directory

Two directories src ,test and a configuration file bower.json are created. src directory contains source files and test directory contains tests performed.

Now Modify src/Main.purs file to create Hello World Program. Now let's build and run the above code.

Features
PureScript is a strong and statically typed compile-to-JS language with a number of interesting features, such as:

Types and Type Inference
Types in PureScript are inspired from ML and Haskell and are not similar to the types normally seen in languages like Java or C#, although they serve the same purpose at a high level. Type system in purescript supports type inference. Following is a simple example code which defines a number, but there is no reference to the Number type anywhere in the code.

Pattern matching
pattern matching is a technique that allows to code compact functions which express complex ideas. Algebraic data types is a similar feature and is closely related to pattern matching. Below is a Simple Pattern Matching function that calculates GCD of two numbers using Euclidean Algorithm. Pattern matching allows to code simple and declarative cases. Functions work by pairing sets with results. Each line in the function is a case, expressions on left of equals sign are patterns. Each case is evaluated and the first case which matches is returned.

There are various types of patterns like Integer literal, Variable, Wildcard and Boolean literal patterns.

Simple FFI
FFI(Foriegn Function Interface) is a feature provided by PureScript that enables communication between PureScript and JavaScript code. FFI is very flexible and it allows developer to choose between using foreign functions or type system. JavaScript functions might return not null values which have to be handled by the developer when designing these interfaces.

Call the above PureScript module from javascript, using FFI.

Modules
Module keyword is used to refer to modules and all the code in PureScript is written in a module. When a module is imported(using import keyword) aliases are created for all values and types in it. Values, type constructors and data structures can be explicitly imported. Below is an example to import type constructors by passing a list of data constructors in parentheses. Modules can be exported by providing a set of names in parenthesis in the declaration similar to import.

Type Classes
Type classes is a powerful kind of abstraction provided by PureScript's type system. Following is the example of show, which is defined by type class in the Prelude module called Show.

Below are some some standard type classes defined in the Prelude and standard libraries.

Eq: Eq type class is used to test two values for equality.

class Eq a where eq :: a -> a -> Boolean

Ord: The Ord type class is used to compare two values for types which support ordering.

data Ordering = LT | EQ | GT

class (Eq a) <= Ord a where compare :: a -> a -> Ordering

Num: The Num type class is used to identify the types which support addition, subtraction, multiplication and division operations. class (DivisionRing a) <= Num a

DO Notation
Do notation is a special syntax which is used to improve the readability of the code. It uses the keyword Do to introduce a block of code which uses do notation. It makes the structure of algorithm clearer. Following is the factors function written using do notation.

factors :: Int -> Array (Array Int) factors n = filter (\xs -> product xs == n) $ do i <- 1 .. n j <- i .. n return [i, j]

Functions and Records
Functions and records are the building blocks of PureScript.

Simple Types
PureScript defines seven built-in types: numbers, strings, booleans, integers, arrays, records, and functions which are defined in Prim module (implicitly imported in every module). These can be seen in PSCi by using the :type command. Following are a few examples.

Unlike JavaScript, all the elements of PureScript array should be of same type.

Records are PureScript's objects and the syntax of Record literals is same as that of JavaScript's Object literals.

Dot is used to access the fields of records.

Functions
Functions can be defined inline ( using \ followed by space) or they can be defined at the top-level of file (by specifying arguments before equal sign).

Version History
Following table is based on the information provided in Github. {| class="wikitable" style="text-align:center;" ! Version !! Release date !! Equivalent to !! Changes !! Enhancements !! Bug Fixes Naming scheme of prelude modules is similar to haskell || Pretty printing for row types has been improved ||Overlapping variables in typeclass instances are rejected A bug in the optimizer related to inlining was fixed Runtime type checks has been removed||Tail call optimization has been improved for functions using case expressions. Generated code for type class dictionaries was simplified||Binding group errors in type class members are now caught at compile time. The body of a guarded expression must now be indented past the guard. ||The body of a guarded expression must now be indented past the guard. Import instances when referencing qualified values || The operators in the Bits type class have been renamed. ||Relax indentation requirements || Fixed a bug in the typechecker. Syntactic sugar has been introduced for object update functions||Some command line options were fixed in psc-make Some module import errors were fixed
 * || Apr 27, 2014|| ||Block support has been removed
 * || Apr 27, 2014|| ||Block support has been removed
 * || Aug 3, 2014 || || || ||Provides bug fixes and features required to compile the latest core libraries
 * || Sep 2, 2014 || || || ||
 * || Sep 21, 2014 || || || ||
 * || Sep 22, 2014 || || || ||
 * || Sep 22, 2014 || || || ||
 * || Oct 6, 2014 || || || ||
 * || Oct 29, 2014 || || || ||
 * || Oct 30, 2014 || || || ||
 * || Nov 8, 2014 || ||Alternative type class hierarchy has been refactored
 * || Sep 22, 2014 || || || ||
 * || Oct 6, 2014 || || || ||
 * || Oct 29, 2014 || || || ||
 * || Oct 30, 2014 || || || ||
 * || Nov 8, 2014 || ||Alternative type class hierarchy has been refactored
 * || Oct 30, 2014 || || || ||
 * || Nov 8, 2014 || ||Alternative type class hierarchy has been refactored
 * || Nov 8, 2014 || ||Alternative type class hierarchy has been refactored
 * || Nov 8, 2014 || ||Alternative type class hierarchy has been refactored
 * || Nov 9, 2014 || || || ||Prevent psci and psc-make from rebuilding everything on every build
 * || Nov 17, 2014 || ||The body of a guarded expression must now be indented past the guard.
 * || Nov 17, 2014 || ||The body of a guarded expression must now be indented past the guard.
 * || Nov 17, 2014 || ||The body of a guarded expression must now be indented past the guard.
 * || Nov 19, 2014 || ||The pipe symbol is now a reserved operator.
 * || Nov 19, 2014 || ||The pipe symbol is now a reserved operator.
 * || Nov 24, 2014 || || || ||
 * || Nov 28, 2014 || ||Command line options with multiplicity 1 now require an equals symbol||Use optparse-applicative instead of cmdtheline ||
 * || Jan 8, 2015 || || ||There is a new "core functional representation", which will enable certain optimizations, and new features such as rewrite rules.||Case statement at end of Eff block not being executed.
 * || Jan 23, 2015 || || || ||Fix some precedence issues in the code generator.
 * || Jan 8, 2015 || || ||There is a new "core functional representation", which will enable certain optimizations, and new features such as rewrite rules.||Case statement at end of Eff block not being executed.
 * || Jan 23, 2015 || || || ||Fix some precedence issues in the code generator.
 * || Jan 23, 2015 || || || ||Fix some precedence issues in the code generator.
 * || Jan 23, 2015 || || || ||Fix some precedence issues in the code generator.
 * || Feb 3, 2015 || || || ||Various small bug fixes.
 * || Feb 8, 2015 || || ||Lightweight record constructors are now supported
 * || Feb 8, 2015 || || ||Lightweight record constructors are now supported
 * || Feb 8, 2015 || || ||Lightweight record constructors are now supported
 * || Feb 8, 2015 || ||The syntax of record getters was changed to _.prop ||The record part of a record updater can now be made into a wildcard ||Newline issues were fixed in executables
 * || Feb 12, 2015 || || || ||
 * || Oct 30, 2014 || || || ||
 * || Oct 30, 2014 || || || ||
 * || Oct 30, 2014 || || || ||
 * || Oct 30, 2014 || || || ||
 * || Oct 30, 2014 || || || ||
 * || Oct 30, 2014 || || || ||