Skip to content

Commit 8dbb21d

Browse files
committed
Add rotate solution
1 parent c4b6bf4 commit 8dbb21d

File tree

2 files changed

+9
-11
lines changed

2 files changed

+9
-11
lines changed

problems/lists.org

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -264,11 +264,11 @@ Success:
264264
Examples:
265265

266266
#+begin_src teyjus-edit
267-
?- rotate ["a","b","c","d","e","f","g","h"] 3 X.
268-
X = ["d","e","f","g","h","a","b","c"]
267+
?- rotate [1,2,3,4,5,6,7,8] 3 X.
268+
X = [4,5,6,7,8,1,2,3]
269269

270-
?- rotate ["a","b","c","d","e","f","g","h"] -2 X.
271-
X = ["g","h","a","b","c","d","e","f"]
270+
?- rotate [1,2,3,4,5,6,7,8] -2 X.
271+
X = [7,8,1,2,3,4,5,6]
272272
#+end_src
273273

274274
Hint: Use the predefined predicates =pred std.length i:list A, o:int= and

solutions/lists.mod

Lines changed: 5 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -203,25 +203,23 @@ drop-nth N Xs Dropped :-
203203
% of Ls of length N, and Back is the remaining suffix of
204204
% Ls. It is a fatal error if N is a negative number.
205205
pred split i:int, i:list A, o:list A, o:list A.
206-
split N Ls Front Back :- print "split > " N Ls Front Back, fail.
206+
% split N Ls Front Back :- print "split > " N Ls Front Back, fail.
207207
split N _ _ _ :- N < 0, std.fatal-error "split with negative number".
208-
% split 0 Xs [] Xs.
209208
split 0 Xs _ Xs.
210209
split N [X|Xs] [X|Front] Back :- split {calc (N - 1)} Xs Front Back.
211210
212211
% 1.18
213212
pred slice i:int, i:int, i:list A, o:list A.
214-
slice I J Xs Slice :- print "slice > " I J Xs Slice, fail.
213+
% slice I J Xs Slice :- print "slice > " I J Xs Slice, fail.
215214
slice I J _ _ :- J < I, std.fatal-error "slice with second index < first".
216215
slice I J Xs Slice :- split I Xs _ Xs'
217216
& M is (J - I)
218217
& split M Xs' Slice _
219218
.
220219
221220
% 1.19
222-
% pred rotate i:list A, i:int, o:list A.
223-
% rotate Ls N Ls' :- split N Ls Front Back, std.append Front Back Ls'.
224-
221+
pred rotate i:list A, i:int, o:list A.
222+
rotate Ls N Ls' :- split N Ls Front Back, std.append Back Front Ls'.
225223
}
226224
227225
@@ -270,6 +268,6 @@ main :- test (list.last [1, 2, 3, 4] (some 4))
270268
& test (list.drop-nth 3 [1,2,3,4,5,6,7,8] [1,2,4,5,7,8])
271269
& test (list.split 3 [1,2,3,4,5,6,7,8,9,10] [1,2,3] [4,5,6,7,8,9,10])
272270
& test (list.slice 4 8 [1,2,3,4,5,6,7,8,9,10] [5,6,7,8,9])
273-
% & test (list.rotate ["a","b","c","d","e","f","g","h"] 3 ["d","e","f","g","h","a","b","c"])
271+
& test (list.rotate [1,2,3,4,5,6,7,8] 3 [4,5,6,7,8,1,2,3])
274272
& print "ALL TESTS PASSED"
275273
.

0 commit comments

Comments
 (0)