Racket Introduction CSC270 Pepper

Slides:



Advertisements
Similar presentations
Racket Introduction CSC270 Pepper major portions credited to
Advertisements

Chapter 3 Functional Programming. Outline Introduction to functional programming Scheme: an untyped functional programming language.
Programming in Visual Basic
Lecture 2 Introduction to C Programming
Introduction to C Programming
 2000 Prentice Hall, Inc. All rights reserved. Chapter 2 - Introduction to C Programming Outline 2.1Introduction 2.2A Simple C Program: Printing a Line.
Introduction to C Programming
CMT Programming Software Applications
 2007 Pearson Education, Inc. All rights reserved Introduction to C Programming.
 2002 Prentice Hall. All rights reserved. 1 Chapter 2 – Introduction to Python Programming Outline 2.1 Introduction 2.2 First Program in Python: Printing.
Guide To UNIX Using Linux Third Edition
Introduction to C Programming
Week 4-5 Java Programming. Loops What is a loop? Loop is code that repeats itself a certain number of times There are two types of loops: For loop Used.
 2003 Prentice Hall, Inc. All rights reserved. 1 Introduction to C++ Programming Outline Introduction to C++ Programming A Simple Program: Printing a.
JavaScript, Fifth Edition Chapter 1 Introduction to JavaScript.
CH Programming An introduction to programming concepts.
Introduction to Python
Created by, Author Name, School Name—State FLUENCY WITH INFORMATION TECNOLOGY Skills, Concepts, and Capabilities.
Haskell Chapter 1, Part I. Highly Recommended  Learn you a Haskell for Great Good. Miran Lipovaca.
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.
CS 330 Programming Languages 11 / 21 / 2006 Instructor: Michael Eckmann.
© Copyright 1992–2004 by Deitel & Associates, Inc. and Pearson Education Inc. All Rights Reserved. Chapter 2 Chapter 2 - Introduction to C Programming.
 Pearson Education, Inc. All rights reserved Introduction to Java Applications.
