Week 2: The Clojure Language. Background Basic structure A few of the most useful facilities. A modernized Lisp. An insider's opinion

Similar documents
Week 5: Background. A few observations on learning new programming languages. What's wrong with this (actual) protest from 1966?

Intro. Scheme Basics. scm> 5 5. scm>

Week 11: Case study: Designing, building, & testing a Person class Background for the Project Needed in many applications Is it possible? practical?

INF4820: Algorithms for Artificial Intelligence and Natural Language Processing. Common Lisp Fundamentals

Early programming languages ca. 1960

Week - 01 Lecture - 04 Downloading and installing Python

CSE341: Programming Languages Lecture 17 Implementing Languages Including Closures. Dan Grossman Autumn 2018

CSC 326H1F, Fall Programming Languages. What languages do you know? Instructor: Ali Juma. A survey of counted loops: FORTRAN

.consulting.solutions.partnership. Clojure by Example. A practical introduction to Clojure on the JVM

CS 360 Programming Languages Interpreters

SCHEME AND CALCULATOR 5b

Chapter 11 :: Functional Languages

SCHEME 7. 1 Introduction. 2 Primitives COMPUTER SCIENCE 61A. October 29, 2015

SCHEME 8. 1 Introduction. 2 Primitives COMPUTER SCIENCE 61A. March 23, 2017

CS61A Discussion Notes: Week 11: The Metacircular Evaluator By Greg Krimer, with slight modifications by Phoebus Chen (using notes from Todd Segal)

MetacircularScheme! (a variant of lisp)

Summer 2017 Discussion 10: July 25, Introduction. 2 Primitives and Define

CSE413: Programming Languages and Implementation Racket structs Implementing languages with interpreters Implementing closures

CS 11 java track: lecture 1

CS61A Notes Week 1A: Basics, order of evaluation, special forms, recursion

Fortunately, you only need to know 10% of what's in the main page to get 90% of the benefit. This page will show you that 10%.

Week 12: Priority queues Heaps and heap operations

CPL 2016, week 10. Clojure functional core. Oleg Batrashev. April 11, Institute of Computer Science, Tartu, Estonia

Intro to Programming. Unit 7. What is Programming? What is Programming? Intro to Programming

Typical workflow. CSE341: Programming Languages. Lecture 17 Implementing Languages Including Closures. Reality more complicated

CS103 Spring 2018 Mathematical Vocabulary

Project 5 - The Meta-Circular Evaluator

Spring 2018 Discussion 7: March 21, Introduction. 2 Primitives

1.3.4 case and case* macro since 1.2. Listing Conditional Branching, Fast Switch. Listing Contract

CircuitPython 101: Working with Lists, Iterators and Generators

6.001 Notes: Section 15.1

Fall 2017 Discussion 7: October 25, 2017 Solutions. 1 Introduction. 2 Primitives

The Stack, Free Store, and Global Namespace

Project 5 - The Meta-Circular Evaluator

6.001 Notes: Section 8.1

MITOCW watch?v=0jljzrnhwoi

Functional programming in LISP

Lesson 3 Transcript: Part 1 of 2 - Tools & Scripting

Fall 2018 Discussion 8: October 24, 2018 Solutions. 1 Introduction. 2 Primitives

SCHEME The Scheme Interpreter. 2 Primitives COMPUTER SCIENCE 61A. October 29th, 2012

Week 7 Prolog overview

Lists, loops and decisions

PROFESSOR: Last time, we took a look at an explicit control evaluator for Lisp, and that bridged the gap between

So on the survey, someone mentioned they wanted to work on heaps, and someone else mentioned they wanted to work on balanced binary search trees.

Functional Languages. CSE 307 Principles of Programming Languages Stony Brook University

MITOCW watch?v=kz7jjltq9r4

First Java Program - Output to the Screen

Modern Programming Languages. Lecture LISP Programming Language An Introduction

A lot of people make repeated mistakes of not calling their functions and getting errors. Make sure you're calling your functions.

Instructor: Craig Duckett. Lecture 04: Thursday, April 5, Relationships

Functional Programming


FP Foundations, Scheme

Intro. Speed V Growth

Administration. Conditional Statements. Agenda. Syntax. Flow of control. Lab 2 due now on floppy Lab 3 due tomorrow via FTP

Scala Style Guide Spring 2018

What did we talk about last time? Examples switch statements

Skill 1: Multiplying Polynomials

The Curious Clojureist

Learning to Program with Haiku

Seminar on Languages for Scientific Computing Aachen, 6 Feb Navid Abbaszadeh.

Scheme: Expressions & Procedures

Mastering Python Decorators

Ruby: Introduction, Basics

Why do we have to know all that? The stored program concept (the procedural paradigm) Memory

Mathematical Logic Part One

Nano-Lisp The Tutorial Handbook

