3.5 utils.pl -- A collection of useful general-purpose predicates.

author
Adrian Prantl <adrian@complang.tuwien.ac.at>
version
0.8.6-rc4
copyright
Copyright (C) 2007-2009 Adrian Prantl
license
See COPYING in the root folder of the SATIrE project

The predicates drop/3, foldl/4, foldl1/3, last/2, replicate/3, split_at/4 and take/3 are inspired by the Haskell Prelude, but are implemented declaratively: They can be used to generate as well as test.

drop(?N, ?List, ?Tail)
Drop N elements from List, yielding Tail.
drop(N, List, Tail) :-
  length(Head, N),
  append(Head, Tail, List).
last(?List, ?Elem)
Elem is the last element of List.
last(List, Elem) :-
  reverse(List, [Elem|_]).
replicate(?A, ?Num, ?As)
Replicate A Num times yielding As.
replicate(A, Num, As) :-
  length(As, Num),
  maplist(=(A),As).
split_at(?N, ?List, ?Head, ?Tail)
Split List at element N yielding Head, Tail
split_at(N, List, Head, Tail) :-
  length(Head, N),
  append(Head, Tail, List).
take(?N, ?List, ?Head)
Head is unified with the first N elements of List
take(N, List, Head) :-
  length(Head, N),
  append(Head, _Tail, List).
foldl1(?List, ?Pred, ?Result)
Fold List left-to-right using Pred, starting with the first element of List.
foldl(?List, ?Pred, ?Start, ?Result)
Fold a list left-to-right using Pred, just as you would do in Haskell.
pred(LHS, RHS, Result)

Thanks to Markus Triska for the definition.

[det]string_to_term(+Text, -Term)
Convert a String to a Term, stripping whitespaces
[det]atom_to_string(+Atom, -String)
Convert an Atom to a String
[det]term_to_string(+Term, -String)
[det]list_from_to(+Start, +End, -List)
Create a list of integers [Start..End]
[det]repeat_string(+S, +N, -Res)
[det]replace_nth(+Xs, +N, +E, +R, -Ys)
replace the nth element of a list with R and return it in E
[nondet]term_mod(+Term, +M, -ModTerm)
Try to apply M on Term recursively