how do I approach Prolog - prolog

I have taken up Prolog course in my university. The coursework requires us to write prolog programs. I am able to write simple programs, however I am finding it little hard to digest the complex programs. Any suggestions how to proceed or how do I study this subject ? what is the approach to tackle this kind of course. Any help appreciated.
Thanks.

I write an answer (which is probably just rant) because I struggle with the same problems. If a question gets closed I think the answer still stays?
what is the approach to tackle this kind of course.
Like all courses, you collect all material that your teacher/instructor/professor gives and find the books they recommend. You do the exercises as the way they expect. Do not be clever with your solutions, be clever with finding what your instructor wants to see! Try to find old exams and study them; esp. if you can find graded exams (if you are lucky and "resourceful"... ask older students for help!)
Any suggestions how to proceed or how do I study this subject ?
It is difficult to learn Prolog. I try to learn it at University and the teacher says one thing. Then I pick up a book ("Sterling and Shapiro") and it says another thing. Then I go online and I find yet another two very different things ("Amzi Amazing Adventure" and "Learn Prolog Now!"). There is also "Expert systems in Prolog" (from the Amzi website) and it is still another thing. I come to StackOverflow and I find yet other answers that are NOWHERE in all the texts I have tried to read.
The best place to learn for me is the SWI-Prolog predicate documentation: this is outrageous, don't you think? At least it is consistent....
And then many of the answers here on Stackoverflow I have tried to learn from are talking about things that make no sense (to me), probably because I don't know Prolog, but how to learn it? And almost all questions are homework, and almost all answers to homework are like little lectures that talk about ISO and logic and pureness but no clear answers.
In conclusion: "Sterling and Shapiro" was at least complete, and not too strange; and SWI-Prolog has good predicate documentation and even code examples.
EDIT: nowhere nowhere in all texts do you read about modules, but how do you write big programs without module system? Even the C++ book by Stroustrup explains how to use headers and source files to maintain a bigger program. Again, you go and read the chapter on modules in the SWI-Prolog documentation.
EDIT2: I study "Computer Science" so I maybe know "programming" and "data structures" and "algorithms" and I understand what is "proof tree" and "backtracking" and such things. Prolog is just another language. So why is it so difficult to teach it and learn it? This is an open question. I don't want to ask it on Stackoverflow because it will be closed.
EDIT3: Because you have tagged "clpfd", there is yet another text:
https://www.metalevel.at/prolog
It shows many solutions with clp(fd) that I did not see in any other of the texts I cited. It is useful and consistent but again it is different from all other texts. Maybe if I read and study everything I can find I can give you a real answer.
But do you want to pass the course or learn Prolog? Do you want a good grade and minimum effort? You need to find answers to such questions first!

Related

Looking for a path to learn the math required to understand algorithm books / theory [closed]

