difference-lists

How do you append an element to a list in place in Prolog?

心不动则不痛 提交于 2019-12-04 00:50:27
If I have a list in Prolog such as X = [1, 2, 3, 4], how do I add the element 5 to the end of the list to have X = [1, 2, 3, 4, 5]? The append function needs two lists, ie append(A,B,C) to get A and B concatenated to the list C. I can do this with a temporary list Y = [1, 2, 3, 4] and Z = [5], to then do an append(Y, Z, X), but I don't like having a temporary list. The usual disclaimers apply here - this is not homework and I am just learning Prolog. Variables in Prolog can only be assigned once. As soon as X has the value [1,2,3,4] it can never have another value. A temporary variable and

How to use difference lists in a Prolog interpreter

狂风中的少年 提交于 2019-12-01 13:13:20
When I was writing down this question on an empty list as a difference list I wanted to test what I knew about those structures. However, when I tried something as simple as comparing different notations it seemed that I was wrong and that I did not understand what is actually going on with difference lists. ?- L = [a,b,c|[d,e]]-[d,e], L = [a,b,c]. false % expected true I tested this on SWI-Prolog as well as SICStus. I verified the notation as this is how it is written in Bratko's Prolog Programming for AI, page 210, but apparently unification is not possible. Why is that? Don't these

How to use difference lists in a Prolog interpreter

一个人想着一个人 提交于 2019-12-01 11:04:52
问题 When I was writing down this question on an empty list as a difference list I wanted to test what I knew about those structures. However, when I tried something as simple as comparing different notations it seemed that I was wrong and that I did not understand what is actually going on with difference lists. ?- L = [a,b,c|[d,e]]-[d,e], L = [a,b,c]. false % expected true I tested this on SWI-Prolog as well as SICStus. I verified the notation as this is how it is written in Bratko's Prolog

How is this context free grammar using difference lists in Prolog functioning?

时光毁灭记忆、已成空白 提交于 2019-11-30 20:41:10
I'm reading this tutorial on context free grammars in Prolog, and they mention at the bottom of the page implementing a context free grammar in Prolog using difference lists, with the following code block included: s(X,Z):- np(X,Y), vp(Y,Z). np(X,Z):- det(X,Y), n(Y,Z). vp(X,Z):- v(X,Y), np(Y,Z). vp(X,Z):- v(X,Z). det([the|W],W). det([a|W],W). n([woman|W],W). n([man|W],W). v([shoots|W],W). It mentions: Consider these rules carefully. For example, the s rule says: I know that the pair of lists X and Z represents a sentence if (1) I can consume X and leave behind a Y , and the pair X and Y

Why are difference lists not an instance of foldable?

天涯浪子 提交于 2019-11-30 19:26:31
The dlist package contains the DList data type, which has lots of instances, but not Foldable or Traversable . In my mind, these are two of the most "list-like" type classes. Is there a performance reason that DList is not an instance of these classes? Also, the package does implement foldr and unfoldr , but none of the other folding functions. DList a is a newtype wrapper around [a] -> [a] , which has an a in a contravariant position, so it cannot implement Foldable or Traversable , or even Functor directly. The only way to implement them is to convert to and from regular lists (see the foldr

Why are difference lists not an instance of foldable?

僤鯓⒐⒋嵵緔 提交于 2019-11-30 16:55:19
问题 The dlist package contains the DList data type, which has lots of instances, but not Foldable or Traversable . In my mind, these are two of the most "list-like" type classes. Is there a performance reason that DList is not an instance of these classes? Also, the package does implement foldr and unfoldr , but none of the other folding functions. 回答1: DList a is a newtype wrapper around [a] -> [a] , which has an a in a contravariant position, so it cannot implement Foldable or Traversable , or

Understanding difference lists (Prolog)

空扰寡人 提交于 2019-11-30 05:45:35
问题 I'm having trouble understanding difference list, particularly in this predicate: palindrome(A, A). palindrome([_|A], A). palindrome([C|A], D) :- palindrome(A, B), B=[C|D]. Could anyone help me follow what's happening? 回答1: palindrome(A, A). palindrome([_|A], A). palindrome([C|A], D) :- palindrome(A, B), B=[C|D]. Seeing the arguments to this predicate as a difference list, the first clause says, a list from A to A (i.e., an empty list) is a palindrome. The second clause says, a one-element

How is this context free grammar using difference lists in Prolog functioning?

廉价感情. 提交于 2019-11-30 04:09:10
问题 I'm reading this tutorial on context free grammars in Prolog, and they mention at the bottom of the page implementing a context free grammar in Prolog using difference lists, with the following code block included: s(X,Z):- np(X,Y), vp(Y,Z). np(X,Z):- det(X,Y), n(Y,Z). vp(X,Z):- v(X,Y), np(Y,Z). vp(X,Z):- v(X,Z). det([the|W],W). det([a|W],W). n([woman|W],W). n([man|W],W). v([shoots|W],W). It mentions: Consider these rules carefully. For example, the s rule says: I know that the pair of lists

Explicit Purely-Functional Data-Structure For Difference Lists

人走茶凉 提交于 2019-11-29 11:26:30
In Haskell, difference lists , in the sense of [a] representation of a list with an efficient concatenation operation seem to be implemented in terms of function composition . Functions and (dynamic) function compositions, though, must also be represented somehow in the computer's memory using data structures, which raises the question of how dlists could be implemented in Haskell without using function compositions, but, rather, through some basic purely-functional node-based data structures. How could this be done with the same performance guarantees as those of function composition? (++) 's

Difference lists in Prolog and mutable variables

▼魔方 西西 提交于 2019-11-28 13:57:23
Are difference lists a means to 'get-around' the fact that variables are immutable in prolog? I.e. if I implement append using difference lists: diff_append(OpenList, Hole, L2) :- Hole = L2. And then run: X=[a,b,c|Hole], diff_append(X, Hole, [d,e,f]). The X, in a way, has been used as a mutable variable. For our intents and purposes it has been changed? In other words, the fact that we have been able to modify X (mutable) rather than having to construct a new list, say Z (immutable) is what makes difference lists attractive. So why not just have mutable variables? Update: diff_append2(OpenList