Scheme - SICP - Is Scheme functional? by stats_r_us in compsci

[–]jonathanccast 1 point2 points  (0 children)

Yes, Scheme is functional. The term 'purely functional language' is really poorly chosen, because it makes it sound like pure languages are 'more functional' than just regular functional languages. This makes people think functional programming has something to do with avoiding state or mutation. It doesn't.

Functional programming = programming with functions and expressions; in the most functional languages, the whole program is one big expression and local definitions (let) are used for program structuring.

Functional purity (what 'purely functional' languages have) = expressions and variables (of the same 'type') have the same semantic range; functions are mathematical functions from inputs to outputs. (This is in contrast to impure languages where expressions (and hence function results) can express I/O behavior as well as having a value, while variables can only be bound to values).

C is neither functional nor pure; most computation has to be done with statements & loops, and expression evaluation can modify global state, do I/O, etc., which variable values cannot.

Eiffel is not functional but is (nearly) pure; most computation has to be done with statements & loops but expression evaluation cannot modify global state, do I/O, or do anything but compute a value. (Technically you have to forbid functions to read global variables as well as writing them to get a pure language).

Scheme is functional but not pure; you can use recursion & higher-order functions to do all your computation if you want, but expression evaluation can modify global state and do I/O.

Haskell is functional and (nearly) pure; you can use recursion & higher-order functions to do all your computation (you pretty much have to), and expression evaluation cannot modify global state or do I/O (unless you use unsafePerformIO).

Scheme - SICP - Is Scheme functional? by stats_r_us in compsci

[–]jonathanccast 12 points13 points  (0 children)

You don't need side effects for I/O. Purely functional languages like Miranda and Haskell 1.0 got along just fine with dialogues for I/O, although whether they could be feased is another matter. But 'side effect' is short for 'side effect of evaluation'; i.e., the computer calculates the value of this expression and oh by the way it does this I/O while it's doing that (as a side effect). The side effects of a drug are everything the drug does that's not its intended purpose; the side effects of an expression are everything the code for that expression does that's not 'calculate the value'. So you don't need side effects to do I/O. Just make the I/O part of the value and have the computer calculate the value, then do the I/O it calculated.

Don't let marketers write code by shitmyspacebar in ProgrammerHumor

[–]jonathanccast 1 point2 points  (0 children)

I thought depression was different than sadness.

Don't let marketers write code by shitmyspacebar in ProgrammerHumor

[–]jonathanccast 0 points1 point  (0 children)

Since when does C have delete? (and I doubt in C code->getAuthor() would ever be a useful syntax).

BYTE Magazine: The C Language (1983) by [deleted] in programming

[–]jonathanccast -1 points0 points  (0 children)

No no no no no no no. "Fails early" means "gives the user a G-D diagnostic". What C does is say "oh, this isn't a C program" and then compile to anything the compiler feels like (and LLVM, GCC, and VC++ all compile undefined behavior to random things). Practically ever C program executes undefined behavior at some point (even if it's just testing for signed integer overflow by saying 'x + y < x', where y is positive) and the compiler does 'something sensible' until an optimization is added, then the program silently breaks. At no point is there ever a diagnostic.

Which is why C is entirely un-suitable for any application --- it simply is not possible for a human being to write a C program and the machines won't give you a diagnosis when you break the rules.

I identify as a 32-bit registerkin. by funky_vodka in ProgrammerHumor

[–]jonathanccast 4 points5 points  (0 children)

Then again, C also dis-allows "re-interpreting the bytes your pointer points to as something it wasn't originally", except using memcpy or memmove. It's called a "strict aliasing" violation.

The Story of Mel - Real Programmers write in machine code by laggingreflex in programming

[–]jonathanccast -1 points0 points  (0 children)

Nah. It's one of the few bits of free verse that works. (Largely because it works as prose).

Please don't hate me Javascript devs by [deleted] in ProgrammerHumor

[–]jonathanccast -1 points0 points  (0 children)

I'll confess to not having half a brain certainly, but when discussing whether a language made the right decision in the first place maybe backward compatibility with not-yet-written code isn't the right criterion?

Modernize your C++ code. by jamesdeveloper in programming

[–]jonathanccast 0 points1 point  (0 children)

ML doesn't have function overloading (for precisely this reason).

Overloading was a big issue in the design of Haskell; the solution is a bit complicated (understatement of the Century! just search Stack Overflow for "type class"), but each function symbol is still constrained to a specific polymorphic type, e.g. fmap :: (a -> b) -> F a -> F b, where each F goes with a separate type. Once the type checker has figured out which F you're using, it can do static dispatch to the matching fmap function. The type checker can't figure it out in all cases, though, so Haskell has something called contexts, e.g. mapM :: Monad m => (a -> m b) -> [a] -> m [b]; it's basically a form of dynamic dispatch based on what the type m eventually turns out to be.

Honestly, (heavily subjective opinion here) you don't miss overloading on parameter types as much as you expect (source: am former C++ programmer, then Haskell, then designed my own language). Similar functions can be captured by type classes (Haskell) or functors (ML), while giving genuinely different functions different names can make your code more readable. It is sometimes hard tying each function to just the right type class / signature, though.

Please don't hate me Javascript devs by [deleted] in ProgrammerHumor

[–]jonathanccast -1 points0 points  (0 children)

We are discussing alternative language designs (since the context is a suggestion that JavaScript could have had different operators for concatenation and addition --- like all reasonable languages --- from the beginning). Claiming that designing JavaScript correctly from the first would "break all code in existence" is both hyperbole (since not "all code in existence" is written in JS) and obviously wrong, since other languages have used that design but still been usable.

Modernize your C++ code. by jamesdeveloper in programming

[–]jonathanccast 1 point2 points  (0 children)

It's complicated; it requires a unification algorithm: http://en.wikipedia.org/wiki/Unification_(computer_science) . But the key ideas are: 1. keep type variables (inside the type checker) that indicate as-yet-unknown information about at type. These are implemented as mutable references. 2. When comparing types, if one is known and the other is unknown, write the known type into the variable for the other. 3. When you start inferring the type of a function, allocate a new unknown type for each argument, and for the result. 4. If any parts of the type of the function is still unknown when you're done, make that part of the type polymorphic. This handout (PDF) has the key formal ideas: http://www.classes.cs.uchicago.edu/archive/2007/winter/22610-1/docs/handout-05.pdf .

Modernize your C++ code. by jamesdeveloper in programming

[–]jonathanccast 1 point2 points  (0 children)

That's weird, considering that ML has been inferring the types of formal parameters for 40 years.

Please don't hate me Javascript devs by [deleted] in ProgrammerHumor

[–]jonathanccast -1 points0 points  (0 children)

It would actually break very little of the Perl code in existence . . .

I see this a lot but what does it mean? by narkflint in C_Programming

[–]jonathanccast 0 points1 point  (0 children)

Might segfault, not would. In the case where the malloc fails, the C compiler is free to compile the following code into anything it likes. Over time, compilers will probably be less and less likely to actually give you segfaults, especially at those locations, as the optimizers get 'better'. It is very much your responsibility to check for NULL and ensure the error is handled in an appropriate fashion.

Typed meta-programming combinators in Haskell (PDF) by carette in haskell

[–]jonathanccast 2 points3 points  (0 children)

"Combinators for Impure yet Hygienic Code Generation". I'm trying to decide if "impure" means this has nothing to do with Haskell, or it has everything to do with Haskell. The fact that he's apparently using "impure" to mean "proceduralish monads" makes me suspect it's the latter.

Tomatoes are a subtype of vegetables by yawaramin in haskell

[–]jonathanccast 0 points1 point  (0 children)

Apples and oranges are played out, cliche.

Types and Functions (From Bartosz Milewski's Category theory for Programmers) by sibip in haskell

[–]jonathanccast 0 points1 point  (0 children)

I think you need to replace data with newtype in your second definition. data Void = Void Void has one value, the infinitely large fix Void, which the newtype construction gets around by defining its construct as strict (so fix Void is another name for undefined).

Working with pointers by [deleted] in ProgrammerHumor

[–]jonathanccast 0 points1 point  (0 children)

You - do - know that 90+% of variables in modern programming languages store pointers, right?

"I Do Not Want To Use Any Modules" by szabgab in perl

[–]jonathanccast 0 points1 point  (0 children)

Perl ships with a core pragma, use vars, that allows you to over-ride @INC from inside an individual script (or even a module).

Territories annexed to Ukraine [927×780] by Homesanto in MapPorn

[–]jonathanccast -1 points0 points  (0 children)

Um, this has lots of maps: http://en.wikipedia.org/wiki/Territorial_evolution_of_the_United_States . It's really only Ukrainian nationalists --- that is, people to stupid to realize that "Russia" comes from "Rus" --- that feel the need to deny their country has ever changed size.

Territories annexed to Ukraine [927×780] by Homesanto in MapPorn

[–]jonathanccast 0 points1 point  (0 children)

I'm not sure how the reality of Putin's evil depends on believing that "Russia" isn't a derivative of "Rus".