Is there a way to simplify:
if x == 1 and y == 2 and z == 3:
if x == 1 and y == 1 and z == 1:
if x == 1 or y == 2 or z == 3:
if x == 1 or x == 2 is simplified as if x in [1, 2]:
One of your examples is not like the others. The and form can easily be simplified:
if x == 1 and y == 2 and z == 3:
becomes:
if (x, y, z) == (1, 2, 3):
However, the or form can't be made any neater. It could be rewritten as:
if any(a == b for a, b in zip((x, y, z), (1, 2, 3))):
but that's hardly "simplified".
Related
I'm very new to haskell.
How can I return (x1,x2) and print it out from my code?
qqq x
| x < 0 x1 = mod (-x) 10
| 1 < x && x < 99 x1 = mod x 10
| x2 = mod x 10
You are using guards the wrong way. You seem to see these as if statements, that you then can use for assignements. In Haskell, you do not assign values to a variable, you declare these. You can work with:
qqq :: Integral a => a -> (a, a)
qqq x
| x < 0 = (mod (-x) 10, x2)
| 1 < x && x < 99 = (mod x 10, x2)
where x2 = mod x 10
Here each guard thus has a condition before the equation sign (=), and at the right side returns a 2-tuple with as first item an expression for x1, and as second item x2.
You should also implement extra case(s) for x == 1 and x >= 99, these are not covered by the two guards.
First of all sorry for my Englisch, I am new in Haskell and i dont really know much about it.
I want all even Numbers from [0..6] and then the square Numbers from them. Like this
a x = [ x * x | x <- [0 .. 6], x `mod` 2 == 0, x > 0]
but with map and filter.
i tried this one, but i dont know where i can put the x*x in there
amap x = map'(\x -> (filter'(\x -> x `mod` 2 == 0 && x > 0)[0..6])) [1,2]
the ouput of this is [[2,4,6]]
You should map the result of the filter, expression like:
map (\x -> x*x) (filter (\x -> x `mod` 2 == 0 && x > 0) [0..6])
filter (\x -> xmod2 == 0 && x > 0) [0..6] will thus return a list of items that are even and greater than 0, then we can use map to map each element of that list x to x*x.
I'm trying to print only products of 3 and 5 if x < 1000. I'm getting a lot of repeats. How do I make sure that I don't have any repeats?
for x in range(1000):
y = 3
z = 5
a = x % y
b = x % z
if a == 0:
print (x)
if b == 0:
print (y)
You need to combine the two conditions with disjunction (or):
for x in range(1000):
y = 3
z = 5
a = x % y
b = x % z
if a == 0 or b == 0:
print (x)
The problem:
You are given a function plusOne x = x + 1. Without using any other (+)s, define a recursive function addition such that addition x y adds x and y together.
(from wikibooks.org)
My code (it does not work -- endless loop):
plusOne x = x + 1
addition x y
| x > 0 = addition (plusOne y) (x-1)
| otherwise = y
Questions:
How to connect the plusOne function to the addition recursive function?
How should it be written?
You are mixing up x and y in your recursive case
addition x y | y > 0 = addition (plusOne x) (y - 1) -- x + y == (x + 1) + (y - 1)
| otherwise = x -- x + 0 = x
using == and 0
addition = add 0 where
add a y x | a == y = x
| otherwise = add (plusOne a) y (plusOne x)
Let's say I have a function:
isOne :: Int -> Int -> Int
isOne x y =
Then if x == 1 and y != 1 then it returns 1 (one of the parameters equals 1), if x == 1 and y == 1 it returns 2 (because both are 1), if x != 1 and y != 1 it returns 0 etc.
I can't figure out how to do more than a single check with an if statement (or using cases).
Why, you just need to translate your english to Haskell:
if (x==1) && (y /= 1) then 1
else if (x/=1) && (y==1) then 1
...
But you really want:
isOne 1 1 = 2
isOne 1 _ = 1
isOne _ 1 = 1
isOne _ _ = 0
or, even shorter:
isOne x y = fromEnum (x==1) + fromEnum (y==1)
The easiest way to this is with pattern matches. You can define a function by cases, which are interpreted in the order at which they occur
isOne 1 1 = 2
isOne 1 _ = 1
isOne _ 1 = 1
isOne _ _ = 0
alternatively, you can use guards
isOne x y | (x == 1) && (y == 1) = 2
| (x == 1) && (y != 1) = 1
| (x != 1) && (y == 1) = 1
| otherwise = 0
again, these are checked from top to bottom. That is, if the first guard matches then it goes with the first equation, otherwise it tries the second, and so on. This can also be written
isOne x y | (x == 1) && (y == 1) = 2
isOne x y | (x == 1) && (y != 1) = 1
isOne x y | (x != 1) && (y == 1) = 1
isOne x y | otherwise = 0
or
isOne x y | (x == 1) && (y == 1) = 2
isOne x y | (x == 1) || (y == 1) = 1
isOne x y | otherwise = 0
another way of doing it would be to use an if then else expression.
isOne x y = if (x == 1) && (y == 1)
then 2
else if (x == 1) || (y == 1) then 1 else 0
or perhaps you could try doing
isOne x y = (go x) + (go y) where
go 1 = 1
go _ = 0
or any of dozens of other ways...
Method 1
Use paired case statements
isOne x y = case (x, y) of
(1, 1) -> 2
(1, _) -> 1
(0, 0) -> 0
...
Method 2
Use nested if statements
isOne x y = if x == 1 then (if y == 1 then 2 else 1) else 0
I'd second using either direct pattern matching in the function definition, or case on tuples.
But the most readable alternative would IMO be length [ q | q<-[x,y], q==1 ].