1.02k likes | 1.17k Views
TeachScheme, ReachJava. Stephen Bloch, Adelphi University , Garden City, NY John Clements, Cal Poly , San Luis Obispo, CA Kathi Fisler, Worcester Polytechnic Institute , Worcester, MA Matthew Flatt, University of Utah , Salt Lake City, UT
E N D
TeachScheme, ReachJava Stephen Bloch, Adelphi University, Garden City, NY John Clements, Cal Poly, San Luis Obispo, CA Kathi Fisler, Worcester Polytechnic Institute, Worcester, MA Matthew Flatt, University of Utah, Salt Lake City, UT Shriram Krishnamurthi, Brown University, Providence, RI Viera K. Proulx, Northeastern University, Boston, MA Supported by NSF grant #0618543 Related work by Matthias Felleisen, Robby Findler,Kathy Gray, Eli Barzilay, et al
What is a problem? • What is the meaning of life? • Not an objective question • How old am I? • An objective question, which requires information you don’t have • What is the smallest prime number? • An objective, fully-specified question, but once it’s answered once, it’s over TeachScheme, ReachJava 2009
What is a problem? • What is the Celsius equivalent of 45F? • Again, once it’s answered, it’s over • What is the Celsius equivalent of 87F? • Ditto, but closely related • What is the Celsius equivalent of ___ in Fahrenheit? • Generalizes infinitely many questions TeachScheme, ReachJava 2009
What is a problem? Question: has a single answer Problem: large (or infinite) family of questions, sharing some features & differing in others Program: general rule for solving a problem, with input for the features that differ Computation: a program working on particular input to produce particular results TeachScheme, ReachJava 2009
Introductions • Who are you? • What kind of school? students? • Background in teaching CS • Background in Scheme, Java, etc? TeachScheme, ReachJava 2009
This week • You’re primarily students, not teachers • Get “the student experience” of this approach — only faster • Time on Friday to talk pedagogy TeachScheme, ReachJava 2009
What is “computer science”? • “microscope science” — about microbes • “telescope science” — about stars It’s not about the tool, it’s about what you can study using the tool! • “computer science” — about information and the manipulation thereof TeachScheme, ReachJava 2009
What is a beginning CS course about? • How to use Microsoft Office et al • Survey of the CS profession • A language that will get students jobs • Concepts, habits, & methodologies of programming Four completely different courses, each valuable for different reasons and audiences #3 useful for students about to graduate, or about to get internships and summer jobs #4 more useful as a first course. Let's call it "CS1"… TeachScheme, ReachJava 2009
What is CS1 about? TeachScheme, ReachJava 2009
What you teach blah algorithm blah variable blah function blah data type blah object blah re-use blah methodology blah testing blah design blah composition blah refinement blah abstraction TeachScheme, ReachJava 2009
What they hear blah ; blah { blah ) blah ] blah return blah this.x = x; blah public static void main (String[] args) blah /* blah // blah <int> blah if ( blah JOptionPane blah class Posn(int x,y) TeachScheme, ReachJava 2009
Programming languages:a necessary evil • Necessary: to write real programs that really run on real computers • Evil: distract students from the important stuff • Will be obsolete in a few years anyway TeachScheme, ReachJava 2009
How to minimize language & IDE? • Introduce features one at a time • Avoid "black magic"; never ask students to write anything they can't understand now • Introduce features only as necessary to teach an important concept • Corollary: any feature that doesn't help teach a CS1 concept shouldn't be mentioned in CS1 • Corollary: need enforced language subsets so students don't accidentally use language features they haven't seen • For GUI, robotics, networking, etc. need interface between student code (in subset) and library code (in full language) TeachScheme, ReachJava 2009
What Java do we need? When? • Realistically, most of us must cover a certain amount of Java by the end of CS2 • Not obvious that the best way to do this is Java from start of CS1 • Instead, teach concepts & habits first, then complicated language TeachScheme, ReachJava 2009
Alternatives to Java-first • Leading alternatives: Alice, Python, Scheme • All three: start w/simpler syntax while students master concepts • Difference: Alice & Python usually taught imperatively; Scheme usually functionally • (Functional-first Python: has potential) TeachScheme, ReachJava 2009
Why functional? • Simple, familiar semantic model • 3+4*5 => 3+20 => 23 • Ease of writing test suites • each test is usually one stmt, rather than"set up", "call method", "check side effects" • tests order-independent • no worry about aliasing, equals() vs. ==, etc. • If testing is easy, students may actually do it! TeachScheme, ReachJava 2009
But what about OOP? • Students need to learn OOP, and an OO language, by 2nd or 3rd semester • OOP is a terrific way to organize multi-KLOC programs • First-term students don't write multi-KLOC programs • OOP's benefits aren't apparent in first term; students see only the overhead • (Challenge: write a short, OO C++/Java program that wouldn't be simpler without OO) TeachScheme, ReachJava 2009
Main points of TSRJ • Multi-language approach to CS1/CS2 • Start in Scheme, develop concepts & habits • Switch to Java after 2-4 months • Step-by-step design recipe in both languages • concrete questions & products at each step • test-first methodology • strong emphasis on data types • shape of data determines shape of code & test cases • DrScheme development environment • beginner-friendly • interactive testing and experimentation • enforces language subsets w/appropriate messages TeachScheme, ReachJava 2009
Let’s try DrScheme • DrScheme • Interactions pane (bottom half) • Literal expressions • 7 • "hello world" • copy and paste picture from Web browser • true • Try www.adelphi.edu/sbloch/book/pictures/ TeachScheme, ReachJava 2009
Operating on pictures • (reflect-vert paste-picture-from-Web) • Same with reflect-horiz, rotate-cw, rotate-ccw, rotate-180 • (image-above picture1 picture2) • Same with image-beside, overlay • These three actually accept two or more pictures. • Syntax rule: (operation pic1 … ) • Terminology: expression, function, argument, value, literal TeachScheme, ReachJava 2009
Composing functions > (image-beside (reflect-horiz )) Exercise: write expressions to produce TeachScheme, ReachJava 2009
Scheme syntax • All expressions fully parenthesized;no order of operations to memorize • All functions (both built-in and user-defined) are prefix, inside parentheses • Some functions have arity 1, some 2, some"2 or more", etc. as appropriate. TeachScheme, ReachJava 2009
Definitions pane • Editable and savable • "Run" button evaluates all expressions in order, replacing old Interactions • Try7"hello world"copied image from Web(image-above (rotate-cw picture) (rotate-ccw picture)) TeachScheme, ReachJava 2009
The Stepper • Type several nested expressions into Definitions pane • Click "Step" button at top • See each sub-expression replaced with its value, one by one • Valuable for students who never really "got" algebra TeachScheme, ReachJava 2009
Variable definitions • (define calendar ) • (image-above calendar (reflect-vert calendar)) • Define another variable to hold a different picture copied from the Web; write some expressions using both. • Note no declared data type;data have types, variables don't TeachScheme, ReachJava 2009
Building images from scratch • (rectangle 34 21 "solid" "blue") • (circle 18 "outline" "green") • (ellipse 34 55 "outline" "purple") • (triangle 22 "solid" "pink") • (star 7 20 10 "solid" "blue") • (text "Hello there" 18 "blue") • Try your own variations • (define blue-star (star 7 20 10 "solid" "blue")) • Try modifying & combining these usingimage-above, overlay, reflect-vert, rotate-cw, surround, etc. TeachScheme, ReachJava 2009
Terminology • Three kinds of literals: image (pasted from Web browser), number, and string. • Data types TeachScheme, ReachJava 2009
Function contracts ; reflect-vert : image -> image ; reflect-horiz, rotate-cw, rotate-ccw, rotate-180 : similar ; image-beside : image image … -> image ; image-above, overlay : similar ; rectangle : number(width) number(height) string(solid/outline) string(color) -> image Summarizes what a function takes in and returns, in a brief, standard notation Note semicolon for comment-to-end-of-line TeachScheme, ReachJava 2009
More image functions ; crop-bottom : image number(pixels) -> image ; crop-top, crop-left, crop-right : similar ; image-width : image -> number ; image-height : similar ; place-image : image(foreground) number(x) number(y) image(background) -> image ; text : string(text) number(size) string(color) -> image TeachScheme, ReachJava 2009
Defining functions > (define (mirror picture) (image-beside picture (reflect-horiz picture))) (mirror calendar) (mirror (rotate-cw (triangle 10 "solid" "blue"))) • Note no declared parameter or return types; again, data have types • Try (mirror calendar hacker) -- wrong number of arguments! • Try (mirror 7) -- wrong type of argument! TeachScheme, ReachJava 2009
Exercise Define a function four-square that takes in an image and produces a 2x2 arrangement of it like TeachScheme, ReachJava 2009
Worked Exercise Define a function counterchange that takes in two images and produces a 2x2 arrangement like My answer: (define (counterchange topleft topright) (image-above (image-beside topleft topright) (image-beside topright topleft))) TeachScheme, ReachJava 2009
Testing functions • With English descriptions: • (mirror calendar) "should be a calendar on the left, and a right-left-reflected calendar on the right" • With check-expect: • (check-expect (mirror calendar) (image-beside calendar (reflect-horiz calendar))) • Put either or both in Definitions window; hit Run & see what happens. • Try with wrong function definition! TeachScheme, ReachJava 2009
Design recipes We could writecounterchangein an ad-hoc way, but in general we use… Step-by-step recipes to get from English-language description to working, tested code One recipe for functions, one for data types, one for abstractions, one for event-driven animations… TeachScheme, ReachJava 2009
How to accomplish anything • Figure out what you want to do • Do it • Check that you did it right TeachScheme, ReachJava 2009
How to write a function/method • Figure out what you want to do • Contract: Specify name, inputs, outputs • Data definition: Identify any new data types in the problem • Examples: Write test cases with expected answers • Do it • Skeleton: Write boilerplate code for function definition • Inventory: Write available expressions, their types, and (later) their values for a non-trivial test case • Body: Fill in details based on problem to solve • Check that you did it right • Testing: Run test cases TeachScheme, ReachJava 2009
The design recipe in practice contract: ; counterchange : image (topleft) image(topright) -> image data definition: problem only involves images, which are predefined test cases: (check-expect (counterchange calendar schemelogo) (image-above (image-beside calendar schemelogo) (image-beside schemelogo calendar))) (check-expect (counterchange book (ellipse 30 10 "outline" "pink")) (image-above (image-beside book (ellipse 30 10 "outline" "pink")) (image-beside (ellipse 30 10 "outline" "pink") book)) TeachScheme, ReachJava 2009
The design recipe in practice skeleton: (inserted before examples)(define (counterchange topleft topright) ) inventory:(define (counterchange topleft topright); topleft an image; topright an image ) body: (fill in something real, using expressions from inventory)(define (counterchange topleft topright) ; topleft an image ; topright an image(image-above (image-beside topleft topright) (image-beside topright topleft))) testing: hit "Run" and see whether answers match TeachScheme, ReachJava 2009
The result ; counterchange : image (topleft) image(topright) -> image (check-expect (counterchange calendar schemelogo) (image-above (image-beside calendar schemelogo) (image-beside schemelogo calendar))) (check-expect (counterchange book (ellipse 30 10 "outline" "pink")) (image-above (image-beside book (ellipse 30 10 "outline" "pink")) (image-beside (ellipse 30 10 "outline" "pink") book)) (define (counterchange topleft topright) ; topleft an image ; topright an image (image-above (image-beside topleft topright) (image-beside topright topleft))) TeachScheme, ReachJava 2009
Testing again • Change the definition so that it's wrong; run the tests again and see what happens. TeachScheme, ReachJava 2009
Design recipes as pedagogy • Note "test-first" methodology (a la XP); identify special cases before writing code • Use as grading rubric: partial credit for each step • First steps are in comments; non-threatening,avoids "blank page syndrome" • Each step has concrete questions and concrete products • I don't help with step N until I see step N-1 • Same steps apply in Java, C++, etc.(but more complicated) • Know-it-all students usually try to skip the recipe…until Chapter 12 of HtDP… TeachScheme, ReachJava 2009
Exercises Write contracts and test cases, but no definitions, for • a function named copies-beside that takes in a number and an image, and produces that many copies of the image side by side • a function named pinwheel that takes in an image & produces a 2x2 rotated thing like • a function named checkerboard2 that takes in two color names & produces a 2x2 checkerboard in those colors TeachScheme, ReachJava 2009
Exercises Write skeletons & definitions for pinwheel and checkerboard2 (you don't know how to do copies-beside yet) Do all the steps to define lollipop, which takes in two numbers and a color name, and creates a picture of a lollipop w/specified radius, stick length, and color TeachScheme, ReachJava 2009
Discussion break • How is this different from what you've done in the past? • How much explaining would it take for your students? • I have a lot of mathophobic students, so I start with images rather than numbers and algebra. The prefix notation doesn't throw them, because they don't already "know" the "right" notation for operating on images. TeachScheme, ReachJava 2009
Animation (run-animation 50 50 calendar 0.5 (on-tick rotate-cw)) ; run-animation : number(width) number(height) image number(tick-interval) handler … -> boolean Every tick-interval, applies handler to old image to get new image Try some variations: different widths, heights, images, tick-intervals, "on-tick" handlers TeachScheme, ReachJava 2009
A more complex animation • Write an animation of an image that moves 5 pixels to the right every second • We'll need a function that takes in an image and returns the same image 5 pixels to the right • Follow the design recipe! TeachScheme, ReachJava 2009
move-right-5 ; move-right-5 : image -> image TeachScheme, ReachJava 2009
move-right-5 ; move-right-5 : image -> image (check-expect (move-right-5 calendar)(image-beside (rectangle 5 0 "solid" "white") calendar)) (check-expect (move-right-5 (circle 3 "solid" "red"))(image-beside (rectangle 5 0 "solid" "white") (circle 3 "solid" "red"))) TeachScheme, ReachJava 2009
move-right-5 ; move-right-5 : image -> image (define (move-right-5 old-pic)) (check-expect (move-right-5 calendar)(image-beside (rectangle 5 0 "solid" "white") calendar)) (check-expect (move-right-5 (circle 3 "solid" "red"))(image-beside (rectangle 5 0 "solid" "white") (circle 3 "solid" "red"))) TeachScheme, ReachJava 2009
move-right-5 ; move-right-5 : image -> image (define (move-right-5 old-pic); old-pic an image) (check-expect (move-right-5 calendar)(image-beside (rectangle 5 0 "solid" "white") calendar)) (check-expect (move-right-5 (circle 3 "solid" "red"))(image-beside (rectangle 5 0 "solid" "white") (circle 3 "solid" "red"))) TeachScheme, ReachJava 2009