1 recursion haskell subsequence
我试图制作调用子序列的递归函数,但我遇到了一些错误.
我的代码:
recursive 1 list = subsequences list
recursive n list = subsequences (recursive (n-1) list)
Run Code Online (Sandbox Code Playgroud)
错误:
Occurs check: cannot construct the infinite type: a1 ~ [a1]
Expected type: [a1]
Actual type: [[a1]]
Relevant bindings include
recursive :: a -> t -> [[a1]] (bound at p.hs:6:1)
In the first argument of ‘subsequences’, namely
‘(recursive (n - 1) list)’
In the expression: subsequences (recursive (n - 1) list)
Run Code Online (Sandbox Code Playgroud)
你能帮我解决一下这个问题,还是找另一种方法来调用子序列n次?
对不起,我的英语不好
我没有太多的多态递归,所以我想试试这个.这是我得到的:
{-# LANGUAGE DeriveFunctor #-}
import Data.List (subsequences)
-- Any multiply-nested list such as a, [a], [[a]], [[[a]]], ...
data MultiList a
= Leaf a
| Nest (MultiList [a])
deriving (Show, Functor)
recursive :: Int -> [a] -> MultiList [[a]]
recursive 1 list = Leaf (subsequences list)
recursive n list = Nest (fmap subsequences (recursive (n-1) list))
Run Code Online (Sandbox Code Playgroud)