Skip to content

Instantly share code, notes, and snippets.

@victorwyee
Last active November 10, 2020 06:20
Show Gist options
  • Select an option

  • Save victorwyee/6a5cbea4777d1bfec084ae5cea02eec5 to your computer and use it in GitHub Desktop.

Select an option

Save victorwyee/6a5cbea4777d1bfec084ae5cea02eec5 to your computer and use it in GitHub Desktop.
class BankAccount
attr_reader :balance
def initialize
@balance = 0
end
def deposit amount
@balance += amount
end
def withdraw amount
@balance -= amount
end
end
> account = BankAccount.new
#<BankAccount...>
> account.balance
0
> account.deposit 100
> account.withdraw 70
> account.balance
70
class CashRegister
attr_reader :drawer
# a more thorough implementation could
# include the amount of each denomination
# assume that the drawer is sorted largest to smallest
def initialize
@drawer = [2000, 1000,
500, 100,
25, 10,
5, 1]
end
def make_change owed, tendered
difference = tendered - owed
change = []
# moves down the drawer
i = 0
denomination = @drawer[i]
while difference > 0 do
if difference < denomination
i += 1
denomination = @drawer[i]
next
end
change << denomination
difference -= denomination
end
change
end
end
(+ 3 5)
8
(* 1 2 3)
6
(+ (* 3 5)
(- 10 6))
19
(define (square n)
(* n n))
(square 5)
25
(cond
((test) stuff if test is true)
((different test) different stuff)
(else more stuff))
(define (abs x)
(cond
((> x 0)
x)
((= x 0)
0)
(else
(- x))))
; Semicolon in front to distinguish from function call
'(1 2 3)
; Access first element in list
(car '(1 2 3))
1
; Access rest of list
(cdr '(1 2 3))
'(2 3)
; Make a list, here cons-ing 1 onto list containing 2 3
(cons '1 '(2 3))
'(1 2 3)
; Factorial
(define (fact n)
(cond
((<= n 1)
1)
(else
(* n (fact (- n 1))))))
; Fibonacci
(define (fib n)
(cond ((<= n 0)
0)
((= n 1))))
; Make change in Racket
(define (make-change x denoms)
(cond
((= x 0) ; don't owe any money
'()) ; change for zero: empty list
((empty? denoms) ; do I money in my cash drawer?
false) ; if not, return false
((< x (car denoms)) ; car gets the first elem in the denoms list (the highest amount)
(make-change x (cdr denoms)))
(else ; case where the first bill is actually used
(cons (car denoms) ; get the rest of the list
(make-change (- x (car denoms))
denoms)))))
state(washington).
border(washington, oregon).
border(washington, idaho).
border(oregon, california).
adjacent(X, Y) :- border(X, Y).
?- adjacent(washington, oregon).
yes
?- adjacent(oregon, washington).
no
father(homer, bart).
father(homer, lisa).
mother(marge, bart).
mother(marge, lisa).
% what value of X makes this statement true?
?- mother(X, bart).
X = marge
% the opposite: who's Marge the mother of?
?- mother(marge, Y).
Y = bart ?; % ? means that another answer is correct
Y = lisa
sibling(X, Y) :- % X and Y are siblings if
mother(Z, X), % Z is the mother of X
mother(Z, Y), % Z is the mother of Y
X \== Y % X and Y are not the same person
sibling(X, Y) :-
father(Z, X),
father(Z, Y),
X \== Y
?- sibling(X, Y).
X = bart
Y = lisa
% lists
[]
[1, 2, 3]
[apples, [1, 3], mangos] % nested lists!
% F = first, R = rest
% just like car and cdr in Racket!
[F | R]
[1, 2, 3]
F = 1
R = [2, 3]
% Member (List Rule)
% X is a member of this list if it's first thing in the list
member(X, [X | _]).
% X isn't the first thing in this list, but it's a member of the rest of the
% list. This implies that X is a member of this list.
member(X, [_ | R) :- member(X, R).
change(amount, coins, change)
% amount: int - the amount we owe
% coins: list - the coins we have
% change: list - how we make change with that list of coins
% this will be true only if that last argument is how you make
% `amount` using the `coins` in the middle
% how do we make change for the amount 0?
% change for the amount zero is the empty list.
% we don't care what's in the cash drawer if we're making change for 0.
change(0, _, []).
% we make change for the amount A by
% using the first thing from my cash drawer
% and that is the first thing in the way I make change.
change(A, [F | R], [F | X]) :-
A >= F, % I can only do this if the A >= F
B is A - F % B is what's left over after using that bill
change(B, [F | R], X). % and I can make change for B with X (which matches the top X).
% the case where we can't use the first coin or bill
change(A, [_ | R], X) :- % don't care about the first coin since we can't use it
A > 0, % only care if we owe more than 0, otherwise use first fact
change(A, R, X). % we make change for A with what's left in the cash drawer
% and the way we do this is X.
@2 # assign 2 into the A register
D=A # load the contents of the A register into the D register
@3 # assign 3 into the A register
D=D+A # add what's in D (2) and what's in A (3) and assign it to myself
@0 # assign 0 into the A register
M=D # then I take whatever's there and assign it to memory cell 0.
# M refers to memory, not the value in A.
@0 # store my result in memory cell 0
M=0 # zero out what's there
@5 # assign 5 to A register
D=A # put that 5 into D
@1 # refer to memory cell 1
M=D # now memory cell 1 has that 5
(LOOP) # parenthesis notation is a label
@1 # take contents of memory cell 0, which is 5 in this case
D=M # put it in D
@0 # take content of memory 0, which is my accumulator (currently has 0)
M=M+D # add what's in M and D and put it back in M
@1
MD=M-1 # get what's in memory cell 1, decrement it, and store in both memory cell 1 and D
@END # load END into A register, this is where to go when the program's over
D;JLE # the counter: if D is less than or equal to 0, jump to the END
@LOOP
0;JMP # in all other cases, jump to back to the LOOP label. Unconditional jump.
# Making change in Assembly (here, R represents Memory)
@67 # starting amount
D=A
@R0
M=D
# Load denominations
@25 # quarters
D=A
@R=1
M=D
@10 # dimes
D=A
@R2
M=D
@5 # nickels
D=A
@R3
M=D
@1 # pennies
D=A
@R4
M=D
(QUARTERS)
@R0
D=M
@R1
D=D-M
@DIMES
D;JLT
M=D
@R5
M=M+1
@QUARTERS
0;JMP
(DIMES)
@R0
D=M
@R2
@NICKELS
D;JLT
@R0
M=D
@R6
M=M+1
@DIMES
0;JMP
(NICKELS)
@R0
D=M
@R3
@PENNIES
D;JLT
@R0
M=D
@R7
M=M+1
@NICKELS
0;JMP
(PENNIES)
@R0
D=M
@R4
D=D-M
@END
D;JLT
@R0
M=D
@R8
M=M+1
@PENNIES
0;JMP
(END)
@END
0;JMP
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment