4 - Programming as Search (Part 2) [ID:21827]
50 von 113 angezeigt

Okay, multiplication, exponentiation, and so on. You can do all kinds of things, but

you can do more. There's no reason in Prolog to use the queries as you

thought you wanted to use them in kind of the input-output behavior. Remember,

we thought input-input-output. So we would make a query which had the variable

there. What happens if we put the variable here? As, for instance, in S of

0, X, S of S of S of 0. Remember, this means is there an X such that 1 plus X

is 3? And you would probably say, natural intelligences that you are, yes, it's 2.

3 minus 1, if you were wondering. And Prolog does the same. Try it out, you'll

get 2, and it says, yes, true. You can do things like compute Fibonacci

numbers. When you can do multiplication and addition, that's always the next

thing you do. Fibonacci numbers, you just write down what you know about them.

Fib of 0 is 0, right? Fib of the successor of 0 is the successor of 0,

which is just Fib of 1 is 1. You start with 0, 1. And the Fib of X plus 2 is

actually the result of Fib of X minus 1 and Fib of X minus, yeah, Fib of X plus 2

is Fib of X plus 1 plus Fib of X of 2. Okay? Just write down in a very natural

way what you know about these things. And then you know that Fibonacci numbers

grow exponentially, so they get big very soon. And even something like 24,

which is, what is it, Fib of 4 or 5 or something like this, so very early. You

don't even want to see all these Fibonacci, these S's, 24 of them. And if

you go to Fib of 6 or 5, whatever, then you would get 120 or something like

this and then debugging becomes really, really nasty. Okay? So even though, yes,

you can do everything with S's and 0's, you don't want to do that. Therefore,

Prolog can actually do real numbers and integers as well. And we use the

predicate is, which is written in fix, is kind of the poor man's alternative to an

equality sign. And here is only succeeds automagically if the right-hand side is

something that does not contain any variables. So if Fib of xy is

only called for numbers, right for 17 and 17, X being 17, then D is x ñ 1

can be computed because 17 ñ 1 does not contain any variables. So D is

16 here, x minus 2, that's 15, and then we can compute fib of D and fib of E, and then

we can say y is z plus w. Okay, so you can actually do the same thing with honest to

goodness numbers and it looks almost the same. We're losing something here. We're losing

the ability to use fib backwards here. Remember we're kind of quote unquote using addition

backwards to get subtraction? We can't use this backwards because D and x would be exchanged

and x and the right hand side would have variables and that doesn't compute. This here does.

If you're prepared to write 120 s's and 1 0 and another 240 brackets, then you can say

fib of x and then this big ugly beast and then it says 6 or s of s of s of s of s of

s of 0. Okay, so here we can, this we can use backwards, this one we can't, but of course

if you really want to, if you really need numbers, please by all means use this, down

to s of 0. They're just theoretical tools to understand what's going on. Questions?

Okay very important, we add lists. In this case, lists of any terms and the first thing

we do is we, how do we write down lists? Well, very easy, square brackets and then a sequence

of commas separated. Objects, that's one way of doing it. The other way, which is even

more important, is the kind of first rest notation where you say the head of the list,

the first element is f and r is another list, that's the rest list. So if we match, if we

match 1, 2, 3 against first rest, then we can, they're the same if f is 1 and rest is

2, 3. Okay? And if we match the list only with 1, then f is again 1 and r is, have you

programmed with lists? A list is all the elements and then the empty list, right? Okay, lists

are very, very important things. And now you can do things like make the member predicate

to find out whether x is a member of a list. We have two cases, either x is the first element,

by the way it's very good practice and you should always do it. If you're not intending

Teil eines Kapitels:
Logic Programming

Zugänglich über

Offener Zugang

Dauer

00:16:41 Min

Aufnahmedatum

2020-10-26

Hochgeladen am

2020-10-26 09:06:54

Sprache

en-US

Some more examples and lists in PROLOG.

Einbetten
Wordpress FAU Plugin
iFrame
Teilen