(defn removeFirst
[x xs]
(if (= x (first xs)) (rest xs) (cons (first xs) (removeFirst x (rest
xs)))))
(defn perms
( [] [[]])
([xs]
(for [x xs p (perms (removeFirst x xs))] (cons x p)) )
)
Good day,
I have been studying a functional approach to the design and analysis of
algorithms. I am trying to generate permutations by performing a list
comprehension to implement the following piece of haskell
perms [] [[]]
perms [x:p | x<-xs, p<-perms(removeFirst x xs)]
where
removeFirst x [] = []
removeFirst x (y:ys) | x == y ys
| otherwise = y : removeFirst x ys
This is my attempt in clojure:
(defn removeFirst
[x xs]
(if (= x (first xs)) (rest xs) (cons (first xs) (removeFirst x (rest
xs)))))
(defn perms
( [] [[]])
([xs]
(for [x xs p (*perms* (removeFirst x xs))] (cons x p)) ) ;; I have tried
using *recur* instead of *perms* here , but clojure complains that
;;I cannot recur there - I guess not in tail position
)
(perms [1 2 3])
===============
This is the output I receive in the repl (*IntelliJ IDEA*)
#'user/removeFirst
user=>#'user/perms
user=> user=>()
user=> user=> user=>
=================
This is how it looked on *lightTable*
(defn removeFirst
[x xs] *1 (1)*
(if (= x (first xs)) (rest xs) (cons (first xs) (removeFirst x (rest
xs))))) *1 (1) (1) (1) 1 (1)
*(defn perms
( [] [])
( [xs]
(for [x xs p (perms (removeFirst x xs))] (cons x p))) *() () () () ()*
)
(perms [1 2 3]) *()*
Any idea what's wrong?
Thanks
Keith
--
--
You received this message because you are subscribed to the Google
Groups "Clojure" group.
To post to this group, send email to [email protected]
Note that posts from new members are moderated - please be patient with your
first post.
To unsubscribe from this group, send email to
[email protected]
For more options, visit this group at
http://groups.google.com/group/clojure?hl=en
---
You received this message because you are subscribed to the Google Groups
"Clojure" group.
To unsubscribe from this group and stop receiving emails from it, send an email
to [email protected].
For more options, visit https://groups.google.com/groups/opt_out.