CS457/557 Functional Languages

What's that? Why? Is one "better" than the other? Terminology. Comparison. Loop testing. Some experts (e.g. Pezze & Young) call it structural testing

MITOCW watch?v=zm5mw5nkzjg

MITOCW watch?v=rvrkt-jxvko

4. Java Project Design, Input Methods

Functions. Nate Foster Spring 2018

6.001 Notes: Section 1.1

CS 11 Haskell track: lecture 1

Fifth Generation CS 4100 LISP. What do we need? Example LISP Program 11/13/13. Chapter 9: List Processing: LISP. Central Idea: Function Application

4/19/2018. Chapter 11 :: Functional Languages

Instructor: Craig Duckett. Lecture 02: Thursday, March 29 th, 2018 SQL Basics and SELECT, FROM, WHERE

Functional Programming. Big Picture. Design of Programming Languages

Math Modeling in Java: An S-I Compartment Model

1 Getting used to Python

Quiz 3; Tuesday, January 27; 5 minutes; 5 points [Solutions follow on next page]

printf( Please enter another number: ); scanf( %d, &num2);

Compilers. Prerequisites

Racket Style Guide Fall 2017

Documentation for LISP in BASIC

1 Lexical Considerations

Shell Scripting. Todd Kelley CST8207 Todd Kelley 1

A Brief Introduction to Scheme (I)

Quote of the Day. CS Functional Programming. Introductory Notes on Lisp/Clojure. Another Quote. What is Clojure? 1-4

Troubleshooting Maple Worksheets: Common Problems

Today. CSE341: Programming Languages. Late Binding in Ruby Multiple Inheritance, Interfaces, Mixins. Ruby instance variables and methods

In Java, data type boolean is used to represent Boolean data. Each boolean constant or variable can contain one of two values: true or false.

SKILL AREA 304: Review Programming Language Concept. Computer Programming (YPG)

CONTENTS: What Is Programming? How a Computer Works Programming Languages Java Basics. COMP-202 Unit 1: Introduction

Clojure. The Revenge of Data. by Vjeran Marcinko Kapsch CarrierCom

Discussion 12 The MCE (solutions)

Chapter 1 Getting Started

Lecture Set 2: Starting Java

Symbolic Programming. Dr. Zoran Duric () Symbolic Programming 1/ 89 August 28, / 89

Transcription:

Week 2: The Clojure Language Background Basic structure A few of the most useful facilities A modernized Lisp Review of Lisp's origins and development Why did Lisp need to be modernized? Relationship to Java COMP 378 -- Spring. 2017 Mr.. Weisert An insider's opinion "In the study of Artificial Intelligence, the reason for learning Lisp is roughly that for learning French if you were going to France it's the native language." --Charniak & McDermott, 1985, p. 33 What do they mean by "native language"? Do we believe that? Their explanation "First, Lisp is more flexible than most languages. Users have such total control over what goes on that if they do not like the syntax of the language, they may change it to suit themselves...." "The second reason for choosing Lisp is the way in which Lisp is oriented toward the manipulation of symbols as opposed to, say, numbers." From today's point of view that's overstated, but it explains how Lisp came to dominate the field. COMP 370, Spring 2017 1-4

Homoiconicity What's that? Lisp code is homoiconic http://en.wikipedia.org/wiki/homoiconicity Code is data. Programs can: Create and execute other programs on the fly Modify themselves. This is a very powerful concept, but also hard to manage. cf. assembly language code So what's wrong with Lisp? Too many dialects. Standardization came late and many "improved" versions of Lisp were being widely used. ANSI Common Lisp (1994) Too many parentheses hard to read error prone to code Slow execution Interpretive rather than compiled execution Original Lisp was naively machine specific Keywords referred to specific properties of the IBM 704 internal instruction format! http://www.idinews.com/history/carcdr.html Clojure objectives and advantages over Lisp Fewer parentheses (but still a lot) Faster execution Compiles to Java bytecode. But it remains homoiconic! Is that possible? Full access to Java capabilities Do we care about that? Why? When? Unlike Lisp, not specifically for A.I, but a general-purpose programming language Is that good? The choice for this course We're going to use Clojure instead of Lisp for our examples because: It avoids some of the ugliness of Lisp (But Clojure is still a little ugly and error-prone) It's available and free. It works on any computer that has a JVM. It generates moderately efficient code They're similar enough that if you know Clojure you can pick up Lisp very quickly if you ever need it. COMP 370, Spring 2017 5-8

