Today’s Agenda ML Development Workflow –Emacs –Using use –The REPL More ML –Shadowing Variables –Debugging Tips –Boolean Operations –Comparison Operations.

Slides:



Advertisements
Similar presentations
CSE341: Programming Languages Lecture 2 Functions, Pairs, Lists Dan Grossman Winter 2013.
Advertisements

Programming Languages Section 1 1 Programming Languages Section 1. SML Fundamentals Xiaojuan Cai Spring 2015.
CSE341: Programming Languages Lecture 16 Datatype-Style Programming With Lists or Structs Dan Grossman Winter 2013.
8-May-15 Additional control structures. 2 The if-else statement The if-else statement chooses which of two statements to execute The if-else statement.
CSE341: Programming Languages Lecture 8 Lexical Scope and Function Closures Dan Grossman Fall 2011.
CSE341: Programming Languages Lecture 1 Course Mechanics ML Variable Bindings Dan Grossman Fall 2011.
CSE341: Programming Languages Lecture 3 Local bindings, Options, Benefits of No Mutation Dan Grossman Fall 2011.
CSE341: Programming Languages Lecture 7 Functions Taking/Returning Functions Dan Grossman Fall 2011.
CSE341: Programming Languages Lecture 2 Functions, Pairs, Lists Dan Grossman Fall 2011.
CIS 101: Computer Programming and Problem Solving Lecture 8 Usman Roshan Department of Computer Science NJIT.
Introduction to ML Last time: Basics: integers, Booleans, tuples,... simple functions introduction to data types This time, we continue writing an evaluator.
CS31: Introduction to Computer Science I Discussion 1A 4/2/2010 Sungwon Yang
While Loops and Do Loops. Suppose you wanted to repeat the same code over and over again? System.out.println(“text”); System.out.println(“text”); System.out.println(“text”);
Methods. Why methods? A method gives a name to something that you want to do, so you don’t have to think about how to do it, you just do it The name should.
OCaml The PL for the discerning hacker.. Hello. I’m Zach, one of Sorin’s students.
Testing Michael Ernst CSE 140 University of Washington.
1 CSC 221: Computer Programming I Spring 2010 interaction & design  modular design: roulette game  constants, static fields  % operator, string equals.
Week 5 - Wednesday.  What did we talk about last time?  Exam 1!  And before that?  Review!  And before that?  if and switch statements.
Writing Scala Programs. Command Line There are three common operating systems: Windows (various flavors; I recommend Windows 7) UNIX or Linux (basically.
1 Conditions Logical Expressions Selection Control Structures Chapter 5.
Current Assignments Homework 2 is available and is due in three days (June 19th). Project 1 due in 6 days (June 23 rd ) Write a binomial root solver using.
Making Decisions (True or False) Relational Operators >greater than =greater than or equal to
CSC 110 Using Python [Reading: chapter 1] CSC 110 B 1.
CMSC 330: Organization of Programming Languages Operational Semantics a.k.a. “WTF is Project 4, Part 3?”
Error Example - 65/4; ! Toplevel input: ! 65/4; ! ^^ ! Type clash: expression of type ! int ! cannot have type ! real.
How to execute Program structure Variables name, keywords, binding, scope, lifetime Data types – type system – primitives, strings, arrays, hashes – pointers/references.
CSED101 INTRODUCTION TO COMPUTING FUNCTION ( 함수 ) 유환조 Hwanjo Yu.
CSE 341 : Programming Languages Lecture 8 First Class Functions Zach Tatlock Spring 2014.
CMSC 330: Organization of Programming Languages Operational Semantics.
CSE 341 : Programming Languages Lecture 9 Lexical Scope, Closures Zach Tatlock Spring 2014.
CSE341: Programming Languages Lecture 4 Records, Datatypes, Case Expressions Dan Grossman Spring 2013.
Instructor: Chris Trenkov Hands-on Course Python for Absolute Beginners (Spring 2015) Class #003 (February 14, 2015)
CSE-321 Programming Languages Introduction to Functional Programming POSTECH March 8, 2006 박성우.
Programming Languages Dan Grossman 2013 ML Expressions and Variable Bindings.
IST 210: PHP Logic IST 210: Organization of Data IST2101.
Copyright © 2014 Pearson Addison-Wesley. All rights reserved. 4 Simple Flow of Control.
CSE 341 Section 1 (9/28) With thanks to Dan Grossman, et. al. from previous versions of these slides.
CSE341: Programming Languages Lecture 7 First-Class Functions
CSE 341 Section 1 (9/28) With thanks to Dan Grossman, et. al. from previous versions of these slides.
EGR 2261 Unit 4 Control Structures I: Selection
CSE341: Programming Languages Lecture 4 Records, Datatypes, Case Expressions Dan Grossman Winter 2013.
Nicholas Shahan Spring 2016
Sentinel logic, flags, break Taken from notes by Dr. Neil Moore
CSE 341: Programming Languages Section 1
CSE 341: Programming Langs
CSE341: Programming Languages Section 1
Sentinel logic, flags, break Taken from notes by Dr. Neil Moore
CSE341: Programming Languages Lecture 8 Lexical Scope and Function Closures Dan Grossman Winter 2013.
CSE 341 Section 1 Nick Mooney Spring 2017
CSE341: Programming Languages Section 1
CSE341: Programming Languages Lecture 8 Lexical Scope and Function Closures Dan Grossman Spring 2013.
CSE341: Programming Languages Lecture 7 First-Class Functions
CSE341: Programming Languages Lecture 8 Lexical Scope and Function Closures Dan Grossman Spring 2016.
CSE341: Programming Languages Lecture 7 First-Class Functions
CSE 341: Programming Languages Section 1
CSE341: Programming Languages Lecture 8 Lexical Scope and Function Closures Dan Grossman Autumn 2018.
Winter 2018 With thanks to: Dan Grossman / Eric Mullen
CSE341: Programming Languages Lecture 8 Lexical Scope and Function Closures Zach Tatlock Winter 2018.
CSE341: Programming Languages Lecture 7 First-Class Functions
CSE341: Programming Languages Lecture 7 First-Class Functions
CSE341: Programming Languages Lecture 8 Lexical Scope and Function Closures Dan Grossman Autumn 2017.
Additional control structures
CSE341: Programming Languages Lecture 7 First-Class Functions
CSE341: Programming Languages Lecture 7 First-Class Functions
Programming Languages Dan Grossman 2013
CSE341: Programming Languages Lecture 8 Lexical Scope and Function Closures Dan Grossman Spring 2019.
CSE341: Programming Languages Section 1
Brett Wortzman Summer 2019 Slides originally created by Dan Grossman
CSE341: Programming Languages Lecture 7 First-Class Functions
Presentation transcript:

Today’s Agenda ML Development Workflow –Emacs –Using use –The REPL More ML –Shadowing Variables –Debugging Tips –Boolean Operations –Comparison Operations 1

Emacs Recommended (not required) editor for this course Powerful, but the learning curve can at first be intimidating Helpful resources –CSE 341 Emacs GuideCSE 341 Emacs Guide –Emacs Cheat SheetEmacs Cheat Sheet –Emacs Reference CardEmacs Reference Card –UW’s (OLD?) Emacs TutorialUW’s (OLD?) Emacs Tutorial –Google it! –Course staff, or ask around in the labs 2

Quick Emacs Demo 3 Image credit:

Using use Enters bindings from the file foo.sml –Like typing the variable bindings one at a time in sequential order into the REPL (more on this in a moment) Result is () bound to variable it –Ignorable 4 use "foo.sml";

The REPL Read-Eval-Print-Loop is well named Conveniently run programs –Useful to quickly try something out –Save code for reuse by moving it into a persistent.sml file Expects semicolons For reasons discussed later, it’s dangerous to reuse use without restarting the REPL session 5

Shadowing of Variable Bindings val a = 1; (* a -> 1 *) val b = a; (* a -> 1, b -> 1 *) val a = 2; (* a -> 2, b -> 1 *) 6 1.Expressions in variable bindings are evaluated “eagerly” –Before the variable binding “finishes” –Afterwards, the expression producing the value is irrelevant 1.Multiple variable bindings to the same variable name, or “shadowing”, is allowed –When looking up a variable, ML uses the latest binding by that name in the current environment 2.Remember, there is no way to “assign to” a variable in ML –Can only shadow it in a later environment –After binding, a variable’s value is an immutable constant

Try to Avoid Shadowing 7 Shadowing can be confusing and is often poor style Why? Reintroducing variable bindings in the same REPL session may.. –make it seem like wrong code is correct; or –make it seem like correct code is wrong. val x = "Hello World"; val x = 2; (* is this a type error? *) val res = x * 2; (* is this 4 or a type error? *)

Using a Shadowed Variable Is it ever possible to use a shadowed variable? Yes! And no… It can be possible to uncover a shadowed variable when the latest binding goes out of scope 8 val x = "Hello World"; fun add1(x : int) = x + 1; (* shadow x in func body *) val y = add1 2; val z = x^"!!"; (* "Hello World!!" *)

Use use Wisely Warning: Variable shadowing makes it dangerous to call use more than once without restarting the REPL session. It may be fine to repeatedly call use in the same REPL session, but unless you know what you’re doing, be safe! –Ex: loading multiple distinct files (with independent variable bindings) at the beginning of a session –use ’s behavior is well-defined, but even expert programmers can get confused Restart your REPL session before repeated calls to use 9

Debugging Errors Your mistake could be: Syntax: What you wrote means nothing or not the construct you intended Type-checking: What you wrote does not type-check Evaluation: It runs but produces wrong answer, or an exception, or an infinite loop Keep these straight when debugging even if sometimes one kind of mistake appears to be another 10

Play around Best way to learn something: Try lots of things and don’t be afraid of errors Work on developing resilience to mistakes –Slow down –Don’t panic –Read what you wrote very carefully Maybe watching me make a few mistakes will help… 11

Boolean Operations not is just a pre-defined function, but andalso and orelse must be built-in operations since they cannot be implemented as a function in ML. –Why? Because andalso and orelse “short-circuit” their evaluation and may not evaluate both e1 and e2. Be careful to always use andalso instead of and. and is completely different. We will get back to it later. 12 OperationSyntaxType-checkingEvaluation andalsoe1 andalso e2 e1 and e2 must have type bool Same as Java’s e1 && e2 orelsee1 orelse e2 e1 and e2 must have type bool Same as Java’s e1 || e2 notnot e1 e1 must have type bool Same as Java’s ! e1

Style with Booleans Language does not need andalso, orelse, or not Using more concise forms generally much better style And definitely please do not do this: 13 (* e1 andalso e2 *) if e1 then e2 else false (* e1 orelse e2 *) if e1 then true else e2 (* just say e (!!!) *) if e then true else false (* not e1 *) if e1 then false else true

Comparisons For comparing int values: = <> > = <= You might see weird error messages because comparators can be used with some other types too: > = <= can be used with real, but not 1 int and 1 real = <> can be used with any “equality type” but not with real –Let’s not discuss equality types yet 14