Use the accumulators to build up the lists in reverse order. Don't use add_number or you'll get a quadratic time algorithm, while you can solve this problem in linear time.
Use the accumulators to build up the lists in reverse order. Don't use add_number or you'll get a quadratic time algorithm, while you can solve this problem in linear time. Foo(, NumsR, Nums, Letters, Letters) :- reverse(NumsR, Nums).
Foo(X|Xs, NumsR, Nums, LettersR, Letters) :- % the following is the Prolog syntax for if-then-else; % you could also do this with two recursive clauses, % but this option is faster because of first-argument indexing (number(X) -> X1 is X+1, foo(Xs, X1|NumsR, Nums, LettersR, Letters) ; foo(Xs, NumsR, Nums, X|LettersR, Letters) ).
Foo(, Nums, Nums, Letters, Letters). Foo(X|Xs, Nums_1, Nums, Letters_1, Letters) :- number(X), X1 is X+1, add_number(Nums_1, X1, Nums_2), foo(Xs, Nums_2, Nums,Letters_1, Letters). Foo(X|Xs, Nums_1, Nums, Letters_1, Letters) :- letter(X), add_letter(Letters_1, X, Letters_2), foo(Xs, Nums_1, Nums, Letters_2, Letters).
Add_number(Nums_1,X,Nums_2) :- append(Numbs_1,X,nums_2). Add_letter(Letters_1,X,Letters_2) :- append(Letters_1,X,Letters_2).
I'd do it something like this: foo( List , Numbers , Letters ) :- worker( List , , Numbers , , Letters ). Worker( , Numbers , Numbers , Letters , Letters ). Worker( X|Xs , NumberAccumulator , Numbers , LetterAccumulator , Letters ) :- digit(X), X1 is X+1 , append( NumberAccumulator , X1 , NumberAccumulator1 ) , worker( Xs , NumberAccumulator1 , Numbers , LetterAccumulator , Letters ).
Worker( X|Xs , NumberAccumulator , Numbers , LetterAccumulator , Letters ) :- letter(X) , worker( Xs , NumberAccumulator , Numbers , X|LetterAccumulator , Letters ). Worker( X|Xs , NumberAccumulator , Numbers , LetterAccumulator , Letters ) :- not letter(X) , not digit(X) , worker( Xs , NumberAccumulator , Numbers , LetterAccumulator , Letters ). Letter( a ).
Letter( be ). Letter( c ). ... letter( z ).
Letter('A'). Letter('B'). Letter('C').... letter('Z').
Digit('0'). Digit('1'). Digit('2').... digit('9').
Since this is a learning exercise, I'd not defer the reversal of the list: I'd do the obvious and build the list in reverse sequence, despite the performance hit. I believe the point of the exercise is that you need to learn to build lists both ways.
I cant really gove you an answer,but what I can give you is a way to a solution, that is you have to find the anglde that you relate to or peaks your interest. A good paper is one that people get drawn into because it reaches them ln some way.As for me WW11 to me, I think of the holocaust and the effect it had on the survivors, their families and those who stood by and did nothing until it was too late.