Presentation is loading. Please wait.

Presentation is loading. Please wait.

Declarative Languages

Similar presentations


Presentation on theme: "Declarative Languages"— Presentation transcript:

1 Declarative Languages
Declarative Languages usually describe the end point first and the program works to get there. One subset of these are Functional Languages where one passes one procedure to another in ever more complex nesting in order to calculate some end result defined by the first layer of nesting. Generally stateful elements like variables are discouraged; the code is constructed from expressions rather than full statements. To return to declarative languages… some declarative languages are also Fifth Generation Languages, though frequently they are not as they involved coding. The general idea is the same though – to describe what you want to end up with, and work from that to generate the solution.

2 text = input("Type a number to add to four")
value = int(text) answer = 4 + value print (answer) print ( 4 + int( input( "Type a number to add to four“ ) Generally stateful elements like variables are discouraged; the code is constructed from expressions rather than full statements. In the above, for example (which is a functional version of the original shown in grey), we start with the fact that we want to print something, and work down adding code to generate the components of the solution. Note, in particular, that we have got rid of the stateful elements – the variables. There is not record of the answers beyond their use (so called “side effects”). This cuts out the opportunity for code elsewhere to alter the state of the solution in error, but also makes coding much harder. It doesn’t especially matter if you don’t follow this now, we will come back to it.

3 Method chaining In some languages (like JavaScript) altered objects are commonly returned from method calls. a = b.method() This means you can then call methods inside those objects, and start to create chains: c = a.method2() Becomes: c = b.method().method2() This saves making unnessary labels, so is more efficient, but sometimes needs pulling appart to debug. Python tends not to return such objects, so this is less common, but it is still worth knowing just incase you see it.

4 Python Functional languages are increasingly popular as everything is contained within the nesting; this means it can be split up easily so different nests run on different processors on machines with more than one processor (which is usual now). Python is a third generation imperative language, but with functional bits. Such hybrids are generally the direction of travel. Most languages are converging on something which is a mix of functional and imperative programming. For example, R, a functional language, comes across as very imperative, whereas Java, which is very imperative by tradition, has introduced many functional elements. Other declarative programming styles have had less effect.

5 Functional programming
In general, Python is functional-ish in places. The functional elements concentrate on the application of functions directly to e.g. sequences:

6 Functions as objects In Python, pretty much everything is an object, including functions. This means you can do this: def f(): pass a = f a() print(a)

7 Efficiency Indeed, it is more efficient. If we do: a = B() a.c() This needs to construct a new reference to the function c each it is called. If we do this: d = a.c # Note lack of parentheses when talk about the name. d() We just use the old call. Function calls are amongst the most processor intensive standard language elements, so this can make considerable savings.

8 Function 'fun' This means it is possible to assign functions to objects. (With the types library it is also possible to assign them to classes currently being used) class A(): def prt(self): print("hi") def prrrt(): print("changed") a = A() a.prt() a.prt = prrrt

9 Now I am become Death, the destroyer of prints
>>> print = 0 >>> print() TypeError: 'int' object not callable.


Download ppt "Declarative Languages"

Similar presentations


Ads by Google