Is there, in Haskell, something similar to sub-guards?

前端 未结 3 1712
离开以前
离开以前 2020-12-03 17:34

I\'m writing a program on the classification of musical intervals. The conceptual structure is quite complicated and I would represent it as clearly as possible. The first f

3条回答
  •  [愿得一人]
    2020-12-03 18:12

    I'd recommend to group each nested condition in a function:

    interval :: _ -> _ -> (String, String)
    interval pt1 pt2
        | gd == 0 = doSomethingA pt1 pt2
        | gd == 1 = doSomethingB pt1 pt2
        | gd == 2 = doSomethingC pt1 pt2
        ...
    

    and then, for example:

    doSomethingA :: _ -> _ -> (String, String)
    doSomethingA pt1 pt2
        | sd <  (-2) = ("unison",show (abs sd) ++ "d") 
        | sd == (-2) = ("unison","dd")
        | sd == (-1) = ("unison","d")
        | sd == 0    = ("unison","P")
        | sd == 1    = ("unison","A")
        | sd == 2    = ("unison","AA")
        | sd >  2    = ("unison",show sd ++ "A")
        where sd = displacementInSemitonesOfPitches pt1 pt2  
    

    Alternatively you can use the MultiWayIf language extension:

    interval pt1 pt2 =
        if | gd == 0 -> if | sd <  (-2) -> ("unison",show (abs sd) ++ "d") 
                           | sd == (-2) -> ("unison","dd")
                           | sd == (-1) -> ("unison","d")
                           ...
           | gd == 1 -> if | sd <  (-1) -> ("second",show (abs sd) ++ "d")
                           | sd == (-1) -> ("second","dd")
                           | sd == 0    -> ("second","d")
                           ...
    

提交回复
热议问题