Closed. This question is off-topic. It is not currently accepting answers.
Want to improve this question? Update the question so it's on-topic for Stack Overflow.
Closed 9 years ago.
Improve this question
I've taken everything up to pre-calculus in college, but when trying to get through things like the Donald Knuth books, or even things like this link: http://en.wikipedia.org/wiki/Self-balancing_binary_search_tree I wind up looking at math that means absolutely nothing to me. I'm not looking for magic, I don't expect to make sense of this in a week, I'm just looking for a good graduated plan of things to read / explore to get me there. Any pointers are welcome, after 20+ years as a professional programmer, I feel it would be nice to have this under my belt. Thanks in advance to everyone! :-)
I actually recommend taking a discrete mathematics course at your local university. This helped me out tremendously. Until I had this, I did not understand recursion (which is based on mathematical induction.) There are a number of other concepts which you will learn in a good discrete mathematics course which are extremely, extremely helpful (graph theory, asymptotic notation, combinatorics...)
I also recommend taking the class for a grade. I have always noticed that this makes people take the course more seriously, even if it is not in line with a degree path or anything past the grade.
If your local university is good, they will likely have tutoring sessions and office hours available that you can go to in order to ask questions and get clarification. These are really, really valuable and helped me learn things in a deeper manner, and more quickly, than I ever could have on my own.
You may need to take calculus in order to meet the prerequisites, but that is something I would also recommend if you'd like to increase your mathematical literacy. This 'answer' will take at least a semester, and more like two, but I think this is the way to go. It's not an immediate solution, but you will become better at math if you perform well in these two classes (and you have a good university close by.)
Your profile says you are in Dallas. I found this course (with no prerequisites!) for you. The syllabus looks like it covered a lot of good material, and the course met at 5:30 p.m. (good for working people!). If they are offering anything similar next semester, I'd consider it. If you call up the instructor, I'm sure he'd be happy to talk with you about what he knows for summer and fall scheduling.
This path has worked well for me.
Good luck!
You can try this: http://www.amazon.com/Concrete-Mathematics-Foundation-Computer-Science/dp/0201558025
There's a pdf version of this available online, you can easily google it out.
Many of my friends who are great programmers recommended it.
A lot of talented programmers understand algorithms before understanding the maths behind them. Maths are only there to help, they are not here to make you understand everything. You will need to spend more time reading about algorithms and complexity, then you might get a sense of how to evaluate them.
I recommend you to read more books about algorithm complexities.
In your long experience as a professional programmer, there surely are topics and sub-domains that you are most curious about. My advice is: identify those areas and go after them. It might be code-breaking, number theory, recursion, functional programming, computational origami, logical puzzles, crystal structures, graphs, genetic algorithms, splines...
Take your own remark to heart:
but when trying to get through things like the Donald Knuth books, or even
things like this link:...I wind up looking at math that means
absolutely nothing to me
What sort of math fascinates you?
I could say there are lots of intriguing puzzles at Project Euler. After you solve a programming challenge, you have access to a forum in which other folks share their solutions and occasionally refer to some body of knowledge they were drawing on. I love it. But what matters is what you like. Your own interests are the key to your learning.
If math and programming no longer have any appeal--you don't like doing them in your spare time--find something else to get into: acting, foreign languages, travel, French cooking, biking. Who knows, maybe you're burned out.
I'd say get a good book in discrete math and one in combinatorics as well. Here are a few I've liked. The Rosen book is good place to start.
http://www.amazon.com/Course-Combinatorics-J-van-Lint/dp/0521006015
http://www.amazon.com/Discrete-Mathematics-Applications-Kenneth-Rosen/dp/0073229725/ref=sr_1_2?s=books&ie=UTF8&qid=1305304408&sr=1-2
http://www.amazon.com/Introductory-Combinatorics-5th-Richard-Brualdi/dp/0136020402/ref=sr_1_7?s=books&ie=UTF8&qid=1305304434&sr=1-7
In line with what Vincent said, I recommend Algorithms in a Nutshell from O'Reilly (here).
There is a plenty of good video-lectures on Discrete Math, Calculus and Applied Math. Just watch them every evening, make notes and try to solve simple problems. To prepare yourself for Knuth, try "Discrete Mathematics". To understand deeply what is math and how all things in the universe are interconnected (including algorithms), try "Joy of Mathematics".
I was looking for just the same thing. I couldn't afford any of the material suggested here so far so here's a link to a YouTube lecture series on Discrete Mathematics. I wish there was a playlist but unfortunately there is not.
The videos are taken uploaded from http://www.aduni.org who ask for a donation of 25c per video to cover operation costs.

Help In Learning Prolog

