Input and Output in Scheme CS 480/680 – Comparative Languages.

Slides:



Advertisements
Similar presentations
Chapter 3 Functional Programming. Outline Introduction to functional programming Scheme: an untyped functional programming language.
Advertisements

Macros and Function Generators CS 480/680 – Comparative Languages.
1-1 An Introduction to Scheme March Introduction A mid-1970s dialect of LISP, designed to be a cleaner, more modern, and simpler version than.
Μαθαίνοντας Python [Κ4] ‘Guess the Number’
Lisp – Introduction יעל נצר מערכות נבונות סמסטר ב' תשס"ו.
CS Lecture 03 Outline Sed and awk from previous lecture Writing simple bash script Assignment 1 discussion 1CS 311 Operating SystemsLecture 03.
Scripting Languages Chapter 6 I/O Basics. Input from STDIN We’ve been doing so with $line = chomp($line); Same as chomp($line= ); line input op gives.
More on Numerical Computation CS-2301 B-term More on Numerical Computation CS-2301, System Programming for Non-majors (Slides include materials from.
Common Lisp Derek Debruin Mark Larue Vinh Doan. Problem Domains There was a need in the early 1960s to define a programming language whose computational.
Basic Input/Output and Variables Ethan Cerami New York
LISP A brief overview. Lisp stands for “LISt Process” –Invented by John McCarthy (1958) –Simple data structure (atoms and lists) –Heavy use of recursion.
Ping Zhang 10/08/2010.  You can get data from the user (input) and display information to the user (output).  However, you must include the library.
1 (Functional (Programming (in (Scheme)))) Jianguo Lu.
Introduction to C++ - How C++ Evolved Most popular languages currently: COBOL, Fortran, C, C++, Java (script) C was developed in 1970s at AT&T (Richie)
Introduction to Functions Intro to Computer Science CS1510 Dr. Sarah Diesburg.
Input/Output Chapters 7 & 9. Output n Print produces output > (print 100) n It also returns the value it printed –that’s where the second 100 came.
C# B 1 CSC 298 Writing a C# application. C# B 2 A first C# application // Display Hello, world on the screen public class HelloWorld { public static void.
Builtins, namespaces, functions. There are objects that are predefined in Python Python built-ins When you use something without defining it, it means.
Copyright © 2012 Pearson Education, Inc. Publishing as Pearson Addison-Wesley C H A P T E R 2 Input, Processing, and Output.
Programming I Introduction Introduction The only way to learn a new programming language is by writing programs in it. The first program to.
Functional Programming in Scheme
The string data type String. String (in general) A string is a sequence of characters enclosed between the double quotes "..." Example: Each character.
Functional Programming in Scheme and Lisp. Overview In a functional programming language, functions are first class objects. You can create them, put.
Introduction to Scheme Lectures on The Scheme Programming Language, 2 nd Ed. R. Kent Dybvig.
Functional Programming and Lisp. Overview In a functional programming language, functions are first class objects. In a functional programming language,
CS 403: Programming Languages Lecture 6 Fall 2003 Department of Computer Science University of Alabama Joel Jones.
CS 326 Programming Languages, Concepts and Implementation Instructor: Mircea Nicolescu Lecture 5.
C Programming Lecture 5 : Basic standard I/O Lecture notes : courtesy of Ohio Supercomputing Center, science and technolgy support.
Awk Dr. Tran, Van Hoai Faculty of Computer Science and Engineering HCMC Uni. of Technology
Predicates, Functions and Files "I suppose I should learn Lisp, but it seems so foreign." - Paul Graham, Nov 1983.
COP4020 Programming Languages Functional Programming Prof. Xin Yuan.
CSE S. Tanimoto Macros 1 Defining Macros in Scheme Extensibility: A language is extensible if the language can be extended. New Scheme control structures.
Random Bits of Perl None of this stuff is worthy of it’s own lecture, but it’s all a bunch of things you should learn to use Perl well.
Chapter 9 1 Chapter 9 – Part 2 l Overview of Streams and File I/O l Text File I/O l Binary File I/O l File Objects and File Names Streams and File I/O.
Milos Hauskrecht (PDF) Hieu D. Vu (PPT) LISP PROGARMMING LANGUAGE.
2. WRITING SIMPLE PROGRAMS Rocky K. C. Chang September 10, 2015 (Adapted from John Zelle’s slides)
1 Homework Done the reading? –K&R –Glass Chapters 1 and 2 Applied for cs240? (If not, keep at it!) Gotten a UNIX account? (If not, keep at it!)
ICS3U_FileIO.ppt File Input/Output (I/O)‏ ICS3U_FileIO.ppt File I/O Declare a file object File myFile = new File("billy.txt"); a file object whose name.
Printing in Python. Printing Every program needs to do some output This is usually to the screen (shell window) Later we’ll see graphics windows and external.
1 CSC 221: Introduction to Programming Fall 2011 Input & file processing  input vs. raw_input  files: input, output  opening & closing files  read(),
JavaScript Functions. CSS Inheritance Which formatting applies? x y z input { display: block; } input.pref { background:red; } If you have a selector.
LECTURE 16 Functional Programming. WHAT IS FUNCTIONAL PROGRAMMING? Functional programming defines the outputs of a program as a mathematical function.
21-October-2002cse IO © 2002 University of Washington1 Input / Output CSE 413, Autumn 2002 Programming Languages
1 FP Foundations, Scheme In Text: Chapter Chapter 14: FP Foundations, Scheme Mathematical Functions Def: A mathematical function is a mapping of.
PRACTICAL COMMON LISP Peter Seibel 1.
C is a high level language (HLL)
Forms Writing your own procedures CS 480/680 – Comparative Languages.
CSE 425: Functional Programming I Programs as Functions Some programs act like mathematical functions –Associate a set of input values from the function’s.
Input Streams “A program designed for inputs from people is usually stressed beyond the breaking point by computer-generated inputs.” Dennis Ritchie, Bell.
Input/Output Chapters 8 & 9. Character Input n Read-char reads a single character n Read-line reads until the next end-of-line –returns a string n Both.
Quiz 3 Topics Functions – using and writing. Lists: –operators used with lists. –keywords used with lists. –BIF’s used with lists. –list methods. Loops.
CS 152: Programming Language Paradigms February 12 Class Meeting Department of Computer Science San Jose State University Spring 2014 Instructor: Ron Mak.
FILES AND EXCEPTIONS Topics Introduction to File Input and Output Using Loops to Process Files Processing Records Exceptions.
File I/O. I/O Flags Flags are passed to give some information about how the file is to be used. – Read only file – flag=0x0 – Write only file – flag=0x1.
Perl & TCL Vijay Subramanian, Modified from : perl_basics_06.ppt.
CS314 – Section 5 Recitation 9
Functional Programming
Programming what is C++
Topic: File Input/Output (I/O)
Introduction to Computer Science / Procedural – 67130
CS 326 Programming Languages, Concepts and Implementation
Plan for the Day: I/O (beyond scanf and printf)
“The fastest I/O is no I/O.” Nils-Peter Nelson, Bell Labs
slides created by Marty Stepp
Strings and Streams Professor Hugh C. Lauer CS-2303, System Programming Concepts (Slides include materials from The C Programming Language, 2nd edition,
Scheme: Basic Functionality
3.4 Local Binding Recall Scheme's let: > (let ((x 5)‏ (y 6))
COP4020 Programming Languages
The Python interpreter
Defining Macros in Scheme
Presentation transcript:

Input and Output in Scheme CS 480/680 – Comparative Languages

Input/Output2 Basic I/O  (display arg) writes arg to standard out  The second (optional) argument to display is the output-port to write to. You get an output port using open-output- file You can assign it to a symbol: (define i (open-output-file ”myfile”)) (display ”Hello world” i)  Likewise, open-input-file opens a file for reading and returns an input port.

Input/Output3 Reading  These functions return a string: (read-char port) – one character (read-line port) – one line (read port) – one S-expression  When at EOF, the read functions return an eof- object, which you can test with (eof-object? arg)  Write first-line.scheme

Input/Output4 Writing  These functions write to a port: (write-char char port) – one character (write expr port) – one S-expression  expr can be a string, a list, or any other S-expression (display expr port) – like write  Write puts “” around strings, #\ in front of characters, etc.  Display does not (newline port) – writes a #\newline to the port  Create output.scheme

Input/Output5 Automatic Opening and Closing  Just like Ruby allows you to pass a block to open(), Scheme has a mechanism to open a file, run a subform, and automatically close the file at the end: (call-with-input-file "hello.txt" (lambda (i) (let* ((a (read-char i)) (b (read-char i)) (c (read-char i))) (list a b c)))) » (#\h #\e #\l) See read-print.scheme

Input/Output6 String Ports  You can read/write from a string as though it was a file: (define i (open-input-string "hello world")) (read-char i) » #\h (read i) » ello (read i) » world

Input/Output7 Output String Ports  The procedure open-output-string creates an output port that will eventually be used to create a string: (define o (open-output-string)) (write 'hello o) (write-char #\, o) (display " " o) (display "world" o)  The function get-output-string gets the accumulated string associated with the port: (get-output-string o) » "hello, world"

Input/Output8 Loading Files  (load ”filename”) – read each S- expression in a file and evaluate it as a form.  Files can load other files  (load-relative ”filename”) Path is relative to the location of the current file

Input/Output9 Eval and Quote  The special form (eval list) evaluates a list as code: (define a (+ 3 5)) a » 8  Quote prevents a list from being evaluated (define a ‘(+ 3 5)) a » (+ 3 5) (eval a) » 8

Input/Output10 Eval-string  To evaluate a string, you need to use (eval-string string)  Requires the “string.ss” library: (require (lib “string.ss”))  See eval-string.scheme

Input/Output11 Exercises  Write a program that reads in a file of S- expressions containing fully parenthesized mathematical expressions in prefix notation. Your program should print the result of each expression. Input File (+ 3 7) (* (+ 3 7) 5) (+ (* 2 5) (+ 2 3)) Standard Out