POETRY OF PROGRAMMING CODE READING EXERCISES IN CLOJURE V

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

Poetry of Programming

Java, with a Clojure

SCHEME 10 COMPUTER SCIENCE 61A. July 26, Warm Up: Conditional Expressions. 1. What does Scheme print? scm> (if (or #t (/ 1 0)) 1 (/ 1 0))

CS558 Programming Languages

Clojure Concurrency Constructs. CSCI 5828: Foundations of Software Engineering Lecture 12 10/02/2014

This tutorial is designed for all those software professionals who are keen on learning the basics of Clojure and how to put it into practice.

Homework 6: Higher-Order Procedures Due: 11:59 PM, Oct 16, 2018

VARIABLES AND TYPES CITS1001

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

PERSISTENT SEQUENCES WITH EFFECTIVE RANDOM ACCESS AND SUPPORT FOR INFINITY

CS558 Programming Languages

Clojure. A (not-so-pure) functional approach to concurrency. Paolo Baldan Linguaggi per il Global Computing AA 2016/2017

Please write your name and username here legibly: C212/A592 6W2 Summer 2017 Early Evaluation Exam: Fundamental Programming Structures in Java

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

Introduction to Logic Programming. Ambrose

Lab 10: OCaml sequences, comparators, stable sorting 12:00 PM, Nov 12, 2017

Module 8: Local and functional abstraction

The syntax and semantics of Beginning Student

The syntax and semantics of Beginning Student

CS 320: Concepts of Programming Languages

Statistics Case Study 2000 M. J. Clancy and M. C. Linn

Interpreters and Tail Calls Fall 2017 Discussion 8: November 1, 2017 Solutions. 1 Calculator. calc> (+ 2 2) 4

Lazytest Better Living Through Protocols. Stuart Sierra. Clojure NYC April 15, 2010

Clojure is. A dynamic, LISP-based. programming language. running on the JVM

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

LISP. Everything in a computer is a string of binary digits, ones and zeros, which everyone calls bits.

Clojure: Enemy of the State

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

CS 61A Interpreters, Tail Calls, Macros, Streams, Iterators. Spring 2019 Guerrilla Section 5: April 20, Interpreters.

CSE413 Midterm. Question Max Points Total 100

INTERPRETERS 8. 1 Calculator COMPUTER SCIENCE 61A. November 3, 2016

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

Functional Programming Languages (FPL)

Homework 6: Higher-Order Procedures Due: 10:00 PM, Oct 17, 2017

CSE 341, Spring 2011, Final Examination 9 June Please do not turn the page until everyone is ready.

Announcement. Overview. LISP: A Quick Overview. Outline of Writing and Running Lisp.

Common LISP-Introduction

CS115 - Module 9 - filter, map, and friends


Common LISP Tutorial 1 (Basic)

Scheme: Data. CS F331 Programming Languages CSCE A331 Programming Language Concepts Lecture Slides Monday, April 3, Glenn G.

Symbolic Computation and Common Lisp

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

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

Exercise 1: Graph coloring (10 points)

Functions that return lists

Functional Programming

CSc 372, Spring 1996 Mid-term Examination Solution Key

Homework 3: Recursion Due: 11:59 PM, Sep 25, 2018

Constructing Algorithms and Pseudocoding This document was originally developed by Professor John P. Russo

6.001 Notes: Section 15.1

cs1114 REVIEW of details test closed laptop period

Repetition Through Recursion

Module 3: New types of data

Dept. of CSE, IIT KGP

Reagent. a ClojureScript interface to React. React Amsterdam Meetup 12 Feb. 2015

Fall 2017 December 4, Scheme. Instructions

LISP - SEQUENCES. The function make-sequence allows you to create a sequence of any type. The syntax for this function is:

Functional abstraction. What is abstraction? Eating apples. Readings: HtDP, sections Language level: Intermediate Student With Lambda

Functional abstraction

Principles of Programming Languages 2017W, Functional Programming

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

FUNCTIONAL PROGRAMMING 1 HASKELL BASICS

Typed Racket: Racket with Static Types

STREAMS 10. Basics of Streams. Practice with Streams COMPUTER SCIENCE 61AS. 1. What is a stream?

Scheme: Strings Scheme: I/O

Lab 7: OCaml 12:00 PM, Oct 22, 2017

TAIL RECURSION, SCOPE, AND PROJECT 4 11

Getting started with Java

A Second Look At ML. Chapter Seven Modern Programming Languages, 2nd ed. 1

At full speed with Python

CSC324- TUTORIAL 5. Shems Saleh* *Some slides inspired by/based on Afsaneh Fazly s slides

LISP Programming. (23 (this is easy) hello 821)

CSc 520 Principles of Programming Languages

User-defined Functions. Conditional Expressions in Scheme

Control Flow. COMS W1007 Introduction to Computer Science. Christopher Conway 3 June 2003

Chapter 5 Errors. Bjarne Stroustrup

Section 2.4: Arguments with Quantified Statements

Exercise: Inventing Language

Fall 2017 December 4, 2017

How to Design Programs

Welcome to CS 135 (Winter 2018)

Introduction to Programming, Aug-Dec 2006

Python Evaluation Rules

CSE 341 Section Handout #6 Cheat Sheet

CSE341 Spring 2017, Final Examination June 8, 2017

CSE341: Programming Languages Lecture 9 Function-Closure Idioms. Dan Grossman Winter 2013

COMP 105 Homework: Type Systems

SCHEME AND CALCULATOR 5b

Introduction to C++ with content from

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

Macros & Streams Spring 2018 Discussion 9: April 11, Macros

Announcements. The current topic: Scheme. Review: BST functions. Review: Representing trees in Scheme. Reminder: Lab 2 is due on Monday at 10:30 am.

The Typed Racket Guide

Computer Science E-119 Fall Problem Set 3. Due before lecture on Wednesday, October 31

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

RACKET BASICS, ORDER OF EVALUATION, RECURSION 1

CS 320 Homework One Due 2/5 11:59pm

CS11 Java. Fall Lecture 1

Transcription:

POETRY OF PROGRAMMING CODE READING EXERCISES IN CLOJURE V2018.6.6 Contents 1. Introduction 1 1.1. Why do we need to read code? 1 1.2. Instructions 2 1.3. Recommended method 2 2. Function composition first 2 3. Arithmetic done with functions 3 4. Asking yes-or-no questions: predicates 4 5. Strings 6 6. List, the most fundamental collection 8 7. Vectors 9 8. Making memories 11 9. Defining functions 12 10. Manual function calls 13 11. Lazy lists of numbers 13 12. Functional collection transformation 14 13. Selections 15 14. Conditionals 15 15. Reduce 16 16. Hash-map 17 17. Hash-sets 18 18. Sequence abstraction 19 19. Iteration 19 Sections correspond to chapters of the book https://egri-nagy.github.io/popbook/. 1. Introduction 1.1. Why do we need to read code? Reading code is executing the program in our head. It is a very important skill, not just for programs written by others. When trying to solve a problem, we seldom get it right at first try. So we should ask What have I said here?, that is reading the code just written back. Common mistake is to be convinced that we wrote exactly what we wanted. In order to write programs we need to know the basic building blocks very well. The following exercises all focus on a single aspect of a data structure, a function or a special form. Solving these will provide the background knowledge in the sense of knowing the tools. These exercises alone will not teach how to write code. One has to solve actual problems for that. Most questions are typical usage examples, but some of them are twisted (if not wicked).

2 CODE READING EXERCISES IN CLOJURE V2018.6.6 1.2. Instructions. What is the output?, What does the expression evaluate to? The task is to figure out the meaning of the piece of code in the box, which is the result of the computation. Therefore, there is only a single correct answer, the exact output. Each box is a separate question, definitions and symbol bindings are only valid in the box they appear in. When the expression leads to an error message, it is enough to state the error, no need to specify which exception is thrown. Indicate the type of the output clearly. For example, sequence operations return sequences that are lists. When the output is (1 2 3), writing 1 2 3 or [1 2 3] instead are not acceptable answers. Similarly, when the output is "hello", then writing hello is incorrect. When the answer is true, writing yes will not earn a mark. 1.3. Recommended method. First, solve these problems without the computer. Write down the answers, then check them in a REPL by evaluating the code snippets. If your answer is different, then check your notes or textbook or documentation for the given function and data structure. Simply copy-pasting the expressions into the REPL without thinking achieves very little learning. This is also the reason for not giving the solutions in this document. Here are some hints for building up reading skills. It makes sense to keep asking these questions. Data literals (e.g. numbers, strings, characters) are easy to read. They just evaluate to themselves. Their meaning is immediate. For a symbol, we need to find what does it bind to? What is the meaning? Is it a function? Is it a data item? We need to go backwards to find where the symbol gets defined. Is it in a let statement? Is it defined in the environment? Is it an argument to a function? If the expression is composite, work from the inside out. Evaluate the sub-expressions first. Make notes. Rewrite the full expression by substituting the values for the subexpressions. If a function is called, what are its input arguments? Does the function expect a number of arguments? or a single collection? 10 10 (identity 10) 10 (inc 2) 3 (dec 9) 8 (inc (dec 0)) 0 ((comp inc dec) 0) 2. Function composition first

POETRY OF PROGRAMMING 3 0 (dec (dec 0)) -2 ((comp dec dec dec) 0) -3 ((comp (comp dec dec) (comp dec dec)) 5) 1 ((comp identity identity) 1) 1 ((comp identity inc) 1) 2 (inc 11.11) 12.11 (dec 0.01) -0.99 ((comp inc) 1) 2 ((comp) 42) 42 (((comp comp comp) dec dec) 1) -1 3. Arithmetic done with functions (+ (- 6 4) (/ 6 3)) 4 (+ 1 (* 2 (- 1 (/ 12 4)))) -3 (+) 0 (-) ArityException Wrong number of args (0) passed to: core/- (*) 1 (/)

4 CODE READING EXERCISES IN CLOJURE V2018.6.6 ArityException Wrong number of args (0) passed to: core// (+ 3) 3 (* 5) 5 (/ 3) 1/3 (- 5) -5 (+ (* 5 2) (- 4 3) (*)) 12 ((comp - *) 2 3 4) -24 ((comp / *) 2 3) 1/6 (* (inc 1) (inc (inc 1))) 6 (/ 10 4) 5/2 (/ 10.0 4) 2.5 (/ 10 4.0) 2.5 (zero? 0) true (zero? 1) false (pos? 1) true (pos? 1111) true (pos? 0) 4. Asking yes-or-no questions: predicates

POETRY OF PROGRAMMING 5 false (neg? 0) false (neg? -2) true (= (+ 1 2 3) (* 1 2 3)) true (<= 2 2 2) true (<= 2 1 2) false (< 2 1 2) false (< 2 2 2) false (< 2 3 4) true (> 2 3) false (> 2 2) false (>= 2 2) true (fn? +) true (fn? -) true (fn? identity) true (fn? (+)) false (number? (+)) true

6 CODE READING EXERCISES IN CLOJURE V2018.6.6 (rational? (/ 7 3)) true (rational? 2) true (float? 2) false (float? 2.0) true (integer? (+ 1 2 3 4 5)) true (integer? (+ 1 2 3.0 4 5)) false (float? (+ 1 2 3.0 4 5)) true (number? 12.1) true (number? 0) true (number? (/ 1 19)) true (char? \x) true (char? \space) true (char? \8) true (= 9 \9) false (string? "Granny Weatherwax") true (string? " ") true 5. Strings

(string? "") true (string? "12") true (number? "12") false (= \space " ") false (= (str \space) " ") true (str \1 2 "3" (- 5 1)) "1234" (str "The answer:" 42) "The answer:42" (str "The answer: " 42) "The answer: 42" (str "The answer: " 42 ".") "The answer: 42." (str "The answer: " (* 6 7) ".") "The answer: 42." POETRY OF PROGRAMMING 7 WARNING! The following questions assume that the string library is loaded by (require [clojure.string :as string]). (string/upper-case "helloooo!") "HELLOOOO!" (string/capitalize (string/lower-case "HELLO!")) "Hello!" (string/ends-with? "mango" "go") true (string/ends-with? "mango" "GO") false (string/starts-with? "How to solve it?" "?") false (string/replace "banana" "a" "e") "benene"

8 CODE READING EXERCISES IN CLOJURE V2018.6.6 (string/replace (string/replace "banana" "a" "-") "-n" "x") "bxx-" 6. List, the most fundamental collection '(1 2 3) (1 2 3) (1 2 3) ClassCastException java.lang.long cannot be cast to clojure.lang.ifn (list 4 5) (4 5) (list 4 5 '(6 7)) (4 5 (6 7)) (list 4 5 '(6 7 (8 9))) (4 5 (6 7 (8 9))) (cons 11 '(19 13)) (11 19 13) (cons 7 (cons 5 (list 3 4))) (7 5 3 4) (first '(8 3 5)) 8 (first '()) nil (rest (5 3 8)) ClassCastException java.lang.long cannot be cast to clojure.lang.ifn (rest '(5 3 8)) (3 8) (rest '()) () ((comp rest rest) '(2 3 4)) (4) ((comp first rest) '(2 3 4)) 3 ((comp first first) '(2 3 4))

POETRY OF PROGRAMMING 9 IllegalArgumentException Don t know how to create ISeq from: java.lang.long (last '(9 3 2)) 2 (last '()) nil (reverse '(1 2 3)) (3 2 1) ((comp first reverse) '(1 2 3)) 3 (empty? ()) true (empty? '(1)) false (count ()) 0 (count (list \a \b)) 2 (concat '(1) '(3) '(2)) (1 3 2) (concat) () (vector \a \b \c) [\a \b \c] 7. Vectors (vector [1 2]) [[1 2]] (vec [1 2]) [1 2] (vec '(\a \b)) [\a \b] (vec []) []

10 CODE READING EXERCISES IN CLOJURE V2018.6.6 (vector []) [[]] (count [1 "two" 3]) 3 (count []) 0 (count [[]]) 1 (count [[[]]]) 1 (count [[[[[[[]]]]]]]) 1 (count [[][][]]) 3 (nth [\a \b \c] 0) \a (nth [\a \b \c] 2) \c (nth [\a \b \c] 3) IndexOutOfBoundsException ([\a \b \c] 0) \a ([\a \b \c] 1) \b ([\a \b \c] 3) IndexOutOfBoundsException (nth [] 0) IndexOutOfBoundsException (conj [] 1) [1] (conj [] 1 2) [1 2]

POETRY OF PROGRAMMING 11 (conj [] 1 2 3) [1 2 3] (conj [\a \b] 3) [\a \b 3] (conj [] []) [[]] (conj [] [] []) [[] []] (conj [1 2] [3]) [1 2 [3]] (vec (range 3)) [0 1 2] (vector (range 3)) [(0 1 2)] ((comp ["hello" "world" "!"] [2 1 0]) 2) "hello" (def a 2) (* 3 a) 6 (def a 5) (def b 7) (* a b) 35 (= 'x "x") false (def x "x") (= "x" x) true (def x "grape") (def x "apple") x "apple" 8. Making memories (let [x 2] (* 3 x)) 6

12 CODE READING EXERCISES IN CLOJURE V2018.6.6 (let [x 2, y 7] (* y x)) 14 (let [s "world"] (str "Hello " s "!")) "Hello world!" (let [s "everyone"] "Hello world!") "Hello world!" (def x 2) (let [x 100] (inc x)) 101 (def x 2) (let [x 100] (inc x)) x 101 2 (let [x 0 y (inc x) z (dec y)] [x y z]) [0 1 0] 9. Defining functions ((fn [x] (+ x 2)) 0) 2 ((fn [x] (+ x 2)) 1) 3 ((fn [x y] (+ x (* 2 y))) 1 2) 5 ((fn [x] (+ x (* 2 y))) 1 2) Unable to resolve symbol: y in this context (def x 31) ((fn [x] (+ x 2)) 1) 3 (defn f [s] (str "Input was " s ".")) (f 42) "Input was 42." (defn f [n] (* 3 n)) ((comp inc f) 1) 4

POETRY OF PROGRAMMING 13 10. Manual function calls (+ [1 2 3]) Cannot cast clojure.lang.persistentvector to java.lang.number (apply + [1 2 3]) 6 (max 5 2 7 4) 7 (max [5 2 7 4]) [5 2 7 4] (apply max [5 2 7 4]) 7 (apply str [\h \i]) "hi" (range 2) (0 1) 11. Lazy lists of numbers (range 1 2) (1) (range 1 2 3) (1) (range 2 1 3) () (range 0 1 0.4) (0 0.4 0.8) (take 2 (drop 2 (range))) (2 3) (take 3 '(3 3 3 3)) (3 3 3) (take-while neg? (range -2 2)) (-2-1) (drop-while neg? (range -2 2)) (0 1)

14 CODE READING EXERCISES IN CLOJURE V2018.6.6 (take-while zero? '(-1 0 1)) () (drop-while zero? '(-1 0 1)) (-1 0 1) (drop-while zero? '(-1 0 1-1)) (-1 0 1-1) (drop-while neg? '(-1 0 1-1)) (0 1-1) (count (take 5 (range))) 5 12. Functional collection transformation (map inc [1 2 3 4 5]) (2 3 4 5 6) (map dec [1 2 3 4 5]) (0 1 2 3 4) (map range [2 3]) ((0 1) (0 1 2)) (mapcat range [2 3]) (0 1 0 1 2) (map str [1 2 3]) ("1" "2" "3") (map reverse [[1 2] [3 4]]) ((2 1) (4 3)) (map reverse (map range [1 2 3])) ((0) (1 0) (2 1 0)) (map even? (range 5)) (true false true false true) (map odd? [0 1 2 3]) (false true false true) (map str [1 \a "2" ()]) ("1" "a" "2" "()")

POETRY OF PROGRAMMING 15 (filter even? [10 11 12 13]) (10 12) 13. Selections (filter odd? [10 11 12 13]) (11 13) (filter even? [1 3 5 7]) () (filter number? [1 "2" \3]) (1) (filter string? [1 "2" \3]) ("2") (filter char? [1 "2" \3]) (\3) (remove nil? [[] nil () 0]) ([] () 0) (remove even? (range 6)) (1 3 5) (filter char? [1 \a 2 \b]) (\a \b) (if true 42 24) 42 (if false 42 24) 24 14. Conditionals (let [x 2] (cond (string? x) "just a word" (= x 2) "two" true "not two")) "two" (let [x 4] (cond (string? x) "just a word" (= x 2) "two" true "not two")) "not two"

16 CODE READING EXERCISES IN CLOJURE V2018.6.6 (let [x "2"] (cond (string? x) "just a word" (= x 2) "two" true "not two")) "just a word" (not nil) false (not 42) true (and false nil) false (or false nil) nil (and 1 2) 2 (or 1 2) 1 (reduce conj [9] [2 3] ) [9 2 3] (reductions conj [9] [2 3] ) ([9] [9 2] [9 2 3]) 15. Reduce (into [9] [2 3] ) [9 2 3] (into '(1 2) '(3 4 5)) (5 4 3 1 2) (reductions conj '(1 2) '(3 4 5)) ((1 2) (3 1 2) (4 3 1 2) (5 4 3 1 2)) (reduce max 0 [2 5 3]) 5 (reduce max 6 [2 5 3]) 6 (reductions max 0 [2 5 3]) (0 2 5 5)

POETRY OF PROGRAMMING 17 (reductions max 6 [2 5 3]) (6 6 6 6) (defn rf [v x] (if (odd? x) (conj v x) (conj v (dec x)))) (reduce rf [] [1 2 3 4 5]) (defn rf [v x] (if (even? x) (conj v x) (conj v (inc x)))) (reduce rf [] [1 2 3 4 5]) [2 2 4 4 6] (reduce * [1 2 3]) 6 (reduce + [1 3]) 4 (hash-map \a \b \b \a) {\a \b, \b \a} 16. Hash-map (hash-map :x 10 :y 11) {:y 11, :x 10} ({1 2 3 4} 3) 4 ({1 2 3 4} 2) nil (zipmap (range 4) (reverse (range 4))) {0 3, 1 2, 2 1, 3 0} (hash-map :a 1 :b 2 :c) IllegalArgumentException No value supplied for key: :c (:a {:a "1" :b 2}) "1" (1 {1 2 3 4}) ClassCastException java.lang.long cannot be cast to clojure.lang.ifn ({:a 1 :b 2} :c) nil

18 CODE READING EXERCISES IN CLOJURE V2018.6.6 ({:a 1 :b 2} :c :nothing) :nothing (get {:a 1 :b 2} :c) nil (get {:a 1 :b 2} :c "No such key!") "No such key!" (map {1 "one" 2 "two"} [0 1 2 3]) (nil "one" "two" nil) (assoc {:a 3} :b 2) {:a 3, :b 2} (keys {:title "Solaris" :author "Stanislaw Lem" :year 1961}) (:title :author :year) (vals {:title "Solaris" :author "Stanislaw Lem" :year 1961}) ("Solaris" "Stanislaw Lem" 1961) (hash-set "hello" 17 \c) #{\c "hello" 17} 17. Hash-sets (set [81 72]) #{72 81} (hash-set [6 28 496]) #{[6 28 496]} (apply hash-set [6 28 496]) #{496 6 28} (set [6 28 496]) #{496 6 28} (sorted-set "helloooooo!!") #{"helloooooo!!"} (apply sorted-set "helloooooo") #{\e \h \l \o}

POETRY OF PROGRAMMING 19 (seq "coffee") (\c \o \f \f \e \e) 18. Sequence abstraction (seq (zipmap (range 4) [\a \b \c \d])) ([0 \a] [1 \b] [2 \c] [3 \d]) (seq {:a 3 :b 2}) ([:a 3] [:b 2]) (seq [1 2 3 4]) (1 2 3 4) (count {:a 3 :b 4}) 2 (take 4 (iterate inc 2)) (2 3 4 5) 19. Iteration (take 4 (iterate dec 0)) (0-1 -2-3) (take 8 (iterate (fn [x] (* 2 x)) 1)) (1 2 4 8 16 32 64 128) (take 4 (iterate (fn [v] (conj v 1)) [])) ([] [1] [1 1] [1 1 1])