|
|
||
1.
double (double 2) = double (2 + 2) = double 2 + double 2 = 4 + 4 = 8
正直何をさせたいのかよくわからなかったが、「どんな順番で計算をやっても結局解答は一緒になるよ」ということを確かめさせる問題だったらしい。
2.
sum[x] = x + sum[] = x + 0 = x
簡単な証明問題ですね。
3.
myproduct [] = 1 myproduct (x:xs) = x * product xs product [2,3,4] = 2 * product [3,4] = 2 * 3 * product [4] = 2 * 3 * 4 * product [] = 2 * 3 * 4 * 1 = 24
計算問題。
プログラミングの勉強で計算問題って珍しいよね。
4.
rqsort [] = [] rqsort (x:xs) = rqsort larger ++ [x] ++ rqsort smaller where smaller = [a | a <- xs, a <= x] larger = [b | b <- xs, b > x]
qsortのlargerとsmallerを入れ替えればOK。
5.
同地が1つにまとまる。
nqsort [] = [] nqsort (x:xs) = nqsort smaller ++ [x] ++ nqsort larger where smaller = [a | a <- xs, a < x] larger = [b | b <- xs, b > x]
nqsort [2,2,3,1,1] = [1,2,3]
1.
(2^3)*4 (2*3)+(4*5) 2+(3*(4^5)))
2.実行したよ。
3.
n = a `div` length xs where a = 10 xs = [1,2,3,4,5]
・関数名にNは使えないのでnに変更。
・整数の除算演算子は'div'ではなく、`div`。
・a=10のインデント位置を修正。
4.
last1 xs = xs !! (length xs - 1) last2 xs = head (reverse xs )
5.
init1 xs = take (length xs - 1) xs init2 xs = reverse (tail (reverse xs))
1.
['a','b','c'] :: [Char] ('a','b','c') :: (Char, Char, Char) [(False,'o'),(True,'1')] :: [(Bool, Char)] ([False,True],['0','1']) :: ([Bool], [Char]) [tail,init,reverse] :: [[a] -> [a]]
2.
second :: [a] -> a swap :: (t, t1) -> (t1, t) pair :: t -> t1 -> (t, t1) double :: (Num a) => a -> a palindrome :: (Eq a) => [a] -> Bool twice :: (t -> t) -> t -> t
3.確かめた。
4.
よくわからなかったので、解答をそのまま訳してみた。
In general, checking if two functions are equal requires enumerating all possible argument values, and checking if the functions give the same result for each of these values. For functions with a very large (or infinite) number of argument values, such as values of type Int or Integer, this is not feasible. However, for small numbers of argument values, such as values of type of type Bool , it is feasible. 一般的に、 2つの関数が等しいかどうかをチェックするには、すべてのとりうる引数の値を列挙し、 かつ、その2つの関数がこれら引数それぞれに対して同じ値を与えることをチェックすること を必要とする IntやInteger型のように、大変多くの(または無限の)引数値を持つ関数にとって これは不可能である。 しかしながら、 Bool型のように、少ない引数値ならば 可能である。
つまり、2つの関数が等しいかを調べるには、取りうる引数すべてに対して、結果が同じかを調べなきゃならない。
てことはIntみたいに取りうる値がほぼ無限の型が引数になってる関数は、結果が同じであるかを調べるのにどえらい時間がかかる。
だから一般的には無理。
でも、取りうる引数のすべてが少ないBoolが引数になっている関数だったら、全ての引数に対して結果が同じ可動化を比較することができる。
よって、2つの関数が等しいかを調べることができるので、Eqのインスタンスにすることができる。
ってことだよな。