Skip to content

Instantly share code, notes, and snippets.

@tafsiri
Created December 24, 2011 15:11
Show Gist options
  • Save tafsiri/1517532 to your computer and use it in GitHub Desktop.
Save tafsiri/1517532 to your computer and use it in GitHub Desktop.
(ns day-3
(:use clojure.contrib.pprint))
;Q1 use refs to create a vector of accounts in memory. Create debit and credit
; functions to change the balance of an account
; Note: I'm going to use a map instead of a vector so that accounts can have names
(def bank (ref { }))
(defn make-account [bank account-name balance]
(dosync
(alter bank (conj {account-name, balance}))))
;Use a let binding to hold the newly calculated balance before altering the ref
(defn debit-account [bank account-name amount]
(dosync
(let
[new-balance (- (get @bank account-name) amount)]
(alter bank assoc account-name new-balance))))
(defn credit-account [bank account-name amount]
(dosync
(let
[new-balance (+ (get @bank account-name) amount)]
(alter bank assoc account-name new-balance))))
;Create som
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment