How to change the order of arguments?

前端 未结 2 1083
[愿得一人]
[愿得一人] 2020-12-24 02:28

What if I want to change the order of arguments in a function?

There is flip:

flip :: (a -> b -> c) -> b -> a -> c
         


        
2条回答
  •  死守一世寂寞
    2020-12-24 03:23

    The best way in general is to just do it manually. Assume you have a function

    f :: Arg1 -> Arg2 -> Arg3 -> Arg4 -> Res
    

    and you would like

    g :: Arg4 -> Arg1 -> Arg3 -> Arg2 -> Res
    

    then you write

    g x4 x1 x3 x2 = f x1 x2 x3 x4
    

    If you need a particular permutation several times, then you can of course abstract from it, like flip does for the two-argument case:

    myflip :: (a4 -> a1 -> a3 -> a2 -> r) -> a1 -> a2 -> a3 -> a4 -> r
    myflip f x4 x1 x3 x2 = f x1 x2 x3 x4
    

提交回复
热议问题