程序代写代做代考 Lambda Calculus data structure COMP0020: Functional Programming Miranda

COMP0020: Functional Programming Miranda
COMP0020 Functional Programming Lecture 4
Miranda
(and her friend Amanda)
Christopher D. Clack (University College London) COMP0020: Functional Programming Academic Year 2019-2020 1 / 14

COMP0020: Functional Programming Miranda
Contents
Miranda/Amanda Miranda demonstration Comments
Legal names and binding Types and type checking Tuples
Simple functions
Christopher D. Clack (University College London)
COMP0020: Functional Programming
Academic Year 2019-2020
2 / 14

COMP0020: Functional Programming Miranda
Miranda / Amanda
Miranda will run on Unix variants such as Linux, OS X / MacOS and Cygwin
For Windows, you can choose either to install Cygwin on Windows and then install Miranda on
Cygwin or you can try installing Amanda
Amanda is a PC version of Miranda
Amanda is almost (but not quite) the same as Miranda !
Get it from the link on Moodle – although it hasn’t been tested on the latest version of Windows.
Christopher D. Clack (University College London) COMP0020: Functional Programming Academic Year 2019-2020 3 / 14

COMP0020: Functional Programming Miranda
Miranda Installation on OS X
OS X’s System Integrity Protection (SIP) feature (El Capitan +) prevents changes to /usr (but not /usr/local).
Miranda installs into /usr/bin and /usr/lib by default, Thus, to install Miranda on OS X you must either :
􏰉 (i) force installation in a different place such as /usr/local/bin etc, or
􏰉 (ii) disable SIP, install Miranda in the standard place, then re-enable SIP
The first way is better : it doesn’t require rebooting OS X and there is no danger of forgetting to re-enable SIP. But the procedure is slightly ugly :
􏰉 tar xpf mira-2044-x86_64-Darwin.tar -C /usr/local
􏰉 sudo ln -s /usr/local/usr/local/bin/mira /usr/local/bin/mira
Christopher D. Clack (University College London) COMP0020: Functional Programming Academic Year 2019-2020 4 / 14

COMP0020: Functional Programming Miranda
Miranda Demonstration
Interpretive environment Simple use as a calculator
Simple definitions stored in a file — default name “script.m” (the file “script.x” contains the intermediate code that is interpreted)
Main definition — the function called “main” is special in Amanda but not in Miranda Define and use functions
Christopher D. Clack (University College London) COMP0020: Functional Programming Academic Year 2019-2020 5 / 14

COMP0020: Functional Programming Miranda
Miranda Demonstration
Similar to the Lambda Calculus
Miranda : (3+4) ∗ (6+7) But (inside a program file) you can give names to expressions
x =3+4
y =6+7 main = (x ∗ y)
Also give names to functions (no lambdas !)
inc x = x + 1 main = inc 56
Christopher D. Clack (University College London) COMP0020: Functional Programming Academic Year 2019-2020 6 / 14

COMP0020: Functional Programming Miranda
Comments
VERY important !
Use them from the start Example :
||a simple definition for some text :
message = ”hello mum”
||here is a function which adds one to a number : inc x = x + 1 ||another comment here
Christopher D. Clack (University College London)
COMP0020: Functional Programming
Academic Year 2019-2020
7 / 14

COMP0020: Functional Programming Miranda
Legal names
BINDING :
􏰉 NAME = EXPRESSION
􏰉 funcname argname = EXPRESSION
⋆ Binds funcname when defined (static)
⋆ Binds argname when applied to an argument (dynamic)
􏰉 Each binding is unique, within specified scope
⋆ Scope of argname is the function body (only)
⋆ Nested scopes (see later) permit nested bindings for the same name (as we have seen previously, the
λ-calculus supports such name clashes, but they are generally not a good idea) Names MUST start with an alphabetic character
Names MUST NOT start with a capital letter
􏰉 and thereafter may contain uppercase and lowercase characters, numbers and underscores
Christopher D. Clack (University College London) COMP0020: Functional Programming Academic Year 2019-2020 8 / 14

COMP0020: Functional Programming Miranda
Types
Data can be, for example :
􏰉 Numbers (42) is of type num
􏰉 Characters (’A’) is of type char
􏰉 Text (“string”) is of type [char]
􏰉 Truth values (True) is of type bool
􏰉 Functions are of type argtype -> resulttype. E.g. the function (f x = x + 1) is of type num -> num
Types help organize data and programs better Helps detect errors – the type system is a debugger
Christopher D. Clack (University College London) COMP0020: Functional Programming Academic Year 2019-2020 9 / 14

COMP0020: Functional Programming Miranda
Type Checking and Type Inference
Performed at compile time, before the program is run
Checks that operators (e.g. +) are executed on data of the correct type
Checks that functions are applied to data of the correct type
On the command line you can ask “what type is this ?”
In a program you can specify “this is a Boolean value” etc.
If types are not given, Miranda will attempt to INFER types from the way that variable names are used
Christopher D. Clack (University College London) COMP0020: Functional Programming Academic Year 2019-2020 10 / 14

COMP0020: Functional Programming Miranda
Tuples
A simple data structure
(“Sheila Bloggs”, 23, “2 Turnabout Road, NW3”, 60, False)
(34, True) is of type (num, bool)
(34, True) DOES NOT EQUAL (True, 34)
(“increment”, (+1), inc) is of type ([char], num->num, num->num)
Christopher D. Clack (University College London) COMP0020: Functional Programming Academic Year 2019-2020 11 / 14

COMP0020: Functional Programming Miranda
Simple Functions
inc : : num -> num inc x = x + 1
|| the hello function
hello : : num -> [char]
hello x = “good morning”, if (x<10) =“goodbye”, otherwise Christopher D. Clack (University College London) COMP0020: Functional Programming Academic Year 2019-2020 12 / 14 COMP0020: Functional Programming Miranda Summary Summary Miranda/Amanda Miranda demonstration Comments Legal names and binding Types and type checking Tuples Simple functions Christopher D. Clack (University College London) COMP0020: Functional Programming Academic Year 2019-2020 13 / 14 COMP0020: Functional Programming Miranda Summary END OF LECTURE Christopher D. Clack (University College London) COMP0020: Functional Programming Academic Year 2019-2020 14 / 14