5 ms·
The thing about syntax like this fac :: Int -> Int fac 0 = 1 fac n = n * fac (n-1) to me, is that all the statements of this function declaration
by joe_the_user 9y ago
The thing about syntax like this
fac :: Int -> Int
fac 0 = 1
fac n = n * fac (n-1)
to me, is that all the statements of this function declaration seem "disconnected". I'm guessing the "fac" connects them but it seems less clear than open-closed brackets in a c-like language (and also more cluttered). Especially, I'm not sure what tells me I've reached the end of the function declaration. This may seem trivial but it's always put my off this variety of functional language.
- tueo23094 9y agoI don't program in functional environments, however this seems beautiful to me. I do understand your argument and tend to agree, I would like to see a syntax error if there is anything between the multiple lines of implementation.
- garmaine 9y agoThere would be
- stingraycharles 9y agoI think this is exactly the same as Haskell. As a matter of fact, I had to actually search their Wiki for the first example that was different from Haskell I think it’s just a matter of familiarity. Once you learn how it works, it’s actually extremely elegant and in no way “disconnected.”
- ggm 9y agoI 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
- deleted 9y ago[deleted]
- dmead 9y agothe definition of fact ends when the lines starting with fac end. they aren't doing something nuts like mixing up lines of a function definition if that's what you're getting at. also, this is syntactic sugar for a case statement.. and you couldn't just use those.
- willtim 9y agoThe style is lifted straight from mathematics tradition. It is a closed definition and so it would be syntax error to insert anything between the lines. In Haskell you could always avoid that style and use the case syntax (or for this example guard syntax) instead. All varieties eventually desugar to a case analysis anyway. Haskell even added curly braces as an alternate to indentation!
- Someone 9y agoIs it a closed definition? The equivalent in Mathematica would allow one to add things whenever one wants, for example to add definitions for fac for negative integers, or to hard-code the value of fac 100. If clean has a REPL (which I couldn’t easily find out from its web page) I expect it to have that, too.
- willtim 9y agoIn Haskell it certainly is a closed definition, you would get a "multiple declarations" syntax error if you tried to split the definition up. I would assume the same for Clean also. In the Haskell REPL, you would need to enter the definitions together.
- TheAceOfHearts 9y agoAlso, aren't the types incorrect? Glancing over their docs, Int represents a signed integer. A negative input shouldn't be allowed, nor can you guarantee that your output fits in an int. Now I'm slightly curious as to how they handle overflows. Maybe they do runtime checks? Conflating of data types with storage types is a surprisingly common occurrence, although I can appreciate it's largely due to pragmatism.
- e12e 9y agoReminds me of this (old) issue I recently came across wrt factorial in Julia (just googling to figure out promotion to handle 52! - the trick is using a bigint). Point being, you're spot on about mixing types and even "simple" functions need not be trivial (for various reasons, eg: factorial of a negative number - might be defined as negative infinity): https://github.com/JuliaLang/julia/issues/6579 https://github.com/JuliaLang/julia/issues/6579
- 1ris 9y agoJust implemented a List in OOP, just to proof a (different) point. Everything is way more "disconnected" in OOP. For example I have to implement length in both the classes EmptyElement (0) and Element (1+next->length()). Both belong together, but they are scattered across the source code. With clean like syntax i would have written them right next to each other.
- davidgrenier 9y agoAnyone who hasn't tried a whitespace-significant language in anger is missing out. I had started with F# and was put off by it like you described. I moved to Haskell and had difficulty understanding it yet again, but when I moved back to F# one more time it was a delight.
- dorfsmay 9y agoI LOVE python, it is the weapon I first take to any battle and the language I recommend to beginners. But, I have to admit that the fact that it is so easy to lose track of which "if" a nested "else" belongs to when modifying code, and, I've seen this being the source of so many bugs, that it has changed my opinion about whitespace-significant languages. Also, I've come to appreciate when I write in say Rust, the fact that I can let go of the syntax, just write and focus on my ideas and let my editor sort the mess at the end.
- daveFNbuck 9y agoHow does Rust make it any easier?
- dorfsmay 9y agoAll clauses are defined within curly brackets like C so indentation has no meaning and you can let tools take care of that.
- vram22 9y agoAlso, with editors like vim, you can press % to go to the matching brace (curly bracket). In fact that works in vi too, right from the early vi days; also, in vim, if you position the cursor on an opening or closing brace, it momentarily highlights the other one (at least if that brace is visible on the screen, can't remember offhand if this works even if not visible). I still like languages like Python and F# though.
- daveFNbuck 9y ago
- ape4 9y agoEspecially if the function has a longer name.
- dev_sebbot 9y agoI think about it like you're listing various definitions for what something is, given various patterns to match against. For me, this makes it more palatable because each pattern has a different operation associated with it. It's almost like binding multiple functions to one name that are called based on what you pass in, instead of creating one function to do the operation -- even though that's exactly what you're actually doing. It's declarative programming, in a way.
- bobthechef 9y agoThis is what makes it clear. Instead of burying the value somewhere in an if expression, pattern matching makes it immediately clear what the evaluated value is for a given argument.
- agumonkey 9y agoas other said it's math tradition, your point about "open" fear is valuable though, you'd have to trust people not to scatter bits of defintion but well it's never a real problem. About style, after a while spent reading about FP, I just can't solve problems without it. Most of my code quickly ends up in lines of // f <param-1 subset> <param-2 subset> => desired structure //// type // f <param-1 subset'> <param-2 subset'> => desired structure //// type which then gets translated on whatever language I end up writing (javascript, java, emacs lisp ..)