I've been learning Prolog for the past two months. I want to increase my knowledge as soon as possible.
Am asking if someone can point me to where i can get Prolog exercises and practice them. I also welcome examination questions as well from universities.
Thanks for your help.
Unfortunately the previous Question mentioned by aschepler has been removed, with its links to Prolog beginner materials. A well-known tutorial is Dennis Merritt's Amzi! Adventure in Prolog. [Disclosure: I previously volunteered as a moderator on the Amzi! Prolog forums.]
For a systematic challenge you might look at Werner Hett's Prologsite 99 problem set:
This is a remake of the P-99: Ninety-Nine Prolog Problems collection that I assembled over several years of teaching at the University of Applied Sciences (Berner Fachhochschule) at Biel-Bienne, Switzerland. The collection is structured into seven sections. I have renumbered the problems in order to get more freedom to rearrange things within the sections.
It might be the ladder to rapidly increase your knowledge after a couple of months of introductory study.
This really helped me, just follow the examples and run them one by one.
Hope it helps.

Becoming operational in Prolog quickly

My company has a project running in Prolog and I want to clarify few things about how to go about learning it. I know Prolog is different. It should not be learnt just like any other language.
Having said that, and considering the fact that I did not lay my hands on any Prolog book yet, Is there any book or online resource, where I can learn Prolog the way how we learn C/C++? What I mean is , just to be operational in C/C++, you just need to know the structure of the program, like main { } , loops, conditions, branches, and few functions that you can use to start writing basic programs in C/C++.
Just this way can I learn Prolog and is there any book that just gives me an idea how to Program in Prolog? (basics, loops, how to implement conditions, program structure, what's predicate? how to use it? how to define it? and so on...).
If you're after a single book, I can highly recommend "The Art of Prolog":
Coming to Prolog from something like C/C++ isn't just a matter of learning a programming language. It's a wholly different way of thinking about programming.
Prolog is about asking the computer questions (or 'queries' if you like). Computation is almost a side-effect of the computer trying to answer your question. There is no meaningful equivalent to loops or conditionals because a prolog programmer wouldn't think in those terms.
A good Prolog program looks like a description of the problem that you're trying to solve decomposed into recursive cases and subproblems rather than lists of instructions organised into functions or classes.
The best way to learn Prolog is to set aside all your previous programming experience. Actually thinking about C and C++ will make Prolog harder to learn and use. Try to adopt a beginner's mind and maybe an approach more like an algebraist than a programmer.
As a supplement to the Prolog tutorials and textbooks mentioned in the other answers, I would suggest having a quick look at this short document:
Prolog for Imperative Programmers
I think it's part of what you're looking for. It won't teach you Prolog, but it will help bridge the gap to understanding Prolog. It describes the basics of Prolog using terminology that experienced non-Prolog programmers would understand. For example, it shows you control structures in Prolog, i.e. sequence, selection and repetition. It does assume that you've already started learning Prolog, though.
It's good if you want to understand something new in terms of something you already know. However, armed with this knowledge/understanding, there is a risk that you could end up writing C code in Prolog syntax. Good luck!
What's wrong with Learn Prolog Now, which is usually the top recommendation each time this kind of question gets asked?
It may not give you exactly the terminology you want -- I believe it doesn't even mention "predicate" (uses "Facts, Rules, and Queries" instead) or "loops" (it just shows how to use recursion instead) -- but getting the terminology right once the concepts are clear should be simple, fast, and easy, and "Learn Prolog Now" does seem to do a good job about making the concepts clear.

Improve algorithmic thinking [closed]

Closed. This question is opinion-based. It is not currently accepting answers.
Want to improve this question? Update the question so it can be answered with facts and citations by editing this post.
Closed 7 years ago.
Improve this question
I was thinking about ways to improve my ability to find algorithmic solutions to a problem.I have thought of solving math problems from various math sectors such as discrete mathematics or linear algebra.After "googling" a bit I have read an article that claimed the need of learning game programming in order to achieve this and it seems logical to me.
Do you have/had the same concerns as me or do you have any ideas on this?I am looking forward to hear them.
Thank you all, in advance.
P.S.1:I want to say that I already know about programming and how to program(although I am at amateur level:-) ) and I just want to improve at the specific issue, NOT to start learning it
P.S.2:I think that its a useful topic for future reference so I checked the community wiki box.
Solve problems on a daily basis. Watch traffic lights and ask yourself, "How can these be synced to optimize the flow of traffic? Or to optimize the flow of pedestrians? What is the best solution for both?". Look at elevators and ask yourself "Why should these elevators use different rules than the elevators in that other building I visited yesterday? How is it actually implemented? How can it be improved?".
Try to see a problem everywhere, even if it is solved already. Reflect on the solution. Ask yourself why your own superior solution probably isn't as good as the one you can see - what are you missing?
And so on. Every day. All of the time.
The idea is that almost everything can be viewed as an algorithm (a goal that has some kind of meaning to somebody, and a method with which to achieve it). Try to have that in mind next time you watch a gameshow on TV, or when you read the news coverage of the latest bank robbery. Ask yourself "What is the goal?", "Whose goal is it?" and "What is the method?".
It can easily be mistaken for critical thinking but is more about questioning your own solutions, rather than the solutions you try to understand and improve.
First of all, and most important: practice. Think of solutions to everything everytime. It doesn't have to be on your computer, programming. All algorithms will do great. Like this: when you used to trade cards, how did you compare your deck and your friend's to determine the best way for both of you to trade? How can you define how many trades can you do to do the maximum and yet don't get any repeated card?
Use problem databases and online judges like this site, http://uva.onlinejudge.org/index.php, that has hundreds of problems concerning general algorithms. And you don't need to be an expert programmer at all to solve any of them. What you need is a good ability with logic and math. There, you can find problems from the simplest ones to the most challenging. Most of them come from Programming Marathons.
You can, then, implement them in C, C++, Java or Pascal and submit them to the online judge. If you have a good algorithm, it will be accepted. Else, the judge will say your algorithm gave the wrong answer to the problem, or it took too long to solve.
Reading about algorithms helps, but don't waste too much time on it... Reading won't help as much as trying to solve the problems by yourself. Maybe you can read the problem, try to figure out a solution for yourself, compare with the solution proposed by the source and see what you missed. Don't try to memorize them. If you have the concept well learned, you can implement it anywhere. Understanding is the hardest part for most of them.
Polya's "How To Solve It" is a great book for thinking about how to solve mathematical problems and do proofs, and I'd recommend it for anyone who does problem solving.
But! It doesn't really address the excitement that happens when the real world provides input to your system, via channel noise, user wackiness, other programs grabbing resources, etc. For that it is worth looking at algorithms that get applied to real-world input (obligatory and deserved nod to Knuth's collection), and systems which are fairly robust in the face of same (TCP, kernel internals). Part of coming up with good algorithmic solutions is to know what already exists.
And alongside reading all that, of course practice practice practice.
You should check out Mathematics and Plausible Reasoning by G. Polya. It is a rare math book, which actually deals with the thought process involved in making mathematical discoveries. I think it is the same thought process that is involved in coming up with algorithms.
The saying "practice makes perfect" definitely applies. I'm tutoring a friend of mine in programming, and I remind him that "if you don't know how to ride a bike, you could read every book about it but it doesn't mean you'll be better than Lance Armstrong tomorrow - you have to practice".
In your case, how about trying the problems in Project Euler? http://projecteuler.net
There are a ton of problems there, and for each one you could practice at developing an algorithm. Once you get a good-enough implementation, you can access other people's solutions (for a particular problem) and see how others have done it. Don't think of it as math problems, but rather as problems in creating algorithms for solving math problems.
In university, I actually took a class in algorithm design and analysis, and there is definitely a lot of theory behind it. You may hear people talking about "big-O" complexity and stuff like that - there are quite a lot of different properties about algorithms themselves which can lead to greater understanding of what constitutes a "good" algorithm. You can study quite a bit in this regard as well for the long-term.
Check some online judges, TopCoder (algorithm tutorials). Take some algorithms book (CLRS, Skiena) and do harder exercises. Practice much.
I would suggest this path for you :
1.First learn elementary parts of a language.
2.Then learn about some basic maths.
3.Move to topcoder div2 easy problems.Usually if you cannot score 250 pts. in any given day,then it means you need a lot of practise,keep practising.
4.Now's the time to learn some tools of a programmer,take a good book like Algorithm Design Manual by Steven Skienna and learn about dynamic programming and greedy approach.
5.Now move to marathons,don't be discouraged if you cannot solve it quickly.Improvement will not happen overnight,you will have to patiently keep on working hard.
6.Continue step 5 from now on and you will be a better programmer.
Learning about game programming will probably lead you to good algorithms for game programming, but not necessarily to better algorithms in general.
It's a good start, but I think that the best way to learn and apply algorithmic knowledge is
Learn about good algorithms that currently exist for your area of interest
Expand your knowledge by viewing other areas; for example, what kinds of algorithms are
required when working on genetic analysis? What's the best approach for determining
run-off potential as it relates to flooding?
Read about problems in other domains and attempt to use the algorithms that you're
familiar with to see if they fit. If they don't try to break the problem down and see if
you can come up with your own algorithm.
A few more books worth reading (in no particular order):
Aha! Insight (Martin Gardner)
Any of the Programming Pearls books (Jon Bentley)
Concrete Mathematics (Graham, Knuth, and Patashnik)
A Mathematical Theory of Communication (Claude Shannon)
Of course, most of those are just samples -- other books and papers by the same authors are usually quite good as well (e.g. Shannon wrote a lot that's well worth reading, and far too few people give it the attention it deserves).
Read SICP / Structure and Interpretation of Computer Programs and work all the problems; then read The Art of Computer Programming (all volumes), working all the exercises as you go; then work through all the problems at Project Euler.
If you aren't damned good at algorithms after that, there is probably no hope for you. LOL!
P.S. SICP is available freely online, but you have to buy AoCP (get the international, not-for-release-in-north-america edition used for 30 USD). And I haven't done this yet myself (I'm trying when I have free time).
I can recommend the book "Introductory Logic and Sets for Computer Scientists" by Nimal Nissanke (Addison Wesley). The focus is on set theory, predicate logic etc. Basically the maths of solving problems in code if you will. Good stuff and not too difficult to work through.
Good luck...Kevin
Great
how about trying the problems in Project Euler? http://projecteuler.net
There are a ton of problems there, and for each one you could practice at developing an algorithm. Once you get a good-enough implementation, you can access other people's solutions (for a particular problem) and see how others have done it. Don't think of it as math problems, but rather as problems in creating algorithms for solving math problems
Ok, so to sum up the suggestions:
The most effective way to improve this ability is to solve problem as frequently as possible.Either real world problems(such as the elevators "algorithm" which is already suggested) or exercises from books like CLRS(great, I already own it :-)).But I didn't see comments about maths and I don't know what to suppose(if you agree or not).:-s
The links were great.I will definitely use them.I also think that it will be a good exercise to solve problems from national/international informatics contests or to read the way a mathematician proves a theorem.
Thank you all again.Feel free to suggest more, although I am already satisfied with the solutions mentioned.

