Giter Club home page Giter Club logo

pset3-lab's Introduction

PSET 3 Lab: Macros!

Due date: November 17.

Problem 1: Unless

Alter the syntax of your language! Implement unless.

Has the same semantics as if, but executes the first branch if the predicate is false and the second branch if the predicate is true.

Problem 2: From

Implement the macro from. The macro binds a symbol and can perform the operations where, orderby, and select over the data it queries.

See the test case under test/pset3/core_test.clj for more details.

Problem 3: With File

Write with-file. It takes a filename, and binds it to the symbol file. Within the macro body, file refers to the java.io.File object.

See the test case under test/pset3/core_test.clj for more details.

Problem 4: Debugging!

What's wrong with this macro?

(defmacro square [n]
  `(* ~n ~n))

Give us a failing case and write a corrected version of the macro!

Problem 5: More debugging

Consider the following numeric if macro, which executes pos, zero, or neg, base on the sign of val.

(defmacro nif [val pos zero neg]
  `(let [~'res ~val]
     (cond (pos? ~'res) ~pos
           (zero? ~'res) ~zero
           :else ~neg)))

The fact below holds true as is:

(nif -1
     "positive"
     "zero"
     "negative") => "negative"

However, this next fact doesn't. Identify the bug and fix it.

 (let [res (java.util.Scanner. (java.io.FileInputStream. "project.clj"))]
   (do (nif 0
            "positive"
            (prn (.nextLine res))
            (prn "negative"))
       (.close res))) => nil

Bonus: Monad comprehension

The construct of monads play a central role in the Haskell programming language, where they underpin the IO system and many other parts of the language. At a high level, a monad allows for composable computation descriptions. For some helpful resources, see

Consider the following list monad:

(def list-monad
  {:return (fn [v] [v])
   :bind (fn [mv f]
           (if (seq mv)
             (apply concat (map f mv))
             []))})

Using the above implementation, the fact below holds true:

(let [bind (:bind list-monad)
      return (:return list-monad)]
  (-> [1 2]
      (bind (fn [a]
              (-> [a (- a)]
                  (bind (fn [b]
                          (return (* 3 b))))))))) => '(3 -3 6 -6)

Now, write a macro, domonad, that permits a nicer monad comprehension syntax as follows:

(domonad list-monad
  [a [1 2]
   b [a, (- a)]]
   (* 3 b)) => '(3 -3 6 -6)

Source: Leonardo Borges's Macro Workshop in Clojure

pset3-lab's People

Contributors

jiangts avatar acganesh avatar

Watchers

James Cloos avatar

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    ๐Ÿ–– Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. ๐Ÿ“Š๐Ÿ“ˆ๐ŸŽ‰

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google โค๏ธ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.