سؤال

I want to write a function that should produce multiple answers for same agruments. Something like:

aux [x,y]
  | x == A && y == B  = [A,B1]
  | x == A && y == B  = [A,B2]

As you can see both conditions are the same so I am only getting one answer [A,B1], but I want [A,B1] and then [A,B2]. So, basicly, I want haskell to work like prolog. Is it possible?

هل كانت مفيدة؟

المحلول

Try something like

import Control.Monad (guard)

data A = A | B | B1 | B2 deriving Eq
aux [x, y] = concat
             [ guard (x == A && y == B) >> [[A, B1]]
             , guard (x == A && y == B) >> [[A, B2]]]

This is just using the list monad and to build up arguments, if the condition is false we just give the empty list, and then concating the results.

نصائح أخرى

I suggest to build internal list of answers and return it as a result.

مرخصة بموجب: CC-BY-SA مع الإسناد
لا تنتمي إلى StackOverflow
scroll top