Skip to content

Commit a6867cc

Browse files
committed
Add 1.21
1 parent 1dbbc16 commit a6867cc

File tree

2 files changed

+29
-11
lines changed

2 files changed

+29
-11
lines changed

problems/lists.org

Lines changed: 13 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -279,7 +279,7 @@ Hint: Use the predefined predicates =pred std.length i:list A, o:int= and
279279
=pred append i:list A, i:list A, o:list A=, as well as the result of problem
280280
1.17.
281281

282-
* 1.20 (*) Remove the N'th element from a list.
282+
* 1.20 (*) Select and remove the N'th element from a list.
283283

284284
Example:
285285

@@ -290,3 +290,15 @@ Success:
290290
X = 2
291291
R = [1,3,4]
292292
#+end_src
293+
294+
295+
* 1.21 (*) Insert an element at a given position into a list.
296+
Example:
297+
298+
#+begin_src teyjus-edit
299+
> insert-at 10 2 [1,2,3,4] L.
300+
301+
Success:
302+
303+
L = [1,2,10,3,4]
304+
#+end_src

solutions/lists.mod

Lines changed: 16 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -230,20 +230,25 @@ rotate Ls N Ls' :- if (N > 0)
230230
pred select-nth i:int, i:list A, o:A, o:list A.
231231
select-nth N Ls X Rest :- len Prefix {calc (N - 1)}
232232
& std.append Prefix [X|Suffix] Ls
233-
& std.append Prefix Suffix Rest.
233+
& std.append Prefix Suffix Rest
234+
.
235+
236+
% 1.21
237+
pred insert-at i:A, i:int, i:list A, o:list A.
238+
insert-at X N Ls Ls' :- len Prefix {calc N}
239+
& std.append Prefix Suffix Ls
240+
& std.append Prefix [X|Suffix] Ls'
241+
.
234242
}
235243

236244

237245
pred test i:prop.
238-
test P :- ( P
239-
& !
240-
)
241-
; ( Msg is "Test '" ^ {term_to_string P} ^ "' FAILED!"
242-
& print Msg
243-
& !
244-
& false
245-
)
246-
.
246+
test P :- P & !.
247+
test P :- Msg is "Test '" ^ {term_to_string P} ^ "' FAILED!"
248+
& print Msg
249+
& !
250+
& fail
251+
.
247252

248253
shorten list.{ ls, el, one, many }.
249254

@@ -282,5 +287,6 @@ main :- test (list.last [1, 2, 3, 4] (some 4))
282287
& test (list.rotate [1,2,3,4,5,6,7,8] 3 [4,5,6,7,8,1,2,3])
283288
& test (list.rotate [1,2,3,4,5,6,7,8] -2 [7,8,1,2,3,4,5,6])
284289
& test (list.select-nth 2 [1,2,3,4] 2 [1,3,4])
290+
& test (list.insert-at 10 2 [1,2,3,4] [1,2,10,3,4])
285291
& print "ALL TESTS PASSED"
286292
.

0 commit comments

Comments
 (0)