Pattern matching for lambda expressions

前端 未结 1 988
广开言路
广开言路 2021-02-07 22:03
 21 --Primitive recursion constructor
 22 pr :: ([Int] -> Int) -> ([Int] -> Int) -> ([Int] -> Int)
 23 pr f g = \\xs 0 -> f xs
 24 pr f g = \\xs (y+1)          


        
1条回答
  •  无人共我
    2021-02-07 22:21

    pr f g = \xs y' -> case y' of 0     -> f xs
                                  (y+1) -> g xs y ((pr f g) xs y)
    

    or simply

    pr f g xs 0 = f xs
    pr f g xs (y+1) = g xs y ((pr f g) xs y)
    

    (Remember that f a b = ... is basically a shortcut for f a = \b -> ... which is a shortcut for f = \a -> \b -> ....)

    Note that n+1 patterns are deprecated and that the type you specified for pr does not match your (and mine) definition.

    Specifically according to your type the function takes an [Int] -> Int (f), then a function that takes another [Int] -> Int (g), then a function that takes an [Int] (xs) and then returning an Int. However you call g with three arguments and the last function that you return takes two arguments, so presumably you want something like ([Int] -> Int) -> ([Int] -> Int -> Int -> Int) -> [Int] -> Int -> Int.

    0 讨论(0)
提交回复
热议问题