RPAL Function definitions

Slides:



Advertisements
Similar presentations
User Defined Functions
Advertisements

1 Advanced C Programming from Expert C Programming: Deep C Secrets by Peter van der Linden CIS*2450 Advanced Programming Concepts.
Writing functions in OCaml. Defining a simple function # let add (x, y) = x + y;; val add : int * int -> int = Notice what this says: –add is a value.
Programming Languages and Paradigms
Structures Spring 2013Programming and Data Structure1.
Chapter 7 User-Defined Methods. Chapter Objectives  Understand how methods are used in Java programming  Learn about standard (predefined) methods and.
CS 355 – PROGRAMMING LANGUAGES Dr. X. Apply-to-all A functional form that takes a single function as a parameter and yields a list of values obtained.
Subroutines sub { –#parameters are placed – –. –return; }
ספטמבר 04Copyright Meir Kalech1 C programming Language Chapter 3: Functions.
Introduction to ML Last time: Basics: integers, Booleans, tuples,... simple functions introduction to data types This time, we continue writing an evaluator.
Introduction Even though the syntax of Scheme is simple, it can be very difficult to determine the semantics of an expression. Hacker’s approach: Run it.
OCaml The PL for the discerning hacker.. Hello. I’m Zach, one of Sorin’s students.
MAHENDRAN CHAPTER 6. Session Objectives Explain Type of Functions Discuss category of Functions Declaration & Prototypes Explain User Defined Functions.
CS 2104 Prog. Lang. Concepts Subprograms
Haskell. 2 GHC and HUGS Haskell 98 is the current version of Haskell GHC (Glasgow Haskell Compiler, version 7.4.1) is the version of Haskell I am using.
Functional Programming in Scheme and Lisp. Overview In a functional programming language, functions are first class objects. You can create them, put.
Digital Computer Concept and Practice Copyright ©2012 by Jaejin Lee C Language Part 2.
Return function Random function Recursion function Function in C 1.
Functional Programming and Lisp. Overview In a functional programming language, functions are first class objects. In a functional programming language,
20-753: Fundamentals of Web Programming 1 Lecture 12: Javascript I Fundamentals of Web Programming Lecture 12: Introduction to Javascript.
1 CSC103: Introduction to Computer and Programming Lecture No 14.
Hello.java Program Output 1 public class Hello { 2 public static void main( String [] args ) 3 { 4 System.out.println( “Hello!" ); 5 } // end method main.
If statements while loop for loop
I Power Int 2 Computing Software Development High Level Language Constructs.
CSC 580 – Theory of Programming Languages, Spring, 2009 Week 9: Functional Languages ML and Haskell, Dr. Dale E. Parson.
© 2007 Pearson Education, Inc. Publishing as Pearson Addison-Wesley 1 Visibility, Accessibility, and Information Hiding.
Scope Rules Prepared by Manuel E. Bermúdez, Ph.D. Associate Professor University of Florida Programming Language Concepts Lecture 11.
Announcements Project1 Due Wednesday September 21 st 4:30pm We will post instructions on how to turnin from home Note: you can always turn in from the.
Multiplying Binomials Objectives: To Multiply Two Binomials FOIL To multiply the sum and difference of two expressions To square a binomial.
Chapter 8 More On Functions. "The Practice of Computing Using Python", Punch & Enbody, Copyright © 2013 Pearson Education, Inc. First cut, scope.
Topics: Function Intitution Algorithm Example Function Definition (and Indentation) Function Call Naming and Binding Local Variables Function Arguments.
Python Functions.
I Power Higher Computing Software Development High Level Language Constructs.
Lambda Calculus Prepared by Manuel E. Bermúdez, Ph.D. Associate Professor University of Florida Programming Language Principles Lecture 11.
Writing RPAL Programs Prepared by Manuel E. Bermúdez, Ph.D. Associate Professor University of Florida Programming Language Concepts Lecture 13.
Introduction to Computing Using Python Namespaces – Local and Global  The Purpose of Functions  Global versus Local Namespaces  The Program Stack 
The RPAL Functional Language Prepared by Manuel E. Bermúdez, Ph.D. Associate Professor University of Florida Programming Language Concepts Lecture 12.
Scope, Aliasing, Tuples & Mutability Intro2CS – week 4a 1.
Methods Awesomeness!!!. Methods Methods give a name to a section of code Methods give a name to a section of code Methods have a number of important uses.
MIT-AITI: Functions Defining and Invoking Functions Functions as Data Function Scope: The call Object Function Arguments: The arguments objects Function.
Department of Electronic & Electrical Engineering Lecture 3 IO reading and writing variables scanf printf format strings "%d %c %f" Expressions operators.
Haskell. GHC and HUGS Haskell 98 is the current version of Haskell GHC (Glasgow Haskell Compiler, version 7.4.1) is the version of Haskell I am using.
6-Jul-16 Haskell II Functions and patterns. Data Types Int + - * / ^ even odd Float + - * / ^ sin cos pi truncate Char ord chr isSpace isUpper … Bool.
Test 2 Review Outline.
Programming Language Concepts
Context-free grammars, derivation trees, and ambiguity
Tuples Module 11.1 COP4020 – Programming Language Concepts Dr. Manuel E. Bermudez.
Parameter passing Module 12.3 COP4020 – Programming Language Concepts Dr. Manuel E. Bermudez.
Haskell.
Introduction to RPAL Module 10.1 COP4020 – Programming Language Concepts Dr. Manuel E. Bermudez.
Programming Language Principles
Bottom-up derivation tree, original grammar
TaBle-driven LL(1) Parsing
Lecture 15 (Notes by P. N. Hilfinger and R. Bodik)
COP4020 Programming Language Concepts Dr. Manuel E. Bermudez
NFA->DFA Module 05.3 COP4020 – Programming Language Concepts Dr. Manuel E. Bermudez.
CSE 341 Section 5 Winter 2018.
CSCE 314: Programming Languages Dr. Dylan Shell
Namespaces – Local and Global
Building AST's for RPAL Programs
G. Pullaiah College of Engineering and Technology
The RPAL Functional Language
Recursion and Rpal’s synTax
Operator precedence and AST’s
Paradigms and paradigm shifts
Operator Precedence and Associativity
Lambda Expressions Cases
Namespaces – Local and Global
Functions, Procedures, and Abstraction
Presentation transcript:

RPAL Function definitions Module 10.2 COP4020 – Programming Language Concepts Dr. Manuel E. Bermudez

Topics RPAL Function definitions Functions are first-class objects let, where constructs Nesting and scopes Simultaneous definitions Cascading definitions @: infix use of a function. Functions are first-class objects

Function definitions – let D in E Example (simple definition): let X=3 in Print(X,X**2) // Prints (3,9) Example (function form): let Abs N = N ls 0 -> -N | N in Print(Abs (-3)) // Prints 3 X=3 is not an assignment. Equivalent to: (fn X. Print(X,X**2))3 Equivalent to: let Abs = fn N. N ls 0 -> -N | N in Print (Abs (-3)) (fn Abs. Print(Abs (-3)) (fn N. N ls 0 -> -N | N) Print((fn N. N ls 0 -> -N | N) (-3)))

Nesting Definitions Scope of ‘let’ parameter is expression after ‘in’. Nested scopes are as expected. let X = 3 in let Sqr X = X**2 Print (X, Sqr X, X * Sqr X, Sqr (X+2)) Scope of X=3, except for Scope of Sqr

Function definitions – E where D Example (simple definition): Print(X,X**2) where X=3 // Prints (3,9) Example (function form): Print(Abs (-3)) where Abs N = N ls 0 -> -N | N // Prints 3 X=3 is not an assignment. Equivalent to: (fn X. Print(X,X**2))3 Equivalent to: (fn Abs. Print(Abs (-3))) (fn N. N ls 0 -> -N | N) Print((fn N. N ls 0 -> -N | N) (-3))

Nesting definitions vs. let X = 3 in let Sqr X = X**2 in Print (X, Sqr X, X * Sqr X, Sqr (X+2)) vs. ( Print (X, Sqr X, X * Sqr X, Sqr (X+2)) where Sqr X = X**2 ) where X = 3 Parentheses required ! Otherwise Sqr X = (X**2 where X=3)

Simultaneous Definitions Join definitions with ’and’. Example: let X=3 and Y=5 in Print(X+Y) Keyword and: not a boolean operator (we have &). Both definitions come into scope at once, in Print(X+Y). Different from let X=3 in let Y=5 in Print(X+Y) let X=3 in let Y=5+X in Print(X+Y) Scope of X=3

cascading function definitions ’within’: make one definition visible inside other(s). Example: let c=3 within f x = x + c in Print(f 3) vs. let c=3 in let f x = x + c Scope of c=3 Primitive form of information hiding

The ‘@’ operator Allows infix use of a function. Example: let Add x y = x + y in Print (2 @Add 3 @Add 4) Equivalent to: in Print (Add (Add 2 3) 4) Useful for concatenating strings: Print(’a’ @Conc ’b’ @Conc ’c’)

Functions are first-class objects Can do (almost) anything with a function. Name it: let Inc x = x+1 in Inc (3) Pass it as a parameter: let f g = g 3 in let h x = x + 1 in Print(f h) Return it from a function: let f x = fn y. x+y in Print (f 3 2) Print it! let Inc x = x+1 in Print (Inc)

Functions are first-class objects Select it using a conditional: let B=true in let f = B -> (fn y.y+1) | (fn y.y+2) in Print (f 3) Store it in a tuple: let T=((fn x.x+1),(fn x.x+2)) in Print (T 1 3, T 2 3) Apply it to a tuple (n-ary function): let Add (x,y) = x+y in Print (Add(3,4))

summary RPAL Function definitions Functions are first-class objects let, where constructs Nesting and scopes Simultaneous definitions Cascading definitions @: infix use of a function. Functions are first-class objects