Skip to content

Instantly share code, notes, and snippets.

@wridgers
Created June 8, 2011 12:59
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save wridgers/1014370 to your computer and use it in GitHub Desktop.
Save wridgers/1014370 to your computer and use it in GitHub Desktop.
fact :: (Integral a) => a -> a
fact f
| f == 0 = 1
| otherwise = f*fact (f-1)
fac :: (Integral a) => a -> a
fac 0 = 1
fac n = n*fac(n-1)
@md2perpe
Copy link

md2perpe commented Jun 8, 2011

Why don't you write the tailrecursive variant fact using direct pattern matching, too?

fact :: (Integral a) => a -> a
fact 0  = 1
fact f  = fact f*(f-1)

@wridgers
Copy link
Author

wridgers commented Jun 8, 2011

I was just experimenting with guards. :) I realise why it didn't work now, stupid typo. fact f_(f-1) should be f_fact (f-1). Whoops! Cheers though. :)

@md2perpe
Copy link

md2perpe commented Jun 8, 2011

I thought that you were trying to create a tailrecursive variant. Like this:

fac :: (Integral a) => a -> a
fac n = tailFac n 1
    where tailFac 0 p = p
          tailFac n p = tailFac (n-1) (n*p)

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment