-2

I am doing a Divides? function that takes as inputs a divisor x and a number n. The function should return true if x divides n and false otherwise.

at the moment this function works fine but I need to twist it a little bit more by adding an anonymous function with filter method, and also for the arguments have to be in the correct order (x n) rather than (n x).

Any ideas would be appreciated:

Divides?:

(defn Divides? [x n]
 (zero? (mod n x)))
(println "Divides"( Divides? 2 10))
 //output : true
Yamen
  • 23
  • 5
  • It's not quite clear, in your current function arguments are already `x n` rather than `n x`. Also, how exactly are you going to use it with filter? – bereal Jan 11 '20 at 15:17
  • There's nothing magic about the names `x` and `n`.Any distinct names will do. To get them in the right order, just swap them in the first line **or** the second. The filtering is a separate issue, which you have not made clear. – Thumbnail Jan 11 '20 at 15:27
  • I am not allowed to change the order of `x` and `n`, so I have to figure out how to pass the arguments to `Divides` function in the correct order. hence I'm required to use the filter method but don't know how ! – Yamen Jan 11 '20 at 15:39
  • if you have the arguments in the right order, you might be able to use partial, but in the end, defining anon fn is just so easy: `#(Divides % 42)` – cfrick Jan 11 '20 at 16:06

1 Answers1

0

It sounds like you're simply looking to flip the arguments of a function, in which case, this answer may be of help. Once you are able to flip the arguments which a function accepts, you shouldn't need to reimplement anything, but instead do something like:

(defn Divides? [x n]
 (zero? (mod n x)))

(defn flip [f]
  (fn [& xs]
    (apply f (reverse xs))))

((flip Divides?) 8 4)
OliverRadini
  • 6,238
  • 1
  • 21
  • 46