Introduction
·
A purely functional programming
language
- In purely functional programming you don't tell the computer what to do as such but rather you tell it what stuff is.
- The factorial of a number is the product of all the numbers from 1 to that number, the sum of a list of numbers is the first number plus the sum of all the other numbers, and so on.
- You express that in the form of functions.
- You also can't set a variable to something and then set it to something else later. If you say that a is 5, you can't say it's something else later because you just said it was 5. What are you, some kind of liar?
- So in purely functional languages, a function has no side-effects.
- The only thing a function can do is calculate something and return it as a result.
- At first, this seems kind of limiting but it actually has some very nice consequences: if a function is called twice with the same parameters, it's guaranteed to return the same result.
- That's called referential transparency and not only does it allow the compiler to reason about the program's behavior, but it also allows you to easily deduce (and even prove) that a function is correct and then build more complex functions by gluing simple functions together.
·
Lazy
- That means that unless specifically told otherwise, Haskell won't execute functions and calculate things until it's really forced to show you a result.
- That goes well with referential transparency and it allows you to think of programs as a series of transformations on data.
- It also allows cool things such as infinite data structures.
- Say you have an immutable list of numbers xs = [1,2,3,4,5,6,7,8] and a function doubleMe which multiplies every element by 2 and then returns a new list. If we wanted to multiply our list by 8 in an imperative language and did doubleMe(doubleMe(doubleMe(xs))), it would probably pass through the list once and make a copy and then return it. Then it would pass through the list another two times and return the result. In a lazy language, calling doubleMe on a list without forcing it to show you the result ends up in the program sort of telling you "Yeah yeah, I'll do it later!". But once you want to see the result, the first doubleMe tells the second one it wants the result, now! The second one says that to the third one and the third one reluctantly gives back a doubled 1, which is a 2. The second one receives that and gives back 4 to the first one. The first one sees that and tells you the first element is 8. So it only does one pass through the list and only when you really need it.
- That way when you want something from a lazy language you can just take some initial data and efficiently transform and mend it so it resembles what you want at the end.
·
Statically typed.
- When you compile your program, the compiler knows which piece of code is a number, which is a string and so on. That means that a lot of possible errors are caught at compile time. If you try to add together a number and a string, the compiler will whine at you.
- Haskell uses a very good type system that has type inference.
- That means that you don't have to explicitly label every piece of code with a type because the type system can intelligently figure out a lot about it. If you say a = 5 + 4, you don't have to tell Haskell that a is a number, it can figure that out by itself.
- Type inference also allows your code to be more general. If a function you make takes two parameters and adds them together and you don't explicitly state their type, the function will work on any two parameters that act like numbers.
·
Elegant and concise.
o Because it uses a lot of high
level concepts, Haskell programs are usually shorter than their imperative
equivalents. And shorter programs are easier to maintain than longer ones and
have less bugs.
·
Really smart guys
o Work on Haskell began in 1987
when a committee of researchers got together to design a kick-ass language. In
2003 the Haskell Report was published, which defines a stable version of the
language.
- A text editor and a Haskell compiler. You probably already have your favorite text editor installed so we won't waste time on that. For the purposes of this tutorial we'll be using GHC, the most widely used Haskell compiler. The best way to get started is to download the Haskell Platform, which is basically Haskell with batteries included.
- GHC can take a Haskell script (they usually have a .hs extension) and compile it but it also has an interactive mode which allows you to interactively interact with scripts. Interactively.
- You can call functions from scripts that you load and the results are displayed immediately. For learning it's a lot easier and faster than compiling every time you make a change and then running the program from the prompt.
- The interactive mode is invoked by typing in ghci at your prompt. If you have defined some functions in a file called, say, myfunctions.hs, you load up those functions by typing in :l myfunctions and then you can play with them, provided myfunctions.hs is in the same folder from which ghci was invoked. If you change the .hs script, just run :l myfunctions again or do :r, which is equivalent because it reloads the current script.
- The usual workflow for me when playing around in stuff is defining some functions in a .hs file, loading it up and messing around with them and then changing the .hs file, loading it up again and so on. This is also what we'll be doing here.
Haskell Classifications (Wikipedia)
- Paradigm: functional
- Paradigm: lazy/non-strict/call-by-need (vs eager/strict/call-by-value)
- Paradigm: modular
- Typing discipline: static (vs dynamic) (compile-time type checking vs run-time type checking)
- Typing discipline: strong (vs. weak) (e.g. Java/C/Python vs Perl/Javascript)
- Typing discipline: inferred (vs. manifest)
http://homepages.inf.ed.ac.uk/wadler/topics/call-by-need.html
Evaluation Strategies
- 1 Strict evaluation
- 2 Non-strict evaluation
- 2.1 Normal order
- 2.2 Call by name
- the arguments to a function are not evaluated before the function is called — rather, they are substituted directly into the function body (using capture-avoiding substitution) and then left to be evaluated whenever they appear in the function
- 2.3 Call by need
- a memoized version of call-by-name
- Haskell is the most well-known language that uses call-by-need evaluation. R also uses a form of call-by-need. .NET languages can simulate call-by-need using the type
Lazy<T>
. - 2.4 Call by macro expansion
- 3 Nondeterministic strategies
From http://en.wikipedia.org/wiki/Comparison_of_programming_languages:
Language | Type strength | Type safety | Expression of types | Compatibility among composite types | Type checking |
---|---|---|---|---|---|
C# | strong | safe[TS 4] | explicit | name-based | static[TS 5] |
Clojure | strong | safe | implicit with optional explicit typing | dynamic | |
Erlang | strong | safe | implicit | dynamic | |
F# | strong | safe | implicit | name-based | static |
Fortran | strong | safe | explicit | name-based | static |
Go[26] | strong | safe | implicit with optional explicit typing | property-based | static |
Groovy | strong | safe | implicit with optional explicit typing | dynamic | |
Haskell | strong | safe | implicit with optional explicit typing | property-based | static |
Java | strong | safe[27] | explicit | name-based | static |
JavaScript | weak | implicit | dynamic | ||
Objective-C | weak | safe | explicit | name-based (subclassing) and property-based (protocols) | dynamic with optional static typing[28] |
OCaml | strong | safe | implicit with optional explicit typing | property-based | static |
Perl 6 | partially implicit[TS 8] | dynamic with optional static typing | |||
PHP | weak | implicit | dynamic | ||
Python | strong | safe | implicit | property-based | dynamic |
Ruby | strong | safe | implicit | property-based | dynamic |
Scala | strong | safe | partially implicit (local type inference) | name-based (subclassing) and property-based (structural) | static |
Scheme | strong | implicit | dynamic (latent) | ||
Smalltalk | strong | safe | implicit | dynamic | |
Standard ML | strong | safe | implicit with optional explicit typing | property-based | static |
Visual Basic .NET | strong | unsafe[TS 6] | explicit | static | |
No comments:
Post a Comment