Review We've already seen (session 1) that: Code and data are both written as lists A function invocation is a list in which the first element names the function and the rest of the elements are the arguments: Example: (power 2 24) Commas are allowed but not needed. Simple (i.e. short, one-line) functions can be invoked (and defined) from the REPL command line. So how do we create and use more interesting functions? Getting started This little web-based tutorial http://www.tryclj.com/ is a very simple introduction to command-line (REPL) dialogues It prompts you to follow specific instructions, but you can also experiment a bit on your own. It may help you to get used to the behavior of Clojure and the strange syntax. Note the error messages (from Java!) when you enter something illegal. Some of them are clear. Others aren't but you'll get used to them Functions (basics, but ugly) Running a Lisp or Clojure program consists of executing a defined top-level function, which may, of course, invoke other functions. To define a function (fn [args] returned-value) but it has no name, an anonymous function! You can define (fn [x] (* x x)) but it doesn't do anything useful. You can say ((fn [x] (* x x)) 8) and get the result 64, but that's tedious, inelegant, and error-prone. When might you ever define and use an anonymous function? Functions (named) You could combine the facility for defining a symbol and the definition of an anonymous function: (def square (fn [x] (* x x )) ) and then invoke it (square 8) but there's an easier and clearer way. (defn square [x] (* x x )) or more generally (defn fctnname [arglist] value) Note: defn is a macro (but do we care?) COMP 370, Spring 2017 9-12

A trivial interaction with REPL (from the tryclj tutorial) > (defn square [x] (* x x)) #'sandbox34619/square > (square 0.5) 0.25 > (square 9) 81 Looks fine! What could possibly go wrong? An irritating setback After experimenting with other Clojure features, we try to repeat the earlier use of square > (square 9) java.lang.illegalstateexception: Attempting to call unbound fn: #'sandbox30082/square We somehow lost our working directory! None of the functions we've defined can be found! There's very likely a simple answer to what happened and an easy way of recovering, but we may have to wait until next week to discover exactly what to do. Fortunately you don't have to use Clojure this week. But we can still continue exploring Clojure We have to shut down the "try Clojure" web page and restart it. That's irritating, but later we'll learn how to avoid whatever we did. (I suspect a bug in the REPL) Fortunately, everything is working again. Other tools have similar irritating peculiarities, but we've gotten used to them. Another simple function (defn average [numbers] (/ (apply + numbers) (count numbers))) #'sandbox7982/average > (average [60 80 100 400]) 160 What's different about the above from the usual REPL interaction? COMP 370, Spring 2017 13-16

Let's explore more Clojure Let's focus on conditionals, because they play a central role in inference engines and other A.I. applications. But Clojure and Lisp experts warn us to think of conditional evaluation (if, cond, when, etc.) not as controlling sequence of execution but just as determing precedence of definition. Don't worry too much about that. In many situations (esp. with immutable functions) it amounts to the same thing. Testing truth value As in LISP, the principal and most useful conditional facility is cond It uses fewer parentheses than the LISP version, but it's just as powerful and flexible. Yes, it's a function, not an imperative operator or command verb. Actually it's a macro on top of if Easier to organize and read complicated code than using if directly. We'd need it if we wanted to use Clojure for assignment #1 (but that wasn't required) if tests a single condition Two or three operands: (if condition trueresult falseresult) (if condition trueresult) The condition is not necessarily a pure Boolean expression. Anything other than nil or false is considered true! Is that a help or a confusion? If no falseresult is specified and condition is false, the result is nil. cond tests multiple conditions "Takes a set of test/expr pairs. It evaluates each test one at a time. If a test returns logical true, cond evaluates and returns the value of the corresponding expr and doesn't evaluate any of the other tests or exprs. (cond) returns nil." - from the Clojure.docs web site Here's a simple example: (cond (age < 18) juvenile (age < 23) student (age < 65) adult : else senior) Note: Unlike switch case in other languages the clauses needn't test values of the same variable. Often used effectively with recursive function invocation (more next week) COMP 370, Spring 2017 17-20

Improving on Lisp The Clojure conditional (cond (age < 18) juvenile (age < 23) student (age < 65) adult : else senior) is simpler and less error-prone than the Lisp version: (cond ((age < 18) juvenile) ((age < 23) student) ((age < 65) adult) ( T senior)) Another syntax improvement When Clojure or Lisp sees a list, it wants to evaluate it: The first element names a function The remaining elements are the arguments But sometimes a list is just data. How can we suppress evaluation? Suppressing list evaluation In early Lisp you'd say quote (list) That still works in Clojure, but that's ugly and tedious to code. It originated when character sets were limited. Today we prefer to say the more readable '(list) Also Clojure supports vectors [1 2 3 4] for example as function parameter lists and some special forms that don't evaluate the first element as a function call. Summary We've shown a few of the most common and most useful features of Clojure. From here on you should be able to read and understand the Matuszek handbook (web link from the course schedule page) Let's look it over now Starting next week, we'll concentrate on A.I. concepts and coding. See schedule page (may be revised during the course, depending on students' interests and experience). COMP 370, Spring 2017 21-24