News for hackers. Stuff that matters.
Re:What if you couldn’t program with loops? by dig1 on Monday July 15, 2019 @04:51AM (#20439113)
In Scheme, the only way to loop is via recursion. In Clojure, all high level functions and looping macros (for/doseq) are implemented via recursion as well (loop/recur).

Yes, it requires significant paradigm shift when you start to use these languages, but after a while, you start to appreciate high level functions (map, reduce) instead of language looping constructs (for, while) - they can be easily composed, parallelized, scaled over cluster, refactored... Even code looks much cleaner.

You like it even more if you are mathematician, because things looks more "natural" :D

Re:What if you couldn’t program with loops? by tomtomtom777 on Monday July 15, 2019 @08:21AM (#20440086)
In this list of loop-special-languages, XQuery also deserves an honorable mention.

With the sequence being the root datatype, it makes loops rather implicit. You can write

    declare function local:formatName($person) {
      xs:concat($person/@firstName, " ", $person/@lastName)
    };
and it will format the name of zero, one or many persons.

Working with XQuery makes you think differently about iteration and shows how much plumbing we normally use for it.

Re:What if you couldn’t program with loops? by thrownaway954 on Monday July 15, 2019 @10:24AM (#20441097)
OMG!!! I remember writing code generators for ColdFusion in XQuery. Total mind shift and, dare I say, ALOT OF FUN!!! It's amazing what XQuery did.
Re:What if you couldn’t program with loops? by dheera on Monday July 15, 2019 @05:23AM (#20439228)
Scheme has a "do" loop that doesn't require recursion. It's just that the people who generally teach Scheme are hell-bent on recursion rather than irritation.
Re:What if you couldn’t program with loops? by reificator on Monday July 15, 2019 @10:46AM (#20441281)
> It's just that the people who generally teach Scheme are hell-bent on recursion rather than irritation.

Freudian slit?

Re:What if you couldn’t program with loops? by chowells on Monday July 15, 2019 @11:59AM (#20442037)
Have you ever looked at how that's implemented? Seems kind of like saying that since it has the y combinator, you never need to use recursion.
Re:What if you couldn’t program with loops? by mratsim on Monday July 15, 2019 @05:09AM (#20439174)
When you need efficient parallel map or reduction you often go back to low-level C loops wrapped in a pretty map/reduce/array package like numpy or R.

I'd argue that Einstein Summation like

`C[i, j] = A[i, k] * B[k, j]` is much more natural than any composition of map reduce you may use to describe the loops of matrix multiplication.

Re:What if you couldn’t program with loops? by dan-robertson on Monday July 15, 2019 @05:49AM (#20439315)
Einstein summation for matrix multiplication isn’t really a great example because we have a perfectly good way to describe matrix multiplication without loops or sufficies or anything else:

  C = AB
Rate is multiplication (and matrix-vector multiplication) is easy to denote. Tensor multiplication (well really tensor contraction) is annoying to denote and that is the point of summation notation.

Notation is easier when there is less to denote:

- Matrix multiplication: only one thing you can do (well you can add to but this distributes)

- Tensor multiplication: all you can do is add, tensor product, and contract. First two are easy, last one you add extra notation for. This notation can also express permuting indices but this is the same as multiplication by a particular tensor and contraction so ok to do.

- map,reduce,fold,etc. These can do quite a lot but not everything (eg processing multiple and in particular different data structures is often annoying. Processing things out of the usual order is often annoying. Processing eg consecutive elements is often annoying). One gets convenience for common things by making it impossible/hard to express uncommon things. If you see something that looks like map(f,x), you know the size of the input is the same as the size of the output and (hopefully) f acts independently on each element.

- loops/tail recursion these give you more power to do things so looking at a loop there are many things that could possible happen and one must look carefully to find which.

Re:What if you couldn’t program with loops? by uryga on Monday July 15, 2019 @07:07AM (#20439640)
> Processing eg consecutive elements is often annoying

the only place i saw a higher-order function for this is the K language with its `eachpair` combinator (spelled `':` iirc). i wonder why it isn't more common!

(though in Haskell i'd just stick a `tails` somewhere to turn `[a,b,c]` into `[[a,b,c],[b,c],[c],[]]` and map over that)

Re:What if you couldn’t program with loops? by MayeulC on Monday July 15, 2019 @05:56AM (#20439343)
That might be true in some way, but having a map/reduce abstraction could help map the operation on a computing cluster if necessary, or more generally abstractions allow to make use of more complex processing techniques (that take into account cache locality, NUMA, advanced matrix multiplication algorithms [1], etc.).

So, I would say that the thing you wrote is easier to understand if you look at the low-level code, but you have to look at it, while you could have the same overview at a high level, which provides a different perspective. In C++, that would be overloading the * operator to perform the loop in a method.

It boils down to top-bottom vs bottom-up, I think. And even though I often use the looping technique, I feel like a lot of my code is spent writing these "useless" loops, while caring for the index, boundaries, etc. I really like vectorization as used in Matlab/Octave to deal with that sort of things, and I feel like this is an answer of its own to the topic.

[1]: https://en.wikipedia.org/wiki/Matrix_multiplication#Complexi...

Re:What if you couldn’t program with loops? by smartstakestime on Monday July 15, 2019 @06:01AM (#20439362)
unless you actually start with these language , then move to loops. Which is really easy. Map/reduce is fundamental to scheme like the closure is fundamental to javascript.

Im not sure how many colleges teach it 1st year but Northeastern University uses scheme as the intro language. The professor there wrote the blue book.

Re:What if you couldn’t program with loops? by onychomys on Monday July 15, 2019 @08:07AM (#20440010)
The University of Minnesota also uses it in their very first CS course.
Re:What if you couldn’t program with loops? by knoebber on Monday July 15, 2019 @09:50AM (#20440779)
Grinnell college intro class uses scheme.
Re:What if you couldn’t program with loops? by LandR on Monday July 15, 2019 @08:50AM (#20440275)
I know of zero universities that use Scheme or any LISP.

Most nowadays that I know of start with Javascript. My old university started with C. It now goes straight to javascript.

Re:What if you couldn’t program with loops? by brlewis on Monday July 15, 2019 @09:30AM (#20440596)
Actually I think you do know of more than zero universities, since you replied to a comment that named Northeastern. A sibling comment to yours named another. Here's another: https://cs.brown.edu/courses/info/csci0170/
Re:What if you couldn’t program with loops? by lispm on Monday July 15, 2019 @10:57AM (#20441396)
Just close your eyes and when you learn about recursion, closures, serialized data, evaluation, ... in Javascript, then just pretend you are using Lisp with a fancy notation. Even the original developer of Javascript wanted to do some Lisp/Scheme, was hired to do so and ended up with a slightly different language...

https://thenewstack.io/brendan-eich-on-creating-javascript-i...

Re:What if you couldn’t program with loops? by akavi on Monday July 15, 2019 @10:24AM (#20441094)
Yale started you off with scheme back in 2012. Not sure if they still do, however.
Re:What if you couldn’t program with loops? by agumonkey on Monday July 15, 2019 @02:20PM (#20443607)
What I like about applicative/functional is that it linearize the thinking process.

- function arguments represent what imperative programming means as state

- each recursive call is a step in the walk toward convergence

- every step has to reduce[0] something in the arguments

- every step is clearly identified in the recursive call

I don't think it is "better" than mutable state + while () { }, but it helps my brain massively.

It may also be linked with old research (Turing and others) about expressing program space as a N-dim vector that has to reach a point in this N-dim space.

Some people might be able to make sense of complex imperative code, old array/string algorithms are a bit like that, maybe Knuth has some dedicated brain area I don't know of. But all in all, starting with functional may be a better pedagogical space.

[0] see htdp, sicp, friedman the <adj> <verb>er books

Re:What if you couldn’t program with loops? by leshow on Monday July 15, 2019 @09:28AM (#20440581)
Haskell as well uses recursion as it's primary mechanism of iteration.