Declarative Languages

Slides:



Advertisements
Similar presentations
Introduction to Programming
Advertisements

Chapter 8 Technicalities: Functions, etc. Bjarne Stroustrup
Pass by Value. COMP104 Pass by Value / Slide 2 Passing Parameters by Value * A function returns a single result (assuming the function is not a void function)
The Assembly Language Level
 Introduction to Programming History of programming.
Programming Logic and Design, Third Edition Comprehensive
Week 9: Methods 1.  We have written lots of code so far  It has all been inside of the main() method  What about a big program?  The main() method.
1.3 Executing Programs. How is Computer Code Transformed into an Executable? Interpreters Compilers Hybrid systems.
Python. What is Python? A programming language we can use to communicate with the computer and solve problems We give the computer instructions that it.
Games and Simulations O-O Programming in Java The Walker School
Introduction to Python and programming Michael Ernst UW CSE 190p Summer 2012.
Shorthand operators.
Python quick start guide
ICAPRG301A Week 4Buggy Programming ICAPRG301A Apply introductory programming techniques Program Bugs US Navy Admiral Grace Hopper is often credited with.
Computing Science 1P Lecture 21: Friday 20 th April Simon Gay Department of Computing Science University of Glasgow 2006/07.
Invitation to Computer Science, Java Version, Second Edition.
Programming for Beginners Martin Nelson Elizabeth FitzGerald Lecture 13: An Introduction to C++
Abstraction IS 101Y/CMSC 101 Computational Thinking and Design Tuesday, September 17, 2013 Marie desJardins University of Maryland, Baltimore County.
CS 320 Assignment 1 Rewriting the MISC Osystem class to support loading machine language programs at addresses other than 0 1.
Pseudocode Simple Program Design Third Edition A Step-by-Step Approach 2.
COMP 171: Data Types John Barr. Review - What is Computer Science? Problem Solving  Recognizing Patterns  If you can find a pattern in the way you solve.
CMP-MX21: Lecture 4 Selections Steve Hordley. Overview 1. The if-else selection in JAVA 2. More useful JAVA operators 4. Other selection constructs in.
ICAPRG301A Week 2 Strings and things Charles Babbage Developed the design for the first computer which he called a difference engine, though.
Useful IDLE Alt-ENTER Commenting-out. Writing a Program CMSC 120: Visualizing Information 2/14/08.
CIS 234: Java Methods Dr. Ralph D. Westfall April, 2010.
Expressions and Order of Operations Operators – There are the standard operators: add, subtract, divide, multiply – Note that * means multiply? (No times.
Announcements Assignment 2 Out Today Quiz today - so I need to shut up at 4:25 1.
Code Optimization.
Prof: Dr. Shu-Ching Chen TA: Samira Pouyanfar Spring 2017
Basic 1960s It was designed to emphasize ease of use. Became widespread on microcomputers It is relatively simple. Will make it easier for people with.
High and low level languages
Lesson #6 Modular Programming and Functions.
Algorithmic complexity: Speed of algorithms
Lesson #6 Modular Programming and Functions.
Introduction to Computer Science / Procedural – 67130
What is Programming? Telling a computer what to do, using a language you write in simple text files or at a command prompt. With it, you can do anything.
The what of variables Variables are the combination of an identifying label and a value, often a literal like 2 or "hello world". The label/identifier.
Statement atoms The 'atomic' components of a statement are: delimiters (indents, semicolons, etc.); keywords (built into the language); identifiers (names.
Functions CIS 40 – Introduction to Programming in Python
Programming for Geographical Information Analysis: Core Skills
Repeating code We could repeat code we need more than once: i = 1 print (i) i += 1 print (i) #… stop when i == 9 But each line means an extra line we might.
Procedural Abstraction Object-Oriented Code
Lesson #6 Modular Programming and Functions.
Object Oriented Programming (OOP) LAB # 8
Functional.
Learning to Program in Python
Conditions and Ifs BIS1523 – Lecture 8.
Number and String Operations
Portability CPSC 315 – Programming Studio
Lesson 16: Functions with Return Values
Lecture 23 Pages : Separating Syntactic Analysis from Execution. We omit many details so you have to read the section in the book. The halting.
Lesson Objectives Aims Key Words Compiler, interpreter, assembler
Java Programming Loops
Coding Concepts (Basics)
Topic 3-a Calling Convention 1/10/2019.
Coding Concepts (Data- Types)
Algorithmic complexity: Speed of algorithms
Programming for Geographical Information Analysis: Core Skills
Programming Techniques
Lesson #6 Modular Programming and Functions.
Java Programming Loops
Algorithmic complexity: Speed of algorithms
CS1100 Computational Engineering
Scope Scope is the space within which a variable label exists and can be used. Python talks of names being bound to the code in an area and that determining.
Functions So far we've been using functions from the builtin module, which is automatically loaded in most cases. Occasionally we've accessed functions.
Functions Functions being objects means functions can be passed into other functions: sort (list, key=str.upper)
Control Flow statements
Lecture 18 Compilers and Language Translation (S&G, ch. 9)
Enclosing delimiters Python uses three style of special enclosing delimiters. These are what the Python documentation calls them: {} braces # Sometimes.
Review We've seen that a module is a file that can contain classes as well as its own variables. We've seen that you need to import it to access the code,
Presentation transcript:

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.

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.

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.

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.

Functional programming In general, Python is functional-ish in places. The functional elements concentrate on the application of functions directly to e.g. sequences: https://docs.python.org/3/howto/functional.html

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)

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.

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

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