Www.eschertech.com Fundamentals of Perfect Developer A one-day hands-on tutorial Answers to Exercises.

Slides:



Advertisements
Similar presentations
Functional Programming Lecture 13 - induction on lists.
Advertisements

Haskell Lets review some of the Haskell concepts you have been learning on your own. The answers are included, but try it yourself first.
ML Lists.1 Standard ML Lists. ML Lists.2 Lists A list is a finite sequence of elements. [3,5,9] ["a", "list" ] [] Elements may appear more than once [3,4]
For(int i = 1; i
ML Lists.1 Standard ML Lists. ML Lists.2 Lists  A list is a finite sequence of elements. [3,5,9] ["a", "list" ] []  Elements may appear more than once.
Type Variables in ML Until we know the type of a value (perhaps never!), we use a variable for its type Book uses, e.g., t1, tx, tf PL literature uses.
SORTING Lecture 12B CS2110 – Spring InsertionSort 2 pre: b 0 b.length ? post: b 0 b.length sorted inv: or: b[0..i-1] is sorted b 0 i b.length sorted.
ML Lists.1 Standard ML Lists. ML Lists.2 Lists  A list is a finite sequence of elements. [3,5,9] ["a", "list" ] []  ML lists are immutable.  Elements.
AVL-Trees (Part 2) COMP171. AVL Trees / Slide 2 A warm-up exercise … * Create a BST from a sequence, n A, B, C, D, E, F, G, H * Create a AVL tree for.
Templates in C++. Generic Programming Programming/developing algorithms with the abstraction of types The uses of the abstract type define the necessary.
Cs2220: Engineering Software Class 10: Generic Datatypes Fall 2010 University of Virginia David Evans.
CodeContracts & Clousot Francesco Logozzo - Microsoft Mehdi Bouaziz – ENS.
Operator Overloading Fundamentals
Exercises – don’t use built in functions for these as we want the practice Write a recursive function to add up all the numbers in a list "flatten" a list.
1 A full binary tree A full binary tree is a binary tree in which all the leaves are on the same level and every non leaf node has two children. SHAPE.
Tim Sheard Oregon Graduate Institute Lecture 4: Staging Interpreters CSE 510 Section FSC Winter 2004 Winter 2004.
Fundamentals of Perfect Developer A one-day hands-on tutorial.
A Lightning Tour of Haskell Lecture 1, Designing and Using Combinators John Hughes.
1 Specifying Object Interfaces. 2 Major tasks in this stage: --are there any missing attributes or operations? --how can we reduce coupling, make interface.
Understanding BubbleSort CS-502 (EMC) Fall Understanding BubbleSort CS-502, Operating Systems Fall 2009 (EMC) (Slides include materials from Modern.
Compiling with Dependent Types Hongwei Xi University of Cincinnati.
1 Recursion Simple recursion to traverse lists Recursive data structures and recursive functions.
Functional Programming Lecture 4 - More Lists Muffy Calder.
5. Hierarchical CDFDs and Modules The motivation for building hierarchical CDFDs: It is almost impossible to construct only one level CDFD and module for.
Coinduction in a language and verifier K. Rustan M. Leino Research in Software Engineering (RiSE) Microsoft Research, Redmond IFIP WG 2.3 meeting Seattle,
PrasadCS7761 Haskell Data Types/ADT/Modules Type/Class Hierarchy Lazy Functional Language.
Software reuse means to “borrow” existing code. How can you reuse an existing class to make a new one? Such reuse is really an adaptation -- using the.
Formal Methods in SE Lecture 20. Agenda 2  Relations in Z Specification Formal Methods in SE.
CSE 2813 Discrete Structures Recurrence Relations Section 6.1.
224 3/30/98 CSE 143 Recursion [Sections 6.1, ]
CAR SALES Solve a real-world problem EXAMPLE 3 A car dealership sold 78 new cars and 67 used cars this year. The number of new cars sold by the dealership.
Advanced Programming Andrew Black and Tim Sheard Lecture 11 Parsing Combinators.
Applying Recursion Routing in Computer Networks. Review We’ve seen that recursion provides an elegant, simple, and (sometimes) efficient way to solve.
10. The composite and product types The outline of this part: Composite types Constructing a composite type Constructor Operators Comparison Product types.
Solving Systems of Equations by Elimination (Addition) Section 3.2, Part II.
Tuesday December 10, 2013 Bell Ringer: Solve the following equation: 4(a - 6) + 4 = 2a - 6.
8. The set types The set types are one of the compound types available in SOFL, and usually used for the abstraction of data items that have a collection.
Advanced Formal Methods Lecture 4: Isabelle – Types and Terms Mads Dam KTH/CSC Course 2D1453, Some material from Paulson.
1 Logic Our ability to state invariants, record preconditions and post- conditions, and the ability to reason about a formal model depend on the logic.
ML Lists.1 Standard ML Lists. ML Lists.2 Lists  A list is a finite sequence of elements. [3,5,9] ["a", "list" ] []  Elements may appear more than once.
1 Numeric Abstract Domains Mooly Sagiv Tel Aviv University Adapted from Antoine Mine.
Objective solve systems of equations using elimination.
BIO 315 Week 5 Learning Team Exercises Case Study Review and Discussion Complete the following: Read the field study in Ch. 20 of Elements of Ecology and.
ACC 225 Week 1 Exercise Accounting and Business Organizations · Resource: Fundamental Accounting Principles, p. 30 · Post your answers to Exercises 1-1.
ACC 225 Week 2 Assignment Preparing Journal Entries and Trial Balances · Resource: Fundamental Accounting Principles, pp. 76 and 80 · Complete Exercises.
ACC 225 Week 7 CheckPoint Accounting Information Systems and Special Journals · Resource: Fundamental Accounting Principles, pp. 289, 290, and 291 · Complete.
Software Testing CS II: Data Structures & Abstraction
Copyright © Cengage Learning. All rights reserved.
Programming Languages Dan Grossman 2013
درس طراحی الگوریتم ها (با شبه کد های c ++)
Chapter 16-2 Linked Structures
Solving Systems Using Substitution
البرمجة بلغة فيجول بيسك ستوديو
Copyright © Cengage Learning. All rights reserved.
Sequence Alignment 11/24/2018.
Chapter 11 Generic Collections
Simultaneous Equations
JavaScript Reserved Words
Some heuristics for deriving invariant LN sections 6.7 , 6.8
Question Web Ji Hye Choi.
Sorting And Searching CSE116A,B 4/6/2019 B.Ramamurthy.
Output Variables {true} S {i = j} i := j; or j := i;
CSCE 314: Programming Languages Dr. Dylan Shell
True / False Variables.
CIS 720 Lecture 3.
CIS 720 Lecture 3.
Simultaneous Equations
CIS 720 Lecture 4.
Step 1: Put the equations in Standard Form. Standard Form: Ax + By = C
Changing a file from being long to being wide*
Presentation transcript:

Fundamentals of Perfect Developer A one-day hands-on tutorial Answers to Exercises

Suggested answers to Exercise 1 const zeroToOneHundred: seq of int ^= ; property assert 42 in zeroToOneHundred, 101 ~in zeroToOneHundred; function divides(i, j: int): bool pre j > 0 ^= i % j = 0; const squaresOfPrimes: seq of int ^= for those i:: :- forall j::2..<i :- ~divides(i, j) yield i ^ 2; function max(S: set of int): int pre ~S.empty ^= that x::S :- forall y::S :- y <= x;

Suggested answers to Exercise 4 A recursive solution to the first problem is: function numLeadingSpaces(s: string): nat decrease #s ^= ( [s.empty | s.head ~= ` `]: 0, []: 1 + numLeadingSpaces(s.tail) ); A non-recursive solution to the first is: function numLeadingSpaces(s: string): nat ^= that j::0..#s :- (j = #s | s[j] ~= ` `) & (forall k::0..<j :- s[k] = ` `); The following member functions may be useful: take drop findFirst prepend

Suggested answers 4 (cont’d) function removeLeadingSpaces(s: string): string ^= s.drop(numLeadingSpaces(s)); function firstWord(s: string): string ^= ( let n ^= s.findFirst(` `); [n < 0]: s, []: s.take(n) );

Suggested answers 4 (cont’d) function splitIntoWords(s: string): seq of string decrease #s ^= ( let stripped ^= removeLeadingSpaces(s); [stripped.empty]: seq of string{}, []: ( let w ^= firstWord(stripped); splitIntoWords(stripped.drop(#w)).prepend(w) ) );

Suggested answers to Exercise 5 function min2a(x, y: int): int satisfy result <= x, result <= y, result = x | result = y via if [x > y]: value y; []: value x fi end; function min2b(x, y: int): int satisfy result <= x, result <= y, result = x | result = y via value ([x > y]: y, []: x) end;

Suggested answers 5 (cont’d) function findFirst1(s: seq of int, x: int): int satisfy 0 <= result <= #s, result = #s | s[result] = x, forall j::0..<result :- s[j] ~= x via loop var i: (nat in 0..#s)! = 0; keep forall j::0..<i' :- s[j] ~= x until i' = #s decrease #s - i'; if [s[i] = x]: value i; [] fi; i! + 1 end; value #s end;

Suggested answers 5 (cont’d) function findFirst1(s: seq of int, x: int): int satisfy 0 <= result <= #s, result = #s | s[result] = x, forall j::0..<result :- s[j] ~= x via var i: (nat in 0..#s)! = 0; loop change i keep forall j::0..<i' :- s[j] ~= x until i' = #s | s[i']= x decrease #s - i'; i! + 1 end; value i end;

Suggested answers 5 (cont’d) function numLeadingSpaces(s: string): nat ^= that j::0..#s :- (j = #s | s[j] ~= ` `) & (forall k::0..<j :- s[k] = ` `) via var i: (nat in 0..#s)! = 0; loop change i keep forall j::0..<i':- s[j] ~= ` ` until i'= #s | s[i'] = ` ` decrease #s - i'; i! + 1 end; value i end;

Suggested answers 5 (cont’d) function splitIntoWords(s: string): seq of string decrease #s ^= ( let stripped ^= removeLeadingSpaces(s); [stripped.empty]: seq of string{}, []: ( let w ^= firstWord(stripped); assert ~w.empty; splitIntoWords(stripped.drop(#w)).prepend(w) )...

Suggested answers 5 (cont’d) via var rslt: seq of string ! = seq of string{}; loop var i: (nat in 0..#s)! = 0; change rslt keep splitIntoWords(s) = rslt' ++ splitIntoWords(s.drop(i')) until i'= #s decrease #s - i'; i! + numLeadingSpaces(s.drop(i)); if [i < #s]: let w ^= firstWord(s.drop(i)); rslt! = rslt.append(w), i! + #w; [] fi end; value rslt end;

Suggested answers to Exercise 6 function longest(s: seq of string): string decrease #s ^= ( [s.empty]: "", []: ( let temp ^= longest(s.front); [#s.last >= #temp]: s.last, []: temp ) );

Suggested answers 6 (cont’d) class ListOfStrings ^= abstract var list: seq of string; internal var long: string; invariant long = longest(list); interface build{} post list! = seq of string{} via list! = seq of string{}, long! = "“ end; …

Suggested answers 6 (cont’d) … schema !add(s: string) post list! = list.append(s) via list! = list.append(s); if [#s >= #long]: long! = s; [] fi end; function longest: string ^= longest(list) via value long end; end;