0
votes

Is there a way to separate a list in Prolog like this: [1,2,3,4|5]? I can write a predicate which separates a head from a tail: predicate([Head|Tail]). And I would like to know if I can write something like this: predicate([Init|Last]). Init and Last are used in Haskell to separate the last element from its list. Thank you in advance!

2
The keyword you need to search for is Difference List.Guy Coder

2 Answers

0
votes

You can use last/2 to yield the last element of a list.

?- A = [1,2,3,4,5], last(A, B). 
A = [1, 2, 3, 4, 5],
B = 5.
0
votes

[1,2,3,4|5] is not a proper list, the tail should be a list itself (recursive definition). But you could use append/3 from library(lists) to perform a call that yields the front elements and the last one, like in ?- L=[1,2,3,4,5],append(Xs,[Last],L)..