Search code examples
lispcommon-lisppredicatepartitionmini-language

Function that accepts two lists of predicates and elements list and partitions the elements list accordingly


i want to write a function that accepts two lists of predicates functions and elements list and returns all the members in the original list that hold all the predicates in pre1_list and delete the members in the original list that unhold all the predicates in pre2_list

i am writing this code in a language called mini lisp , it's similar to lisp but more simple ,but that doesn't matter , i just want help on how to do such a thing ? an idea of how to implement such code !

how i am thinking to do it : (just the beginning of my idea)

(defun get_pred_1_not_2 (pre1_list pre2_list list)
  (cond
    ((null list) NIL)
      ; return all the members in the original list that hold all the predicates in pre1_list
    (pre1_list (get_pred_1_not_2 (cdr pre1_list) pre2_list (filter_pre list (car pre1_list))))
      ; delete all the members in the original list that unhold all the predicates in pre2_list
    (pre2_list (get_pred_1_not_2 pre1_list (cdr pre2_list) ;.... ( 

where filter_pre is a function that returns all the element from the list that holds predications giving to it

i hope someone could help ! as this function is really deficult to write and i don't want to just give up thank you


Solution

  • Don't try building the whole solution all at once. Instead proceed by building the building blocks for yourself, function by function, for simple enough tasks so that each function is easy to write. That's the "functional programming" approach.

    Start with the simple filter function for one predicate. Then modify it to have your filter-not.

    Then use them to implement filter-ps and filter-not-ps.

    Alternatively, turn your list of predicates into one predicate by writing all-ps and all-ps-not functions.

    The produced predicate will work with one element at a time, so you can use it with the simple filter to get what you want, with two calls to filter -- first with the result of all-ps applied to your first predicates list; second with the result of all-ps-not applied to your second predicates list.

    edit: if you delete all elements for which all predicates "unhold" i.e. return false, you are left with all the elements for which at least one of the predicates returns true. So you might as well say, for the second part of the task, that you want to keep all elements for which at least one of the predicates hold. Still the same general guidelines apply, just that instead of defining all-ps-not you need to define at-least-one-of-ps, or perhaps name it some-ps.