This page is not created by, affiliated with, or supported by Slack Technologies, Inc.
2020-11-05
Channels
- # announcements (1)
- # babashka (6)
- # beginners (31)
- # calva (23)
- # cider (38)
- # clj-kondo (98)
- # cljs-dev (3)
- # clojure (56)
- # clojure-australia (2)
- # clojure-europe (14)
- # clojure-italy (1)
- # clojure-nl (2)
- # clojure-spec (17)
- # clojure-uk (27)
- # clojurescript (45)
- # code-reviews (1)
- # core-async (26)
- # core-logic (10)
- # cursive (11)
- # data-science (9)
- # datomic (7)
- # deps-new (4)
- # devcards (1)
- # emacs (4)
- # etaoin (2)
- # fulcro (14)
- # jobs (1)
- # kaocha (1)
- # leiningen (50)
- # malli (3)
- # parinfer (1)
- # pathom (7)
- # reitit (4)
- # remote-jobs (1)
- # reveal (45)
- # shadow-cljs (59)
- # sql (97)
- # tools-deps (1)
- # uncomplicate (4)
- # vim (18)
hi. i have ported one prolog example to core.logic:
% P07 (**) Flatten a nested list structure.
my_flatten(X,[X]) :- \+ is_list(X).
my_flatten([],[]).
my_flatten([X|Xs],Zs) :- my_flatten(X,Y), my_flatten(Xs,Ys), append(Y,Ys,Zs).
(defne flatteno [l1 l2]
([x [x]] (pred x (comp not seq?)))
([[] []])
([[x . xs] zs]
(fresh [y ys]
(flatteno x y)
(flatteno xs ys)
(appendo y ys zs))))
can someone tell me why the clojure version produces multiple results:
([[1 [2]]] (1 [2]) (1 2))
and the prolog version only one:
[1 2]
?yes, my assumption was right:
(defna flatteno [l1 l2]
([[x . xs] zs]
(fresh [y ys]
(flatteno x y)
(flatteno xs ys)
(appendo y ys zs)))
([x [x]] (pred x (comp not seq?)))
([[] []]))
both defna
and defnu
do the job and clauses have to be rearranged.
Someone who knows prolog can tell me why this kind of cut is necessary in clojure and not in prolog?the \+ negation in prolog also does a cut: http://cs.union.edu/~striegnk/learn-prolog-now/html/node90.html#sec.l10.negation.as.failure
I would strongly recommend against using pred, and seq? is likely the wrong predicate there
the re-arrangement of the clauses and using defna or defna are both consequences of seq? being the wrong predicate
Great and interesting hints. Thanks @UCPGSBNQ4 and @U0NCTKEV8. Will look into that…
(defna flatteno [l1 l2]
([[] []])
([[x . xs] zs]
(fresh [y ys]
(flatteno x y)
(flatteno xs ys)
(appendo y ys zs)))
([x [x]]))
i think this is the best solution. it got rid of pred
as @U0NCTKEV8 suggested and uses defna
. the arrangement of the clauses still matters, but it makes sense.