Add miscellaneous classwork stuff

This commit is contained in:
Citlali del Rey 2024-04-30 13:56:33 -07:00
parent 79550d0612
commit 10c0d9d5dd
Signed by: nullobsi
GPG Key ID: 933A1F44222C2634
7 changed files with 144 additions and 1 deletions

11
Week12/knowledge.pl Normal file
View File

@ -0,0 +1,11 @@
parent(john, mary).
parent(john, tom).
parent(mary, lisa).
parent(tom, jerry).
male(john).
male(tom).
male(jerry).
female(mary).
female(lisa).

18
Week12/lecture1.pl Normal file
View File

@ -0,0 +1,18 @@
eats(elephant, shrub).
eats(tiger, deer).
eats(lion, zebra).
eats(panda, bamboo).
eats(cow, grass).
plant(shrub).
plant(bamboo).
plant(grass).
meat(deer).
meat(zebra).
herbivore(X) :- eats(X, Y), plant(Y).
carnivore(X) :- eats(X, Y), meat(Y).

6
Week13/lecture.pl Normal file
View File

@ -0,0 +1,6 @@
take(0, _, []).
take(_, [], []).
take(Count, [F|Rest], List) :- Next is Count-1, List = [F | Part], take(Next, Rest, Part).
member(X, [C|Rest]) :- X = C; member(X, Rest).

View File

@ -6,4 +6,3 @@ fibs :: [Int]
-- F_n+2 = 1 + sum(F_1 .. F_n)
fibs = 1 : scanl (+) 1 fibs

96
Week9/lecture1.hs Normal file
View File

@ -0,0 +1,96 @@
import System.IO
import Data.List( delete )
-- A definition for zipWith:
zipWith' :: (a->b->c) -> [a] -> [b] -> [c]
zipWith' f (x:xs) (y:ys) = f x y : zipWith' (f) xs ys
zipWith' _ _ _ = []
-- Solution for fibs:
-- 0 1 2 3 5 8
-- 1 2 3 5 8
-- ^ one off, so we zipWith (+) with the list and the tail half of the
-- list.
fibs = 0 : 1 : zipWith (+) fibs (tail fibs)
-- An example of an impure IO function.
act :: IO (Char, Char)
act = do x <- getChar
getChar
y <- getChar
return (x,y)
-- How to get a line
getLine' :: IO String
getLine' = do x <- getChar
if x == '\n' then
return []
else
do xs <- getLine'
return (x:xs)
-- How to put a String
putStr' :: String -> IO ()
putStr' [] = return ()
putStr' (x:xs) = do putChar x
putStr' xs
putStrLn' :: String -> IO ()
putStrLn' xs = do putStr' xs
putChar '\n'
-- Now, an example of an interactive program
strlen :: IO ()
strlen = do putStr "Enter a string: "
xs <- getLine
putStr "The string has "
putStr (show (length xs))
putStrLn " characters."
-- how to do a hangman game?
getCh :: IO Char
getCh = do hSetEcho stdin False
x <- getChar
hSetEcho stdin True
return x
sgetLine :: IO String
sgetLine = do x <- getCh
if x == '\n' then
do putChar x
return []
else
do putChar '-'
xs <- sgetLine
return (x:xs)
replace xs i r =
match :: String -> String -> String
match _ [] = []
match word@(w:ws) (x:xs) = if w == x then '!' : match ws xs
else if elem x word then
'?' : match (delete x word) xs
else '-' : match word xs
--match word guess = [if g == w then '!' else if elem g word then '?' else '-' | (w,g) <- zip word guess]
play :: String -> Int -> IO ()
play word 0 = do putStr "Out of guesses... The word was "
putStrLn word
play word guesses = do putStr "? "
guess <- getLine
if guess == word then
putStrLn "You got it!"
else
do putStrLn (match word guess)
play word (guesses - 1)
hangman :: IO ()
hangman = do putStr "Think of a word: "
word <- sgetLine
putStrLn "Try to guess it:"
play word 6

8
Week9/week9.hs Normal file
View File

@ -0,0 +1,8 @@
-- Backtracking
minCost :: [Int] -> Int
minCost [] = 0
minCost (_:[]) = 0
minCost (x:y:xs) = min x y + minCost xs

5
haskell/tree.hs Normal file
View File

@ -0,0 +1,5 @@
data Tree a = Leaf | Branch a (Tree a) (Tree a)
leftside :: Tree a -> a
leftside (Branch x Leaf _) = x
leftside (Branch _ a _) = leftside a