Coder Social home page Coder Social logo

Comments (1)

trptcolin avatar trptcolin commented on June 18, 2024 1

For lists, it's definitely a stack - "last in first out" refers not to the ordering as you look at the data structure, but to the order in which elements are inserted and removed. So the last element to be inserted would be the first one to come back out.

Here's a lein repl session with similar examples to that koan:

user=> (cons :a '(:b :c :d :e)) ;; adding :a to the *front* of the list
(:a :b :c :d :e) 
user=> (conj '(:a :b :c :d) :e) ;; adding :e to the front using conj
(:e :a :b :c :d)
user=> (peek '(:a :b :c :d :e)) ;; "peeking" at the last element inserted, :a
:a
user=> (pop '(:a :b :c :d :e)) ;; removing the last element added, :a
(:b :c :d :e)

Basically, it's efficient to add elements to the front of a list, and it's efficient to remove elements from the front of a list. And adding & removing from the same end is what a stack does. With a queue ("first in first out"), we'd expect to add on one end, and remove from the other end.

"Insert" (or "add") and "remove" take on a little different meaning due to the immutability of the data structures, but hopefully this sheds some more light on why the list gets used like a stack!

from clojure-koans.

Related Issues (20)

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.