Download presentation
Presentation is loading. Please wait.
1
341 midterm review Xinrong Zhao Autumn 2018
2
Today’s content Macros: Brief Review + (Practice Questions)
You can bring one side of one 8.5x11in piece of paper Brief Review + (Practice Questions) Module Signature 2 Type Inference 5 Lexical Scope 4.5 Currying 4 Higher order function 3.5 Tail recursion 2 Datatypes, pattern matching 0.5
3
THINK-PAIR-Share-Report
Think individually pair with person next to you share with your neighbor get together to this question for report
4
Module & SIGNATURE Signature specifies
things with certain (or more general) types must exist in module things can be accessible outside module Module can have no signature or contain more things than its signature Question: Spring 2016 #6 (e’) What if having S1 with signature INTERVAL1 and S2 with signature INTERVAL2? (f’) What if having S1 with signature INTERVAL2 and S2 with signature INTERVAL1? Would S2.size (S1.make (5,~5)) type-check?
5
Type INFERENCE Set up formula Collect facts
If there is a conflict then type inference fails (the thing doesn’t type-check) Question: (g) & (h) on the same question
6
Lexical scope The body of a function is evaluated in the environment where the function is defined. Function Closure = code + environment when we define the function Question: Spring 2013 #3 the closure overall is “closed” — it has everything it needs to produce a function result given a function argument
7
Higher order function & Currying
Function that takes functions as argument or returns function Currying: Currying function takes “multiple arguments” and returns function closure except on the last argument Question: Spring 2016 #4
8
DATATYPES & Pattern matching
Datatype bindings have “One-of” Types Constructors taking some arguments to get this datatype Pattern-matching example for One-of types (datatypes) (* need to match each constructor option has *) case opt of NONE => … | SOME i => … Each-of types (tuple, record) case triple of (0, 0, _) => 0 | (a, b, _) => a + b Self-reference types (list) case int_tuple_list of [] => 0 | (a, b) :: c => a + b
9
Tail recursion No more to do after recursive call except return the result (there is nothing more for the caller to do after the callee returns except return the callee’s result) Example: (* assume n >= 0, f1 is not tail-recursive, but f2 is, why? *) fun f1 (n) = if n = 0 then 0 else f1(n-1) + 1 fun f2 (n) = if n = 0 then 0 else f2(n-1) Question: Spring 2013 #1
Similar presentations
© 2025 SlidePlayer.com. Inc.
All rights reserved.