I am doing 99 Haskell problems, and in one of the solutions I came across the following code:
pack' [] = [] pack' [x] = [[x]] pack' (x:xs) | x == head h_p_xs = (x:h_p_xs):t_p_hs | otherwise = [x]:p_xs where p_xs@(h_p_xs:t_p_hs) = pack' xs
I am wondering when the package will be called in the first guard, and if it is a common template in Haskell code, to refer to the head and tail of the list returned by the function call. Are there several calls for the package at any recursion level and is this a quick fix?
I wonder when the package will be called into the first guard
A guard x == head h_p_xsforces a evaluate h_p_xs, which calls a recursive call.
x == head h_p_xs
h_p_xs
and if this is a common template in Haskell code to refer to the head and tail list returned from the function call.
, . case pack' xs of ... let ... = pack' xs in ....
case pack' xs of ...
let ... = pack' xs in ...
, let where , h_p_xs:t_p_xs, , . , emlty.
let
where
h_p_xs:t_p_xs
'
, Haskell , , , . , , .
, , - .
, , (!)
... where p_xs = h_p_xs:t_p_hs h_p_xs = head (pack' xs) t_p_xs = tail (pack' xs)
, .
?
. , .
pack ', , Data.List.group. . , pack '[1,1,3,2,2] [[1,1], [3], [2,2]].
- haskell. , ,
h_p_xs:t_p_hs = pack' hs
, hs . , h_p_xs , t_p_hs . LHS, RHS , ( ) Haskell. p_xs RHS (@pattern). , , Haskell.
. , , , .
pack ', - O (n) . .
, . , , -
p_xs@( (h_p_x : h_p_xs) : t_p_hs) = pack' xs
p_xs@( h_p_xs : t_p_hs ) = pack' xs
, h_p_x. :
h_p_x
| x == head h_p_xs = (x : h_p_x: h_p_xs):t_p_hs
So you see that using the operator :here is just cluttering up the code by adding useless objects. As for the number of recursions, I see here only one recursive call at each level, so this is basically linear runtime and therefore efficient.
:
Source: https://habr.com/ru/post/1568663/More articles:How to upload files - sails.js - javascriptjavascript datetime для строки и обратно в datetime? - javascriptAngular + Problem with Laravel + UI-Router - angularjsNG style with interpolated value not displaying background image? - javascriptAndroid custom theme - androidAn Xcode project with different file versions for each purpose? - iosHow to store arguments of variational type? - c ++Why is this code compiling? (java generic method) - javaИсходный источник данных FormView потерян при нажатии кнопки в другом виде - c#Unable to convert traditional for loop into each loop - javascriptAll Articles