0

I want to do if statement,with a list.We have a list with numbers extreme([1,5,-10,-22,33,-41,16,58,10],LP).How can i do it with if ?Lets say i want the numbers smaller than -10 and bigger than 12.As a result,it will be LP=[-22,-41,33,16,58].Can it use with ,somehow ,a proper way to do that?Additionally,i want to be specific ,i want only for numbers,not for strings the if statement.

false
  • 10,264
  • 13
  • 101
  • 209
  • You can use `include/3`. I don't understand your expected output in your example, for example -10 is not smaller than -10 and is included in the expected result, and 33 is bigger than 12 and is not included... – gusbro Jun 18 '18 at 15:52
  • Don't think of it as an "if". Think of it as describing conditions. For example, `(E < -10 ; E > 12)`. Use a recursive predicate to define the list that holds these conditions. On this site, search for `[prolog] filter list` for many examples. – lurker Jun 18 '18 at 15:58
  • @gusbro you are right i didn't do it right and i wonder how i did this,i edit –  Jun 18 '18 at 16:21
  • @lurker i have searched but nothing as this i didn't find.It has similarities but not that.Example it finds min or max.I don't search that. –  Jun 18 '18 at 16:25
  • There are a large number of similar questions and answers posted. If you study them you can apply to your problem. – lurker Jun 18 '18 at 16:43

1 Answers1

0

Using include/3 something like this may suit your needs:

extreme(L, NL):-
  include(in_range, L, NL).

in_range(X):-
  number(X),
  (X < -10 ; X > 12).

Trying to be more pure as suggested in the comments the procedures could be written this way:

extreme(L, NL):-
  include(in_range, L, NL).

in_range(X):-
  number_or_fd_var_itype(X),
  X in inf..(-9)\/13..sup.

number_or_fd_var_itype(N):-
   (
     fd_var(N) -> true ;
     ( functor(N,_,_),number(N) )
   ).
gusbro
  • 22,357
  • 35
  • 46
  • @false: yes, at first i was going to suggest just using `X #<10 ; X #> 12`, but that yields errors if the list has something like `a`, so I added the `number/1` call which led me to my answer. Now, how would you make `extreme/2`, as defined by OP, pure ? – gusbro Jun 22 '18 at 21:17
  • @gusbro: Replace `number/1` by [number_itype/1](https://stackoverflow.com/a/30600104/772868). That is, produce instantiation errors for insufficiently instantiated queries. – false Jun 23 '18 at 18:04
  • @gusbro: Instead of `X #< 10 ; X #> 12` rather prefer `X #\= 10, X #\= 11, X #\= 12` or even better `X in inf..9\/13..sup` – false Jun 24 '18 at 17:38
  • @false: I like your suggest of using` number_itype/`1 instead of just `number/1`. But why would you use CLP if only numbers are accepted ? (using CLP is roughly one order of magnitude "slower" at least with SWI). – gusbro Jun 25 '18 at 20:28
  • @gusbro: Actually, I first did not think of them when making the first comment. Also, maybe a better _itype would be appropriate: That is, if the argumet is a constrained variable already, it would succeed. – false Jun 25 '18 at 20:31