0

I am a newby to Haskell and I am trying to create a function that takes a triple and returns an array of triples, like so:

tripleVariations 1 (0,0,0)
=> [(1,0,0), (0,1,0), (0,0,1)]

I understand how to match to a specific value in the triple with pattern matching and I even created a custom function that maps a function over a triple, but I don't understand how to replace one value of the triple, add that to the array and then continue to the next value in the triple. I find that I tend to try and solve these problems too much according to imperative paradigm, which is exactly what I'm trying to avoid. Any help is welcome, thanks!

ovda96
  • 61
  • 1
  • 8
  • 3
    Surely you can just write it? `tripleVariations w (x,y,z) = [(w,y,z), (x,w,z), (x,y,w)]`? – user253751 Sep 17 '20 at 12:32
  • I am literally facepalming by how simple the solution is, so thank you very much for that! I just wonder if that isn't too much "hardcoding" things, or is this very normal in functional paradigm? Thanks again! – ovda96 Sep 17 '20 at 12:34
  • No, I think "normal in functional programming" is to make a function that can work on any size tuple. But why bother? – user253751 Sep 17 '20 at 12:36
  • Don't abstract for its own sake; abstract if it makes your code simpler. – Lambda Fairy Sep 17 '20 at 12:47
  • Already posted this to the published answer, but: I assumed that I simplified the problem that I am trying to solve in such a way that it still is usable for the eventual solution, but now I worry that I do need a bit more in debt information: I actually need to be able to check a condition when adding the eventual triple to the array, so, i.e., only replace when the initial value is 0 would result in: `tripleVariations 1 (0,0,2)=> [(1,0,2), (0,1,2)]` – ovda96 Sep 17 '20 at 12:52

1 Answers1

3

Surely you can just write it:

tripleVariations w (x,y,z) = [(w,y,z), (x,w,z), (x,y,w)]

If you wanted this to work with any size tuples, it would be more complicated... but you don't, so why waste the effort?.

According to Haskell: how to map a tuple? there is no way to iterate over a tuple because tuples can have diferent types in them. It wouldn't make sense to iterate over the type (Int, String, Char) for example. If you want to write code that works with tuples where every part has the same type, presumably you should use a list instead of a tuple.

user253751
  • 57,427
  • 7
  • 48
  • 90
  • I assumed that I simplified the problem that I am trying to solve in such a way that it still is usable for the eventual solution, but now I worry that I do need a bit more in debt information: I actually need to be able to check a condition when adding the eventual triple to the array, so, i.e., only replace when the initial value is 0 would result in: `tripleVariations 1 (0,0,2)=> [(1,0,2), (0,1,2)]` – ovda96 Sep 17 '20 at 12:39
  • @ovda96 Updated. I suspect it is impossible without simply writing out the whole code, because you cannot iterate over tuples. (Well, you could write your own function to iterate over tuples) – user253751 Sep 17 '20 at 12:54
  • @ovda96 A standard trick for building a 0-or-1-length list is to use comprehension syntax. For example, if your predicate for checking whether a particular spot should be substituted is named `p`, you can write `[(w,y,z) | p x] ++ [(x,w,z) | p y] ++ [(x,y,w) | p z]`. – Daniel Wagner Sep 17 '20 at 14:13
  • Thanks @DanielWagner! I am however trying to adapt this to a list in the end, so I will post a new question :). – ovda96 Sep 17 '20 at 19:03