Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Can someone explain this Haskell?

Tags:

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, I'm just asking for guidance to understand Haskell as I can't get my head around it!

like image 716
James MV Avatar asked Dec 10 '25 13:12

James MV


1 Answers

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.

like image 101
Jay Conrod Avatar answered Dec 12 '25 09:12

Jay Conrod



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!