3 ms·
I have struggled with FP for many years. What I try and do, is approach these concepts with an open mind and I look for ways of 'saying' what I see which I then
by ggm 9y ago
I have struggled with FP for many years. What I try and do, is approach these concepts with an open mind and I look for ways of 'saying' what I see which I then rehearse with my FP friends and stick to the one which does not make them wince when I say it.
So this says three true things we know about this 'fac'
Firstly it says it takes an int and it returns an int.
Secondly that if the int it takes is specifically zero then the int it returns is specifically one.
Thirdly and lastly, for all other ints it returns that int, times (in the arithmetic sense) a recursive call to itself of the int one less.
I worry about why it doesn't have to say abs(n) but then I remember two negatives multiplied together are positive.
- tome 9y ago> I worry about why it doesn't have to say abs(n) but then I remember two negatives multiplied together are positive. The function doesn't terminate if n is negative, so you are right to worry!
- always_good 9y agoMight be cool to just overload in the function body instead of at the top level: fac :: Int -> Int (0) => 1 (n) => n * fac (n-1)
- thepratt 9y agoWhat you're describing is the exact same, just different syntax.
- always_good 9y agoYes, we are talking about syntax. OP's first four words: "The thing about syntax".
- thepratt 9y agoNo, the comment replied to has the first four words "Might be cool to". The differences are so minuscule that the argument could be made for it to be 2 differing standards for the same language.
- always_good 9y agoI don't understand your point, much less why you're using the "first four words" thing back at me on my own comment. The differences may be miniscule. They are syntactic. "Might be cool to" have this other syntax. You're just belaboring the obvious.
- coldtea 9y agoThat's the whole point. Syntax can be very influential in how people use (or don't) a language
- thepratt 9y agoYes and no. For what I replied to is essentially garden shedding the language. The overall problem is grasping the concepts, weather or not you want es6 style lambdas, or proper haskell function definitions is irrelevant if you don't understand the concepts.
- mrkgnao 9y agoIn Haskell: fac :: Int -> Int fac x = case x of 0 -> 1 n -> n * fac (n-1) or even {-# LANGUAGE LambdaCase #-} fac :: Int -> Int fac = \case 0 -> 1 n -> n * fac (n-1)
- joe_the_user 9y agoJust adding parenthesis to the original would make it clearer and closer to the mathematics expresses the structure. Especially given that parenthesis are clearly used sometimes to designate arguments in here. fac :: Int -> Int fac(0) => 1 fac(n) => n * fac(n-1)