Lecture 2: Introduction to C Programming. OBJECTIVES In this lecture you will learn:  To use simple input and output statements.  The fundamental data.
C++ Programming Basic Learning Prepared By The Smartpath Information systems
REVIEW No curveballs this time …. PROBLEM TYPE #1: EVALUATIONS.
Introduction to Python Dr. José M. Reyes Álamo. 2 Three Rules of Programming Rule 1: Think before you program Rule 2: A program is a human-readable set.
Cosc175/operators1 Algorithms computer as the tool process – algorithm –Arithmetic: addition,subtraction,multiplication,division –Save information for.
Python Let’s get started!.
© Copyright 1992–2004 by Deitel & Associates, Inc. and Pearson Education Inc. All Rights Reserved. 1 Chapter 2 - Introduction to C Programming Outline.
CHAPTER 2 PROBLEM SOLVING USING C++ 1 C++ Programming PEG200/Saidatul Rahah.
Controlling Program Flow with Decision Structures.
1 FP Foundations, Scheme In Text: Chapter Chapter 14: FP Foundations, Scheme Mathematical Functions Def: A mathematical function is a mapping of.
 2007 Pearson Education, Inc. All rights reserved. A Simple C Program 1 /* ************************************************* *** Program: hello_world.
Internet & World Wide Web How to Program, 5/e © by Pearson Education, Inc. All Rights Reserved.
An Introduction to Programming with C++ Sixth Edition Chapter 5 The Selection Structure.
A Sample Program #include using namespace std; int main(void) { cout
1 Agenda  Unit 7: Introduction to Programming Using JavaScript T. Jumana Abu Shmais – AOU - Riyadh.
1 Lecture 2 - Introduction to C Programming Outline 2.1Introduction 2.2A Simple C Program: Printing a Line of Text 2.3Another Simple C Program: Adding.
CMSC201 Computer Science I for Majors Lecture 05 – Comparison Operators and Boolean (Logical) Operators Prof. Katherine Gibson Prof. Jeremy.
Functional Programming
CST 1101 Problem Solving Using Computers
Functional Programming Languages
Chapter 6 JavaScript: Introduction to Scripting
Python Let’s get started!.
Introduction to Python
Completing the Problem-Solving Process
Chapter 2 - Introduction to C Programming
GC211Data Structure Lecture2 Sara Alhajjam.
The Selection Structure
Variables, Expressions, and IO
Haskell.
Functions CIS 40 – Introduction to Programming in Python
PHP Introduction.
Chapter 2 - Introduction to C Programming
Introduction to Functional Programming in Racket
Chapter 2 - Introduction to C Programming
Chapter 2 - Introduction to C Programming
Chapter 2 - Introduction to C Programming
PHP.
T. Jumana Abu Shmais – AOU - Riyadh
Dynamic Scoping Lazy Evaluation
Chapter 2 - Introduction to C Programming
Building Java Programs
Algorithms computer as the tool process – algorithm
Introduction to Functional Programming in Racket
Chapter 2: Introduction to C++.
Chapter 2 Programming Basics.
Chapter 2 - Introduction to C Programming
Introduction to C Programming
Presentation transcript:

Racket Introduction CSC270 Pepper major portions credited to http://learnxinyminutes.com/docs/racket/

What is Dr. Racket? Racket Dr. Racket Full spectrum programming language with roots in functional programming which is a type of the declarative paradigm Lisp / Scheme Formerly called PLT Scheme Objects, types, laziness, macros, new syntax builder Dr. Racket Integrated Development Environment

Declarative vs Imperative What not how Language can figure out how when you tell it what No side effects – No mutatable variables Express data flow Imperative Commands manipulate state of system and variables. Many side effects Mutable variables Control flow

Imperative vs. Functional (Declarative) From : https://msdn.microsoft.com/en-us/library/bb669144.aspx

Contrast: Imperative Types Pure Imperative SQL Data Manip Lang (DML : insert, update, delete) Procedural Exactly how algorithms First do this and next do that C Object Oriented Manipulate objects through predefined methods Classes Send messages between objects C++, Java

Another Functional Language: Excel Formulas express data flow Command sequence is not a consideration when coding Excel formulas Cells hold one target value – changing inputs will change the value, but you never do anything with its value over time.

Declarative types Domain specific Logic: Relationships defined in terms of inference rules Prolog Functional: Relationships defined in terms of functions Haskell, Subset of Racket Subset of F# Excel Domain specific Regular Expressions SQL Select

Some Functional Advantages Readability and Maintainability One task and no reliance on an external state Refactoring Because the insides of the function do not effect anything outside itself, as long as the output is the same, refactoring is fine Testing Tested in isolation

Racket Strengths Language syntax builder Continuations (capture future value of a computation and pass it around) Dynamic typing Manages its own memory Function creation on the fly (lambda) Function closure Can pass it like an object Remembers variable values of creation time scope

How to install Racket Panther will run Racket programs without IDE racket programfile Panther will run IDE with SSH X Forwarding to allow X Window System GUI http://aruljohn.com/info/x11forwarding/ drracket See a racket window good See Gtk initialization failed for display – no x windows Download on PC or Mac http://download.racket-lang.org/ IDE choice : racket language with #lang racket Context sensitive F1 help

Hello Racket Program #lang racket "Hello Racket" Save as hello.rkt IDE: File / save definition Running the program IDE : run button Panther without xterm: racket hello.rkt

Hello Racket with a defined variable #lang racket (define hellovar "Hello Racket again") Hellovar Notice that a variable is defined inside parentheses All commands inside parentheses

Hello Racket With a Function #lang racket (define (sayhi ) "Hello from the function") (sayhi) Notice how the function call is in () Notice the function definition syntax used here: (define (function name ) (stuff function does)) Balanced parentheses

Comments Block comments: #| … |# Single comments: ; #lang racket ; define a function called sayhi (define (sayhi ) "Hello from the function") ; and now call it (sayhi)

Rules about literals String: " " (use \" to type a text quote) number: 1, 1.3, 1/2, 1+2i, 6.003e+15, #x1A, #b10111, #o737, 8888888888888888888 will store a rational true/false : #t for true, #f for false logical: not, and, or : ex: (not #t) is false and (and 1 2) is false Suppress expansion: just one leading ' : '(function a b) will be text not a function

Parentheses Do not put a literal inside ()or Racket will evaluate it as a function #lang racket (define x 3) x (x) ; racket hates this (define (sayhi ) "Hello from the function") (sayhi) sayhi ; racket does not hate this, but wont run the sayhi function

Variable use ;Define for the program ;Define locally (define x "outside") ;Define locally (let ([x "inside"]) x) ; displays "inside" x ; displays "outside" ; Define function argument (define (myfunc num) (+ num 3)) ; (myfunc 4) ; displays 7 ; Change a variable (let's avoid it) (set! x 8) x; displays 8

Pictures Variable can contain a picture (require picturing-programs) (define dog1 ) (define cat1 ) ( above dog1 cat1) (above (flip-vertical dog1) (above dog1 cat1))

Variables Rules Summary definition: (define varname value) example: (define x 3) use: just use the name; example: x define locally inside a let expression: (let ([varname value]) expression ) use let * if you want to use the first set of variables to define another set use a variable: just the name - do not put a variable inside () or Racket will evaluate it as a function change a variable – let's avoid it: (set! varname value) example: (set! n (add1 n))

Arithmetic: All arithmetic is a function syntax: ( operator operand#1 operand#2) operators: +,-,/,*,expt,quotient, remainder, special operators: exact->inexact (from rational to real), gcd, lcm (+ 1 2) (/ 5 2) ; not integer division! (expt 2 3) ; 2 to the 3rd power (remainder 11 3) ;

Functions Already defined functions with parms Return is value of last expression (define (add8 num) "hello" (+ num 8) "hello again") (add8 3) Resolves to "hello again"

Simulate Excel Define 2 cells, one for income and one for deductions Define another cell that represents your gross income (income – deduction) Define another cell that represents your taxes at 30%

Booleans #t is true; #f is false = or eq? are functions Use = for numbers only (= 3 3.0) will be #t (eq? 3 3.0) will be #f (eq? "abc" "abc") will be #t (not (eq? "abc" "def")) will be #t <, > , <=, >=,

Decision - Cond (cond [ (= 1 x) (add1 x) ] [ (= 2 x) (+ x 4) ] [ else (+ x 6 ) ] ) 2 when x = 1; 6 when x = 2 13 when x = 7

Random (random 6) ; gives 0 to 5 (+ (random 6 ) 1 ) gives a dice value Create a throw dice function that rolls 2 dice and returns the total. What are your inputs? What is your output? What is your function name? No need to display the individual dice

Dice Roll (define (roll ) ( + (+ (random 6 ) 1) (+ (random 6 ) 1) )) (roll)

Repetition - Recursion add from 1 to a max value (define (addnum max) (cond [ ( = max 0) 0 ] [ else ( + max (addnum (- max 1))) ] )) (addnum 5) ; gives 15

Recursion Thought Process 1) What is true about the problem? (truth statements will end up in your code) 2) What are the base cases? (small simple truths - adding up 0 numbers yields 0) 3) What are you taking in and what is being returned ? ( give a max and get a total) 4) Make some samples: Addnum(0) should give 0 Addnum(1) should give 1 Addnum(2) should give 3 Addnum(3) should give 6 Addnum(4) should give 10

Test Cases before coding (require test-engine/racket-tests) ;; addnum function adds from 1 to a max argument ;; input max number ;; output total of 1 to argument (define (addnum 0) 0) (check-expect (addnum 0 ) 0) (check-expect (addnum 1 ) 1) (check-expect (addnum 3 ) 6) (check-expect (addnum 4 ) 10) (check-expect (addnum 10 ) 55) (check-expect (addnum -1 ) 0) (test)

Coding the recursion Define the function without a body giving names to input arguments (define (addnum num ) ) Fill in the body with a cond (cond [ ( ) ] [ else ]) Put the base case into the first condition (cond [ ( num <= 0 ) 0 ]

Coding the Recursive Call Consider how to handle one pass of the repetition; think about one of the later calls as a sample (addnum 4) Write what is available to you Your input arguments Good return values from your function (see your tests) Define the rest of the information when one part is removed Call that part recursively (cond [ (<= num 0 ) 0 ] [ else num + addnum(num-1) ])

Summary Define Functional Programming Declarative vs Imperative paradigms How to enter literals Create and use variables Create and use functions Decisions Recursive functions Parentheses, Parentheses, Parentheses