這是一個練習:
-- Ex 12: recall the binary function composition operation
-- (f . g) x = f (g x). In this exercise, your task is to define a function
-- that takes any number of functions given as a list and composes them in the
-- same order than they appear in the list.
--
-- Examples:
-- multiCompose [] "foo" ==> "foo"
-- multiCompose [] 1 ==> 1
-- multiCompose [( "bar")] "foo" ==> "foobar"
-- multiCompose [reverse, tail, ( "bar")] "foo" ==> "raboo"
-- multiCompose [(3*), (2^), ( 1)] 0 ==> 6
-- multiCompose [( 1), (2^), (3*)] 0 ==> 2
作為初學者,我無法解決這個問題。我嘗試了很多方法,這個方法不起作用:
multiCompose [] = (1*) $
multiCompose fs = (multiCompose (init fs)) (last fs) $
根據我目前的理解,它應該可以作業,因為它可以如下開發:
multicompose [( 1), (2^), (3*)] = multiCompose [( 1), (2^)] (3*) $
= multiCompose [( 1)] (2^) $ (3*) $
= multiCompose [] ( 1) $ (2^) $ (3*) $
= (1*) $ ( 1) $ (2^) $ (3*) $
我的問題
- 你能幫我回答這個練習的有效答案嗎?
- 你能幫我理解為什么我的解決方案不起作用嗎?
非常感謝
uj5u.com熱心網友回復:
你忘記了這個論點,還有一個$:
multicompose [( 1), (2^), (3*)] $ x
-- = multiCompose [( 1), (2^)] (3*) $ x
-- here |
= multiCompose [( 1), (2^)] $ (3*) $ x
= multiCompose [( 1)] $ (2^) $ (3*) $ x
= multiCompose [] $ ( 1) $ (2^) $ (3*) $ x
= ( 1) $ (2^) $ (3*) $ x
而不是[a,b,c,d] = [a,b,c] [d],使用身份[a,b,c,d] = [a] [b,c,d] = a : [b,c,d]:
= ( 1) $ (2^) $ (3*) $ x
= ( 1) $ (2^) $ (3*) $ multiCompose [ ] $ x
= ( 1) $ (2^) $ multiCompose [ (3*)] $ x
= ( 1) $ multiCompose [ (2^), (3*)] $ x
你可以從這里拿走。特別是,multiCompose [] x = xmust 成立,并且是[]論證案例的有效定義。
轉載請註明出處,本文鏈接:https://www.uj5u.com/houduan/368105.html
上一篇:創建嵌套遞回串列而不切片
下一篇:將兩個數字的串列轉換為陣列的索引
