Skip to content
Merged
Changes from all commits
Commits
File filter

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
102 changes: 74 additions & 28 deletions xml/chapter3/section1/subsection1.xml
Original file line number Diff line number Diff line change
Expand Up @@ -949,9 +949,8 @@ s("how many calls?"); // returns 1
<EXAMPLE>make_accumulator_example1_solution</EXAMPLE>
<EXAMPLE>make_accumulator_example2_solution</EXAMPLE>
<JAVASCRIPT>

function make_monitored(f) {
let counter = 0;//initilized to 0
let counter = 0; //initialized to 0
function mf(cmd) {
if (cmd === "how many calls") {
return counter;
Expand Down Expand Up @@ -1035,6 +1034,47 @@ const acc = make_account(100, "secret password");
"Incorrect password"
</SCHEMEOUTPUT>
</SNIPPET>


<SOLUTION>
<SNIPPET>
<JAVASCRIPT>
function make_account(balance, p) {
function withdraw(amount) {
if (balance > amount) {
balance = balance - amount;
return balance;
} else {
return "Insufficient funds";
}
}
function deposit(amount) {
balance = balance + amount;
return balance;
}
function dispatch(m, q) {
if (p === q) {
if (m === "withdraw") {
return withdraw;
} else if (m === "deposit") {
return deposit;
} else {
return "Unknown request - make_account";
}
} else {
return q => "Incorrect Password";
}
}
return dispatch;
}

const a = make_account(100, "eva");
(a("withdraw", "eva"))(50); //withdraws 50
(a("withdraw", "ben"))(40); //incorrect password
</JAVASCRIPT>
</SNIPPET>
</SOLUTION>

</EXERCISE>


Expand All @@ -1053,7 +1093,9 @@ const acc = make_account(100, "secret password");
<SNIPPET>
<JAVASCRIPT>
function make_account(balance, p) {
let invalid_attempts = 0;//initializes to 0

let invalid_attempts = 0; //initializes to 0

function withdraw(amount) {
if (balance > amount) {
balance = balance - amount;
Expand All @@ -1062,43 +1104,47 @@ function make_account(balance, p) {
return "Insufficient funds";
}
}

function deposit(amount) {
balance = balance + amount;
return balance;
}

function call_the_cops(q) {
return q => "calling the cops as you have exceeded the max no of failed attempts";

function call_the_cops(q) {
return q => "calling the cops as you have exceeded "
+ "the max no of failed attempts";
}

function dispatch(m, q) {
if (invalid_attempts <= 7) {
if (p === q) {
if (m === "withdraw") {
return withdraw;
} else if (m === "deposit") {
return deposit;
} else {
return "Unknown request - - MAKE-ACCOUNT";
}
if (invalid_attempts &lt;= 7) {
if (p === q) {
if (m === "withdraw") {
return withdraw;
} else if (m === "deposit") {
return deposit;
} else {
return "Unknown request - make_account
}
} else {
invalid_attempts = invalid_attempts + 1;
return q => "Incorrect Password";
return q =&gt; "Incorrect Password";
}
} else {
return call_the_cops;
}
} else {
return call_the_cops;
}
}
return dispatch;

return dispatch;

}

const a = make_account(100, "henz");
(a("withdraw", "hen"))(50);
(a("withdraw", "hen"))(50);
(a("withdraw", "hen"))(50);
(a("withdraw", "hen"))(50);
(a("withdraw", "hen"))(50);
(a("withdraw", "hen"))(50);
const a = make_account(100, "eva");
(a("withdraw", "eva"))(50);
(a("withdraw", "eva"))(50);
(a("withdraw", "eva"))(50);
(a("withdraw", "eva"))(50);
(a("withdraw", "eva"))(50);
(a("withdraw", "eva"))(50);
</JAVASCRIPT>
</SNIPPET>
</SOLUTION>
Expand Down