difference-lists

To sort out atoms first and then sublists from a list in LISP

本秂侑毒 提交于 2019-11-28 02:05:19
I have this homework in LISP where I need to sort out atoms and then sublists from a list. I'm sure this is supposed to be easy task but as I'm not much of a programmer then this is really taking quite a while for me to understand. I have this list of numbers: (5 -1 (2 6 1) (8 7 -3) (0 (9 4)) -6) And if I understand correctly my task then I should get something like this: (5 -1 -6 (2 6 1) (8 7 -3) (0 (9 4))) So far all I found out is how to count atoms and/or sublists but I don't need that. (DEFUN ATOMNUMBER (L) (COND ((NULL L) 0) ((ATOM (CAR L)) (+ 1 (ATOMNUMBER (CDR L)))) (T (ATOMNUMBER (CDR

Difference lists in Prolog and mutable variables

怎甘沉沦 提交于 2019-11-27 19:30:43
问题 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

Explicit Purely-Functional Data-Structure For Difference Lists

冷暖自知 提交于 2019-11-27 07:01:29
问题 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.

Understanding Difference Lists

匆匆过客 提交于 2019-11-26 23:02:50
问题 I'm trying to understand difference lists in Prolog, but I'm struggling to actually implement one properly, everytime I try to do it, I get a list of lists, but that's not what I want. I'm trying to implement an append predicate, but having little luck so far. Few attempts, all of which don't work. app(X, Y, Z) :- Z = [X|Y]. ?- app([a,b,c], [z], Z). Z = [[a,b,c],z]. OR app(X, Y, Z) :- Z = [X|Hole], Hole = Y. Same results as the first one, (they do seem to be basically the same). I have an

To sort out atoms first and then sublists from a list in LISP

我与影子孤独终老i 提交于 2019-11-26 22:07:47
问题 I have this homework in LISP where I need to sort out atoms and then sublists from a list. I'm sure this is supposed to be easy task but as I'm not much of a programmer then this is really taking quite a while for me to understand. I have this list of numbers: (5 -1 (2 6 1) (8 7 -3) (0 (9 4)) -6) And if I understand correctly my task then I should get something like this: (5 -1 -6 (2 6 1) (8 7 -3) (0 (9 4))) So far all I found out is how to count atoms and/or sublists but I don't need that.

Flatten a list in Prolog

旧时模样 提交于 2019-11-26 20:57:57
I've only been working with Prolog for a couple days. I understand some things but this is really confusing me. I'm suppose to write a function that takes a list and flattens it. ?- flatten([a,[b,c],[[d],[],[e]]],Xs). Xs = [a,b,c,d,e]. % expected result The function takes out the inner structures of the list. This is what I have so far: flatten2([],[]). flatten2([Atom|ListTail],[Atom|RetList]) :- atom(Atom), flatten2(ListTail,RetList). flatten2([List|ListTail],RetList) :- flatten2(List,RetList). Now, this works when I call: ?- flatten2([a,[b,c],[[d],[],[e]]], R). R = [a,b,c,d,e]. % works as

is it possible to do quicksort of a list with only one passing?

和自甴很熟 提交于 2019-11-26 09:57:33
问题 I am learning haskell and the function definition I see is: quickSort (x : xs) = (quickSort less) ++ (x : equal) ++ (quickSort more) where less = filter (< x) xs equal = filter (== x) xs more = filter (> x) xs Is it possible to write it with only one traversal of the list, instead of 3? 回答1: Although late, here's a version that's supposed to not leak space as much (and seems to run about twice faster than the other 3-way version here): qsort3 xs = go xs [] where go (x:xs) zs = part x xs zs []

Flatten a list in Prolog

三世轮回 提交于 2019-11-26 07:47:09
问题 I\'ve only been working with Prolog for a couple days. I understand some things but this is really confusing me. I\'m suppose to write a function that takes a list and flattens it. ?- flatten([a,[b,c],[[d],[],[e]]],Xs). Xs = [a,b,c,d,e]. % expected result The function takes out the inner structures of the list. This is what I have so far: flatten2([],[]). flatten2([Atom|ListTail],[Atom|RetList]) :- atom(Atom), flatten2(ListTail,RetList). flatten2([List|ListTail],RetList) :- flatten2(List

Why are difference lists more efficient than regular concatenation?

淺唱寂寞╮ 提交于 2019-11-26 04:21:36
问题 I am currently working my way through the Learn you a Haskell book online, and have come to a chapter where the author is explaining that some list concatenations can be inefficient: For example ((((a ++ b) ++ c) ++ d) ++ e) ++ f is supposedly inefficient. The solution the author comes up with is to use \'difference lists\' defined as newtype DiffList a = DiffList {getDiffList :: [a] -> [a] } instance Monoid (DiffList a) where mempty = DiffList (\\xs -> [] ++ xs) (DiffList f) `mappend`