[HN Gopher] How to Average in Prolog (2017)
       ___________________________________________________________________
        
       How to Average in Prolog (2017)
        
       Author : todsacerdoti
       Score  : 31 points
       Date   : 2025-05-07 19:54 UTC (3 hours ago)
        
 (HTM) web link (storytotell.org)
 (TXT) w3m dump (storytotell.org)
        
       | danilafe wrote:
       | This is a strange article to me. I've not seen any class that
       | teaches Prolog place these constraints (use recursion / don't add
       | new predicates) or even accidentally have the outcome of "making
       | prolog look tedious". What's the joke here?
       | 
       | That aside, I wonder if getting the reverse solution (sum(?, 10))
       | is better served by the straightforward or the tedious approach.
       | I suspect both would work just the same, but I'd be curious if
       | anyone knows otherwise.
        
         | 7thaccount wrote:
         | I found the article to be hilarious.
         | 
         | It's existence obviously means some professor didn't allow them
         | to use the standard library and they tongue in cheek show how
         | irritating that is.
         | 
         | I'm sure it's possible they made it up, but we had similar
         | restrictions where I could use an Arduino for my engineering
         | Senior Design Project in college, but no Arduino or module
         | libraries - just our own C. We passed, but barely. It also was
         | weird as it didn't match how normal people would solve the
         | problem.
        
           | Jtsummers wrote:
           | Yeah, this line made it click:
           | 
           | > Now this is starting to look like Professor of Programming
           | Languages code!
           | 
           | A lot of tier 2/3 CS schools with programming language
           | courses (a survey of multiple languages across various
           | paradigms) teach Prolog this way (and I've seen some
           | fantastically terrible examples of Lisp code coming out of
           | those same courses). It's unfortunate because, at these
           | schools, this is often the only formal course where students
           | branch out of C/Java/C++/C#/Python/JS (and they may only get
           | 2-3 of those over the rest of their courses). It leaves the
           | students with a gross misunderstanding of the languages and
           | dread when they see them again.
        
           | Blackthorn wrote:
           | There's nothing wrong with teaching like that. If the class
           | is supposed to teach you something that isn't just "use the
           | stdlib", then it makes sense to ban it.
        
         | floxy wrote:
         | >getting the reverse solution (sum(?, 10))
         | 
         | Doing an "all modes" predicate:
         | sum_list(List,Sum)
         | 
         | ...where...                 sum_list([1,2,3], 6)
         | 
         | ...is "True" as expected would probably be a pretty interesting
         | exercise if you wanted to be able to get all of the lists.
         | You'd probably need to do an "enumerate the rationals" type of
         | thing, since you need to go to infinity in a couple of
         | different directions at the same time. That is, you can't do
         | nested loops/recursing:                  for(x=-[?]; x<[?];
         | ++x) {            for(y=-[?]; y<[?]; ++y) {
         | 
         | i.e.                  sum_list([],0).
         | sum_list([L|Ls],BigSum) :- sum_list(Ls, LittleSum),
         | add(L,LittleSum,BigSum).
         | 
         | ...with an all-modes "add/3". Since there are an infinite
         | supply of pairs that add up to, say 10:
         | [?]         [12,-2]         [11,-1]         [10, 0]         [
         | 9, 1]         [ 8, 2]            [?]         [ 0,10]
         | [-1,11]         [-2,12]            [?]
         | 
         | ...and you can also go to arbitrary list lengths:
         | [10]       [1,9]       [1,1,8]       [1,1,1,7]        [?]
         | [1,1,1,1,1,1,1,1,1,1]        [?]       [0,1,1,1,1,1,1,1,1,1,1]
         | [1,0,1,1,1,1,1,1,1,1,1]        [?]
         | [1,1,1,1,1,1,1,1,1,1,0,0,0,0,0,0,0]        [?]       [-100,-99,
         | -98,...,-12,-11,-9,-8,...,0,1,2,3,4,5,6,7,8,9,10,11,...,99,100]
         | 
         | ...your life probably gets easier if you limit the domain to
         | positive integers (without zero) since then the list length
         | doesn't diverge. Then a declarative looking solution probably
         | just has a all-modes equivalent to "sort" somewhere within?
         | Certainly interesting to think about.
         | 
         | https://www.cs.ox.ac.uk/people/jeremy.gibbons/publications/r...
         | 
         | https://prolog-lang.org/ImplementersForum/0103-modes.html
         | 
         | https://www.swi-prolog.org/pldoc/man?predicate=permutation%2...
        
         | ebolyen wrote:
         | It's been a long time since I took a class like this, but I
         | definitely had a similar experience to the author.
         | 
         | Ideas like fold and map where _never_ mentioned in lisp (to
         | exaggerate, every function had to have the recursive
         | implementation with 5 state variables and then a simpler form
         | for the initial call), at no point did higher-order functions
         | or closures make an appearance while rotating a list by 1 and
         | then 2 positions.
         | 
         | The treatment of Prolog was somehow worse. Often the code only
         | made any sense once you reversed what the lecturer was saying,
         | realizing the arrow meant "X given Y" not "X implies Y", at
         | which point, if you could imagine the variables ran "backwards"
         | (unification was not explained) the outcome might start to seem
         | _possible_. I expect the lecturer was as baffled by their
         | presentation as we were.
         | 
         | In general, it left the rest of the class believing quite
         | strongly that languages other than Java were impossible to use
         | and generally a bad move. I may have been relatively bitter in
         | the course evaluation by the end.
        
           | Y_Y wrote:
           | Thie irony is palpable. I had the (misfortune) of only being
           | (mis)taught procedural languages by professors who thought
           | computers were big calculators who could nwver be understood,
           | but could be bent to your will by writing more code and maybe
           | by getting a weird grad student to help.
           | 
           | Patterns might appear to the enlighted on the zeroth or first
           | instance, but even the mortal must notice them after several
           | goes. The magic of lisp is that if you notice yourself doing
           | _anything_ more than once your can KISS that repetition
           | goodbye and abstract it out.
           | 
           | Not everything needs to be lifted to functional valhalla of
           | course, but not factoring out e.g. map and filter requires
           | (imho) a wilful ignorance of the sort that no teacher should
           | countenance. I think it's bad professional practise, bad
           | pedagogy, and a bad time overall. I will die on this hill.
        
       ___________________________________________________________________
       (page generated 2025-05-07 23:00 UTC)