Currently stuck on trying to implement the towers of hanoi using a collection. I am trying to follow the example in Java using stacks http://www.sanfoundry.com/java-program-implement-solve-tower-of-hanoi-using-stacks/, but I am getting
-module(toh).
-export([begin/0]).
begin() ->
Pegs = 3,
TowerA = createTowerA(N),
TowerB = [],
TowerC = [],
move(Pegs, TowerA, TowerB, TowerC).
%fills Tower A with integers from Pegs to 1.
createTowerA(0) -> [];
createTowerA(N) when N > 0 ->
[N] ++ createTowerA(N - 1).
%displays the towers
display(A, B, C) ->
io:format("~w\t~w\t~w~n", [A, B, C]).
move(Pegs, TowerA, TowerB, TowerC) ->
if Pegs > 0 ->
move(Pegs, TowerA, TowerC, TowerB),
Temp = lists:last(TowerA),
NewTowerC = C ++ Temp,
NewTowerA = lists:sublist(TowerA, length(TowerA) - 1),
display(NewTowerA, B, NewTowerC),
move(Pegs - 1, B, NewTowerA, NewTowerC);
end
When I try running the code, I get this error.
{"init terminating in do_boot",{undef,[{toh,begin,[],[]},{init,begin_i
t,1,[{file,"init.erl"},{line,1057}]},{init,begin_em,1,[{file,"init.erl"},{line,1
037}]}]}}
Crash dump was written to: erl_crash.dump
init terminating in do_boot ()
Can someone see why this is not working? I'm just trying to follow the sanfoundry example.
This code cannot compile, at least the variable C in move/4 is unbound when used. So it seems that you didn't compile this file before trying to execute it.
Although Erlang used a virtual machine, it must be compiled before execution.
There are other problems than the C variable in this code: you call move/4 recursively in the first line of the if, without using any returned value, this cannot have any effect. Also you are using a if statement with a bad syntax. the correct syntax is:
if
GuardSeq1 ->
Body1;
...;
GuardSeqN ->
BodyN % no semicolon at the end of the last body
end
if you intend to use this, beware that you must always have at least one guard that is true, otherwise the code will crash.
My version [edit: remove useless function, better print]:
-module (toh).
-export([start/1]).
start(N) ->
Game = #{1 => lists:seq(1,N), 2 => [], 3 => []},
display(Game,N),
move(N,Game,1,3,N).
move(1,Game,From,To,Size) ->
[H|NewFrom] = maps:get(From,Game),
NewTo = [H|maps:get(To,Game)],
NewGame = maps:update(From,NewFrom,maps:update(To,NewTo,Game)),
display(NewGame,Size),
NewGame;
move(N,Game,From,To,Size) ->
Other = other(From,To),
Game1 = move(N-1,Game,From,Other,Size),
Game2 = move(1,Game1,From,To,Size),
move(N-1,Game2,Other,To,Size).
display(#{1 := A, 2 := B, 3 := C},D) ->
lists:foreach(fun(X) -> print(X,D) end,lists:zip3(complete(A,D),complete(B,D),complete(C,D))),
io:format("~n~s~n~n",[lists:duplicate(6*D+5,$-)]).
complete(L,D) -> lists:duplicate(D-length(L),0) ++ L.
print({A,B,C},D) -> io:format("~s ~s ~s~n",[elem(A,D),elem(B,D),elem(C,D)]).
elem(I,D) -> lists:duplicate(D-I,$ ) ++ lists:duplicate(I,$_) ++ "|" ++ lists:duplicate(I,$_) ++ lists:duplicate(D-I,$ ).
other(I,J) -> 6-I-J.
In the shell:
Eshell V6.1 (abort with ^G)
1> c(toh).
{ok,toh}
2> toh:start(3).
_|_ | |
__|__ | |
___|___ | |
-----------------------
| | |
__|__ | |
___|___ | _|_
-----------------------
| | |
| | |
___|___ __|__ _|_
-----------------------
| | |
| _|_ |
___|___ __|__ |
-----------------------
| | |
| _|_ |
| __|__ ___|___
-----------------------
| | |
| | |
_|_ __|__ ___|___
-----------------------
| | |
| | __|__
_|_ | ___|___
-----------------------
| | _|_
| | __|__
| | ___|___
-----------------------
#{1 => [],2 => [],3 => [1,2,3]}
3>
Is there a direct approach to construct a rotation matrix from the following input?
Say there is a standard perpendicular reference: X[1,0,0], Y[0,1,0], Z[0,0,1] and I want rotation matrix to rotate it to match another perpendicular reference X'[a1,b1,c1], Y'[a2,b2,c2], Z'[a3,b3,c3]. Vectors are unit vectors.
Is it possible that the matrix would be like below?
a1, a2, a3
b1, b2, b3
c1, c2, c3
Given points defined in a XYZ coordinate system, you transform them to a X'Y'Z coordinate system with a 3x3 rotation matrix. In general, the components of the local a, b, and c axes arranged in columns in the world coordinates represent the local->world transformation for that system such that
| x_world | | a1 b1 c1 | | x_local |
| y_world | = | a2 b2 c2 | | y_local |
| z_world | | a3 b3 c3 | | z_local |
and the reverse transformation (with a matrix transpose)
| x_local | | a1 a2 a3 | | x_world |
| y_local | = | b1 b2 b3 | | y_world |
| z_local | | c1 c2 c3 | | z_world |
Now to transform between any two coordinate systems with local->world rotation matrices XYZ and X'Y'Z' (note in your case XYZ is the 3x3 identity matrix) you chain the above for
point_x'y'z' = transpose(X'Y'Z') * (XYZ) * point_xyz
| x' | | a1 b1 c1 | | x | | a1 x + b1 y + c1 z |
| y' | = | a2 b2 c2 | | y | = | a2 x + b2 y + c2 z |
| z' | | a3 b3 c3 | | z | | a3 x + b3 y + c3 z |
Yes, the transformation from identity to any other transform is the other transform. Usually you do not call these things rotation matrices because they represent any arbitrary transformation. It may be that the transformation may not be achievable by rotation alone, as the transformation may be mirrored about one or two planes.
Please note while your using column vector's it is perfectly possible to also use row vectors in which case your results are just transposed, and multiplication order the reverse. Mathematically its the same thing. So check your system
Should I manually apply the production rules to find out the language generated by this grammar? This is tedious, is there any trick/tip to speed up things?
G = {{S, B}, {a, b}, P, S}
P = {S -> aSa | aBa, B -> bB | b}
EDIT: I found Matajon's answer a good one, that is thinking about each language generated by non-terminal symbol and then combine them.
But I'm still stuck when I have to solve some complicated examples like this:
G = {{S, R, T}, {0, 1, 2, 3, 4, 5, 6, 7, 8, 9}, P, S}
P = {S -> A | AS | BR | CT,
R -> AR | BT | C | CS,
T -> AT | B | BS | CR,
A -> 0 | 3 | 6 | 9,
B -> 1 | 4 | 7,
C -> 2 | 5 | 8}
Crazy, isn't it? Taken from past exams (programming languages course).
I don't know any general trick, but usually it helps to think about the language generated from each non-terminal.
In your example language generated from B is obviously L(B) = {b}^+. Then you think about S rules, using the first rule, you can generate sentencial forms {a^n.S.a^n | n >= 1}. If you use second rule on these sentencial forms or on S alone you can generate sentencial forms {a^n.B.a^n | n >= 1}.
Rest is pretty easy, you combine these two things and get L(G) = {a^n.b^+.a^n | n >= 1}
By the way, in the definition of grammar terminals and nonterminals are sets, not tuples. And third component is production rules, not start symbol. So you should write G = {{S, B}, {a, b}, P, S}.
Edit
Actually, there is a way to solve your second example without much thinking just by following something like a cookbook. Because, language generated by your second context-free grammar is in fact regular.
When you substitute rules for A, B and C to first three rules, you get
P' = {S -> 0 | 3 | 6 | 9 | 0S | 3S | 6S | 9S | 1R | 4R | 7R | 2T | 5T | 8T
R -> 0R | 3R | 6R | 9R | 1T | 4T | 7T | 2 | 5 | 8 | 2S | 5S | 8S
T -> 0T | 3T | 6T | 9T | 1 | 4 | 7 | 1S | 4S | 7S | 2R | 5R | 8R}
And P' is regular grammar. Because of that, you can convert it to nondeterministic finite automaton (there is really simple way, look for it) and then convert resulting NFA to the regular expression (this is not so simple but if you follow an algorithm and don't get lost, you should be ok). And it from regular expression it is easy to tell what language it describes.
Also, once you have NFA for this language you can look at it and determine what it does logically (it has something to do with counts of 1,4,7 and 2,5,8 in the word and mod 3 of their difference. Think it through, it is your homework, afterall :-) )
Of course, if you don't context-free grammar generating regular language you can't use this trick. There is no general way to tell what language the grammar generates (language equality problem for CFG's is undecideable), you have to think about every single example and look for similarities and patterns in it's logical structure.
I think you'll just need to apply the production rules.