How can i simplify this equation from this
(x1^y1)&(x2^y2)=(x1^y1)
to this
y2=( (x1^y1) | x1) ^ x1
where ^ is xor, | is or and & is and operations respectively.
Thanks.
When
x1 = y1 = false
x2 = y2 = true
the first equation holds (false = false), but not the second (true = false).
You cannot simplify the first to the second equation.
Related
I am trying to write this function
variation_models' nlocations =
let location_list = [1..nlocations-4]
in [[x1, x2, x3, x4, x5] |
x1 <- location_list,
x2 <- location_list,
x3 <- location_list,
x4 <- location_list,
x5 <- location_list,
both_conditions (adds_up_to nlocations) num_orderedq [x1, x2, x3, x4, x5]]
but have any number of x's. I want variation_models 5 15 to have a valid answer of [1,1,1,1,11].
My current attempt is
variation_models ncars nlocations =
filter (both_conditions (adds_up_to nlocations) num_orderedq) $
replicateM ncars [1..nlocations-ncars+1]
but replicateM seems to make the function take up more and more memory.
Any Ideas on how to write a replacement for replicateM so that it does not consume as much memory?
The rest of the definitions used:
import Control.Monad
orderedq f [] = True
orderedq f (x:[]) = True
orderedq f (x:y:zs) = f x y && orderedq f (y:zs)
num_orderedq = orderedq (<=)
adds_up_to n xs = n == sum xs
both_conditions f g xs = f xs && g xs
variation_models ncars nlocations =
filter (both_conditions (adds_up_to nlocations) num_orderedq) $
replicateM ncars [1..nlocations-ncars+1]
variation_models' nlocations =
let location_list = [1..nlocations-4]
in [[x1, x2, x3, x4, x5] |
x1 <- location_list,
x2 <- location_list,
x3 <- location_list,
x4 <- location_list,
x5 <- location_list,
both_conditions (adds_up_to nlocations) num_orderedq [x1, x2, x3, x4, x5]]
It looks like you are using replicateM to generate integer partitions.
integer_partitions :: Int -> Int -> [[Int]]
integer_partitions 0 _ = []
integer_partitions 1 n = [[n]]
integer_partitions k n =
do x <- [1..n - k + 1]
map (x:) (integer_partitions (k - 1) (n - x))
It seems to have better memory characteristics then the more general replicateM
I'm trying to write a recursive function in SML that receives two natural numbers n1,n2 and returns the result of n1 div n2
The datatype natural is defined as follows:
datatype natural = zero | Succ of natural
I want to write it in terms of the new datatype , or in other words, not by converting them to their regular form and converting back the result.
Any ideas how division is done in this definition?
You could start by defining subtraction:
exception Negative
fun sub (a, zero) = a
| sub (zero, b) = raise Negative
| sub (Succ a, Succ b) = sub (a, b)
From here, it should be pretty easy to simply count how many times you can subtract n2 from n1 without going negative. In particular, this equation should help:
n1 div n2 = 1 + (n1 - n2) div n2
I'll leave the rest to you.
Similar to Sam Westrick's definition, "number of times you can subtract n2 from n1 without going negative", you could also do integer division with addition and greater-than using the definition, "number of times you can add n2 to itself before it is greater than n1."
datatype nat = Z | S of nat
fun gt (S x, S y) = gt (x, y)
| gt (S _, Z) = true
| gt (Z, _) = false
fun add (x, Z) = x
| add (x, S y) = add (S x, y)
fun divide (_, Z) = raise Domain
| divide (x, y) = (* ... *)
Addition might seem like a conceptually simpler thing than subtraction. But greater-than is a more expensive operator than determining when a number is negative, since the case is incurred by induction, so Sam's suggestion would be more efficient.
You might test your solution with the following tests:
fun int2nat 0 = Z
| int2nat n = S (int2nat (n-1))
fun nat2int Z = 0
| nat2int (S n) = 1 + nat2int n
fun range (x, y) f = List.tabulate (y - x + 1, fn i => f (i + x))
fun divide_test () =
let fun showFailure (x, y, expected, actual) =
Int.toString x ^ " div " ^ Int.toString y ^ " = " ^
Int.toString expected ^ ", but divide returns " ^
Int.toString actual
in List.mapPartial (Option.map showFailure) (
List.concat (
range (0, 100) (fn x =>
range (1, 100) (fn y =>
let val expected = x div y
val actual = nat2int (divide (int2nat x, int2nat y))
in if expected <> actual
then SOME (x, y, expected, actual)
else NONE
end))))
end
I am trying to use Haskell to read in a list and perform an alternating series, so starting with the first element adding every other and subtracting every other from the second element. For instance [1, 2, 3, 4] would be 1-2+3-4=-2. I thought I had figured out how to do this for lists of a specific length (I wrote it to accommodate empty lists and lists up to 4 elements long), but it doesn't do what I thought it would. It just returns the first element from the list. Here is what I have:
altSeries :: Num a => [a] -> a
altSeries [] = 0
altSeries (x:xs) = if length xs == 1 then x
else if length xs == 2 then x
else if length xs == 3 then (x - xs!!1 + xs!!2)
else (x - xs!!1 + xs!!2 - xs!!3)
Also, what if I wanted to be able to use any list size?
What you want is a list that ultimately looks something like this [1,-2,3,-4] which you could sum.
You can make a list of alternating sign [1,-1]. this can be made infinite using the cycle function. let alternatingSigns = cycle [1,-1]
To transform a list of [1,2,3,4] you can zip the infinite alternating list with your list like this zipWith (*) alternatingSigns input
The whole function would look something like this:
altSeries :: Num a => [a] -> a
altSeries input = let alternatingSigns = cycle [1,-1]
in sum $ zipWith (*) alternatingSigns input
I think it is clear that this solution does not scale: even if you would write up function up to lists with length thousand, it would break from the moment that someone enters a list with thousand and one elements.
List processing usually is done recursively: we process the head (or the first k heads) of a list, and perform recursion on the tail of the list. Furthermore we have a number of basecases (can be one) to terminate recursion.
You already provided a single base case: the empty list:
altSeries [] = 0
In case we enter an empty list, it is clear that we should return 0. Now we might ask what to do in case we obtain a list of length 1: in that case the list has shape [x]. So in that case we should return x, since we should add x, and can not subtract the second number. So we add:
altSeries [x] = x
Now the question is what to do with lists with length two or more. In that case the list has pattern (x1:x2:t) with x1 the first element, x2 the second element, and t the remaining elements. Based on your description we should caculate x1-x2 and add altSeries t:
altSeries (x1:x2:t) = x1 - x2 + altSeries t
This works since altSeries will then recursively extract x3 and x4, so then it will recurse to:
altSeries [x1,x2,x3,x4,x5] = x1 - x2 + altSeries [x3,x4,x5]
= x1 - x2 + x3 - x4 + altSeries [x5]
= x1 - x2 + x3 - x4 + x5
So the full implementation is:
altSeries :: Num n => [n] -> n
altSeries [] = 0
altSeries [x] = x
altSeries (x1:x2:t) = x1 - x2 + altSeries t
I am trying to further develop some Haskell code that was developed for simplifying regular expressions and I've run into a small problem. When I run the following command:
*Language.HaLex.RegExp> simplifyRegExp(Star (Or a Epsilon))
I get the output 'a'* and if I replace the a's with b's I get 'b'* like I should. The problem arises when I use Then. The following command:
*Language.HaLex.RegExp> simplifyRegExp(Then (Star a) (Star a))
works fine and produces 'a'* as expected but replacing a's with b's produced the following output:
*Language.HaLex.RegExp> simplifyRegExp(Then (Star b) (Star b))
'b'*'b'*
Although it is supposed to produce just 'b'*. Now if I change the variable name to b in the the line
simplifyRegExp (Then x y) | x' == Star a && y' == x' = y'
it works fine for b but not for any other letter. So my question is why it works fine in the Star part but not in the Then part?
I've added some of the important parts of the code below but feel free to ask for more if its not enough.
data RegExp sy = Empty -- ^ Empty Language
| Epsilon -- ^ Empty String
| Literal sy -- ^ Literals
| Or (RegExp sy) (RegExp sy) -- ^ Disjuncion
| Then (RegExp sy) (RegExp sy) -- ^ Sequence
| Star (RegExp sy) -- ^ Repetition, possibly zero time
deriving (Read, Eq)
a = Literal 'a'
b = Literal 'b'
c = Literal 'c'
simplifyRegExp Empty = Empty
simplifyRegExp Epsilon = Epsilon
simplifyRegExp (Literal x) = Literal x
simplifyRegExp (Star x) = case x' of
Or a Epsilon -> Star (simplifyRegExp a)
where x' = simplifyRegExp x
simplifyRegExp (Then x y) | x' == Star a && y' == x' = y'
where x' = simplifyRegExp x
y' = simplifyRegExp y
You're having variable scoping issues.
In the case pattern match Or a Epsilon the a is a fresh variable bound locally to the right side of that case rule (i.e. Star (simplifyRegExp a)). Later, in the equation for simplifyRegExp (Then x y) you refer to Star a where the a is not locally bound but instead refers to the top-level definition
a = Literal 'a'
That's almost certainly not the behavior you're intending as the simplification of a regular expression goes over its structure and ignores the actual choice of literals.
Let's say I have a tuple:
let x = (1,3)
I want to add 1 to only the first value of the tuple. How would I do that?
You use pattern matching to deconstruct the tuple and then construct the updated one:
let (x1, x2) = x in (x1 + 1, x2)
Patterm matching is a typical idiom. Another way would be with fst and snd:
# let x = (1,3);;
val x : int * int = (1, 3)
# let y = (fst x + 1, snd x);;
val y : int * int = (2, 3)