I'm new in Haskell and kind of struggling with an issue I'm not even sure I understand so I could use some help to fix it...
I'm trying to create a simple function that, given three digits "a" "b" and "c", would display all combinations where "a" is smaller than "b" and "b" i smaller than "c" while incrementing them. For instance "4", "5" and "6" would give something like "456, 457, 458, 459, 467, 468, 469, 478, 479, 489, 567, 568, 569, 578, 579, 589, 678, 679, 689, 789"
So far my code looks like this :
axx :: Int -> Int -> Int -> Int
axx (x) (y) (z) = if y == 8 && z == 9
then x 1
else x
ayy :: Int -> Int -> Int -> Int
ayy (x) (y) (z) = let y = if z == 9 && y < 8
then y 1
else if z == 9 && y == 8
then x 1
else y
azz :: Int -> Int -> Int -> Int
azz (x) (y) (z) = let z = if z == 9
then y 1
else z 1
my_print_comb :: Int -> Int -> Int -> IO ()
my_print_comb (x) (y) (z) = do print (x)
print (y)
print (z)
if (x /= 7 && y /= 8 && z /= 9)
then do
putStr ", "
let x = axx (x y z)
let y = ayy (x y z)
let z = azz (x y z)
my_print_comb(x y z)
else putStr "\n"
But when I try to compile it, I get the "parse error (possibly incorrect indentation or mismatched brackets)" error message regarding the line where my "azz" function starts. I tried looking for similar case on the web but without any luck so far.
Could anyone help me?
CodePudding user response:
You can remove a lot of parentheses: axx (x) (y) (z)
-> axx x y z
. print (x)
should be written print x
.
You have to remove let y =
and let z =
Li-yao said.
Also remove the parentheses
let x = axx (x y z)
let y = ayy (x y z)
let z = azz (x y z)
my_print_comb(x y z)
->
let x = axx x y z
y = ayy x y z
z = azz x y z
my_print_comb x y z
Otherwise x
is interpreted as a function applied to y
and z
.
Using function application manifest what you wrote is axx $ ((x $ y) $ z)
when you meant ((axx $ x) $ y) $ z
.
infixr 0 $
($) :: (a -> b) -> a -> b
($) = id
In general intead of checking axx x y z = if x == 8 && z == 9 then .. else
you use pattern matching axx 8 y 9 = ..
.
Use undefined :: a
to develop iteratively
axx :: Int -> Int -> Int -> Int
axx x 8 9 = x 1
axx x y z = undefined
Try to separate the IO
out of my_print_comb
.
CodePudding user response:
Remove let y =
and let z =
in ayy
and azz
.