Jim Lynch Codes
  • Blog
  • About Jim

Writings about one coder's stories & experiences.

Def Vs Let In Clojure

11/4/2017

0 Comments

 
I have a bad habit of starting long, ambitious blogs posts but not finishing them. Hopefully this will be be a short and easy one. This post is about the difference between def and let in Clojure!

What Are Def And Let

The phrases def and let are called "special  forms" in the Clojure docs. I was a bit confused by this at first. Are they functions? Are they macros? Or something "more special"? Well, after asking in the Clojurian slack beginner channel one of the memebers gave me a pretty good explanation- "special forms are implemented in java as part of the compiler. They are outside the normal things that are all implemented as regular java classes or as clojure code - they are special symbols the compiler looks for that guide the compilation of code".

Def

After a quick look at the Clojure docs for def, we can see that this special form pretty much creates a global variable within the current namespace. In the docs they note that many macros expand into def like "defn" or "defmacro". Intuitively, it makes sense that "defn" lets you define a function that you can use later anywhere in the same namespace. 

Let

Let is very similar to def in that it is a special form that allows you to bind values to named variables. According to this nice comment in the first example of the Clojure docs for let page, let provides a way to create ;; lexical bindings of data structures to symbols, and the binding is available only within the lexical context of the let. In other words, you can define variables which are "local variables" that are only available within the parentheses surrounding the let.

That's All, Folks

Welp, that's all I have for you guys today. I'm sorry that I didn't really share any amazing revelations here, but hopefully if you weren't sure the difference between def and let you have a better idea know. That's all for now. Peace out, cub scouts!
0 Comments

Your comment will be posted after it is approved.


Leave a Reply.

    ​Author

    Picture
    The posts on this site are written and maintained by Jim Lynch. About Jim...
    Follow @JimLynchCodes
    Follow @JimLynchCodes

    Want FREE access to
    my daily stock tips 
    ​newsletters??
    Sign up here:

    - Triple Gainers
    - Rippers N' Dippers
    - Growingest Growers
    ​

    Categories

    All
    Actionscript 3
    Angular
    AngularJS
    Automated Testing
    AWS Lambda
    Behavior Driven Development
    Blogging
    Business Building
    C#
    C / C++
    ClojureScript / Clojure
    Coding
    Community Service
    CS Philosophy
    Css / Scss
    Dev Ops
    Firebase
    Fitness
    Flash
    Front End
    Functional Programming
    Git
    Go Lang
    Haskell
    Illustrations
    Java
    Javascript
    Lean
    Life
    Linux
    Logic Pro
    Music
    Node.js
    Planning
    Productivity
    Professionalism
    Python
    React
    Redux / Ngrx
    Refactoring
    Reusable Components
    Rust
    Security
    Serverless
    Shell Scripting
    Swift
    Test Driven Development
    Things
    TypeScript
    Useful Sites
    Useful Tools
    Video
    Website Development
    WebStorm
    Writing

    Archives

    February 2021
    January 2021
    October 2020
    September 2020
    May 2020
    April 2020
    February 2020
    January 2020
    December 2019
    October 2019
    September 2019
    August 2019
    July 2019
    June 2019
    May 2019
    April 2019
    March 2019
    February 2019
    January 2019
    December 2018
    November 2018
    October 2018
    September 2018
    August 2018
    June 2018
    May 2018
    April 2018
    March 2018
    February 2018
    January 2018
    December 2017
    November 2017
    October 2017
    September 2017
    August 2017
    July 2017
    May 2017
    April 2017
    March 2017
    February 2017
    January 2017
    December 2016
    November 2016
    October 2016
    September 2016
    August 2016
    July 2016
    June 2016
    May 2016
    April 2016
    March 2016
    February 2016
    January 2016
    December 2015
    November 2015
    October 2015

    RSS Feed

  • Blog
  • About Jim
JimLynchCodes © 2021