Can someone explain this Haskell?
Been working on some homework with some great help from some members, but a course fellow just showed me this. It boggles my brain the format and exactly how its working? I tried to tweak it to learn about it but I don't get it.
fun2 :: String -> [String]
fun2 [] = []
fun2 (x:xs) = [fun1 (x:xs)] ++ runs (drop (length (munch (x:xs))) (x:xs))
fun1 is :
fun1 (x:xs) = group (x:xs)
Could someone break this down for me in the aids of learning? Using one function into another is required by the work.
Again this is homework,开发者_JAVA技巧 I'm just asking for guidance to understand Haskell as I can't get my head around it!
Some pseudocode to explain what's happening when fun2 is called:
if the argument is [] (the empty list)
return []
else (the argument is a non-empty list x:xs)
fun1Result = fun1 (x:xs)
fun1List = [fun1result] -- a list of one element
munchResult = munch (x:xs)
lengthResult = length munchResult
dropResult = drop lengthResult (x:xs)
runsResult = runs dropResult
return fun1List ++ runsResult -- concatenate the two lists
In Haskell, functions are applied just by putting a space between the function and the argument. So f x
calls function f
with the value x
. Function application is evaluated from left to right, so the parenthesis are just there to make sure everything happens in the right order.
Hopefully this makes the syntax less confusing. I don't think munch
or runs
are standard functions, so I can only guess at what they do.
精彩评论