Created
December 24, 2011 15:11
-
-
Save tafsiri/1517532 to your computer and use it in GitHub Desktop.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
(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