2
0
Fork 0
mirror of https://github.com/MartinThoma/LaTeX-examples.git synced 2025-04-25 22:38:04 +02:00

Abschnitt über Arithmetik in Prolog hinzugefügt; misc

This commit is contained in:
Martin Thoma 2014-03-23 19:28:44 +01:00
parent 82dd24d55b
commit 80e8df59d6
20 changed files with 194 additions and 27 deletions

View file

@ -8,5 +8,7 @@ hIndex l = helper (reverse (sort l)) 0
| otherwise = acc
-- Alternativ
hindex1 = length . takeWhile id . zipWith (<=) [1..] . reverse . sort
hindex2 = length . takeWhile (\(i, n) -> n >= i) . zip [1..] . reverse . sort
hindex1 = length . takeWhile id .
zipWith (<=) [1..] . reverse . sort
hindex2 = length . takeWhile (\(i, n) -> n >= i) .
zip [1..] . reverse . sort

View file

@ -12,4 +12,5 @@ intersectAll (l:ls) = (foldr intersect l) ls
intersectAll [] = undefined
multiples n = [n*k | k <- [1..]]
commonMultiples a b c = intersectAll [ multiples n | n <- [a,b,c]]
commonMultiples a b c =
intersectAll [ multiples n | n <- [a,b,c]]

View file

@ -0,0 +1,6 @@
triples :: [(Integer, Integer, Integer)]
triples = [(x,y,z) | z <-[1..],
x <- [1..z],
y <- [1..z],
z^2 == x^2 + y^2
]

View file

@ -0,0 +1,10 @@
?- X is 3^2.
X = 9.
?- Y is X*X.
ERROR: is/2: Arguments are not sufficiently
instantiated
?- X is X+1.
ERROR: is/2: Arguments are not sufficiently
instantiated

View file

@ -0,0 +1,2 @@
?- X is 5-2*5.
X = -5.

View file

@ -0,0 +1,5 @@
even(0).
even(X) :- X>0, X1 is X-1, odd(X1).
odd(1).
odd(X) :- X>1, X1 is X-1, even(X1).

View file

@ -0,0 +1,6 @@
fib(0,0).
fib(1,1).
fib(X,Y) :- X>1,
X1 is X-1, X2 is X-2,
fib(X1,Y1), fib(X2,Y2),
Y is Y1+Y2.

View file

@ -0,0 +1,2 @@
append([],L,L).
append([X|R],L,[X|T]) :- append(R,L,T).

View file

@ -0,0 +1,3 @@
?- [X|Y] = [1,2,3,4,5].
X = 1,
Y = [2, 3, 4, 5].

View file

@ -0,0 +1,2 @@
member(X,[X|R]).
member(X,[Y|R]) :- member(X,R).