I am trying to define a function that computes for any positive integer the square of its factorial
(I am a beginner in Haskell any tips or help is appreciated)
I have tried a couple different ways one i believe to work and one definition i don't understand why it doesn't work
Function i believe works:
squarefact:: Int -> Int
squarefact 0 = 1
squarefact n = n * n * squarefact(n-1)
Run Code Online (Sandbox Code Playgroud)
Function I don't understand why it doesn't work:
squarefact:: Int -> Int
squarefact 0 = 1
squarefact n = (n * squarefact(n-1) ) * (n * squarefact(n-1) )
Run Code Online (Sandbox Code Playgroud)
An explanation and walk through of the dunctions defined would help me understand them better thanks.
等式
squarefact n = (n * squarefact(n-1) ) * (n * squarefact(n-1) )
Run Code Online (Sandbox Code Playgroud)
可以用数学符号重写为
(n!)^2 = n * ((n-1)!)^2 * n * ((n-1)!)^2
Run Code Online (Sandbox Code Playgroud)
但这个身份是不正确的。右侧包含1,2,....,n-1 四次因子,而不是左侧的仅两次。
通过对比,
squarefact n = n * n * squarefact(n-1)
Run Code Online (Sandbox Code Playgroud)
是正确的,因为双方的所有因素都恰好出现两次。