Having a bit of trouble with self-learning from Cormen et al's algo book

I started reading Intro to Algorithms by Cormen et al like 3 weeks ago on my free time. I finished the second chapter and have been trying out the exercises for quite a while. I find them a bit difficult.
Is this normal? Should I finish all the exercises before moving on? Or is it alright if I solve the ones I can and move on to the next chapters, possibly coming back to the exercises I can't figure out right now?
If anyone out there has had experience with this book, can you tell me how it was for you? I'm a bit discouraged on not being able to solve quite a few of the exercises here.
That book was hard for me too. We used it at the university I attended and I often had to refer to other sources to get simpler explanations when I found CLRS a little over my head. Once I got the Wikipedia explanation straight in my head, and a code sample working (which CLRS often lacks), I found that I was able to go back to the text and make sense of it.
Don't worry about doing all of the exercises. Even the super-elite MIT students don't have to do them all. Do what you can do and move on. If you need a concept in the next chapter that you had glossed over, it will still be there for you to backtrack to.
MIT OpenCourseWare has also made available the old lectures for Introduction to Algorithms (SMA 5503).
Good for you for diving into CLRS by yourself. You're a braver man than me. I used the book for a grad algorithms course I took last semester, and I had a hard time just finishing the problem sets assigned for the course. Completing all of the exercises would be a truly Herculean effort.
I'd recommend tackling the chapters that interest you most and those that you don't find to difficult. The beginning of the book, if I remember correctly, is one of the harder parts, diving into the mathematical background of a lot of different areas of algorithms. Chapter 5 is especially difficult unless you know a fair bit of probability theory. Also, starred sections and problems are significantly more challenging than the surrounding material (like 21.4, which contains material our professor confessed to being unable to prove in class). Finally at the end of the book, there is just a survey of miscellaneous topics; you can just look at those that interest you, since there are entire books written about each of those topics if you want to learn more about them.
I hope this helps, and most importantly, don't get too discouraged! This is the seminal book on algorithms for a reason.
It's a difficult book, used by one of the pre-eminent technical universities in the world. It's no surprise that it's challenging. There are a LOT of exercises of varying difficulty. It's a noble goal to attempt all of them.
Aren't the course materials on-line? It'd be interesting to see if students taking the course for credit do all the exercises.
I wouldn't be discouraged. Keep plugging, even if you have to pass on some of the exercises. There's nothing saying that you have to master it in one pass, either. Go through, take in what you can, and re-do if necessary. You might find that the extra context helps.
The lectures are available on iTunes if you find that helps.
The important thing is to set a deadline and make steady progress. Good luck.
The problem with not doing all the problems is that when you are self-studying, you really don't have a good gage for how much you should be able to answer.
You can look at the course assignments online, I would recommend that for figuring out problem sets to get done.
I am learning Algorithms on my own from the CLRS book in 2020. Regardless of what people tell you about solutions manuals in general, it is advisable to get "good" solutions manuals if you are self studying with the book.
The two sets of solutions I recommend are (1) The official instructor manual and (2) solutions by Rutger's university students Michelle Bodnar and Andrew Lohr. When one of those solutions is unclear, I simply refer to the other one. If you get stuck at a problem, then give yourself a few minutes to solve it. If you don't get the answer, then use the solutions manuals. You can always test yourself on the problems from other text books or leetcode to see how much you can do on your own vs just following a solutions manual.
I won't post the solutions manuals here. I suggest that you search for them online. The Rutgers one is easily available and is legal. The official one is restricted to instructors only and is hard to get. You might be able to pay obscure online sellers/hackers to get the official one for you. Use a preloaded visa or master card gift card to make that purchase. Make sure that the card is accepted in the sellers country.
Chapter 2 was doable because I used Youtube to understand algorithms and time complexity when it was not clearly explained in the CLRS book, which is quite often. The solutions manuals also helped a little bit.
Chapter 3 is hard and I don't know if I will be able to get past this one. I might have to switch to another book, perhaps the one by Tamassia. I had studied elementary algebra, set theory, functions, probability, mathematical series and calculus a few years ago. But, I remember only a few of those things. So, it is difficult to understand Chapter 3 and move ahead.
In general, it is a comprehensive and rigorous book. However, it is bad because of these:
One-based array indexing (instead of the usual 0-based) - so everytime you translate the algorithm present in the book into code you have to either +1 or -1 and / or use < instead of <= or the other way around and so on.
Bad variable naming in pseudocode - instead of lo, hi or left, right you get p and q.
The fact that is is very rigorous may get you confused in the little details and usually you can miss the overall idea of an algorithm.
It is a famous book because many scientific papers refer to this book in their references.
Otherwise, it is ok.

Resources