Hi, I'm really, really new to Haskell. I'm practicing with simple online questions and there's a hcf question which I found the answer to online(http://snipplr.com/view.php?codeview&id=11973) but I still don't understand the coding. I'm not sure if this kind of question is accepted in Daniweb but yeah. Could someone please explain to me?

hcf a 0 = a
hcf a b = hcf b y
    where y = mod a b

mod a b, ofc understandable but the first 2 lines. why is there a 0, why = a ?

Hi!

Those are definitions of the same function (hcf), the first line is defining a default in case the second argument is 0, in that case returns the first argument (a). Why? Because if you run mod 1 0 you get an exception:

*** Exception: divide by zero

In haskell there are partial and total functions, mod is partial, as it returns an error in case the argument is not a valid value. By defining the default for 0 you cover that error. See:

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.