andreagus 8 anni fa
parent
commit
0967514272
6 ha cambiato i file con 207 aggiunte e 3 eliminazioni
  1. 17 0
      haskell/haskell4.hs
  2. 68 0
      haskell/haskell5.hs
  3. 48 2
      haskell/haskell6.hs
  4. 33 0
      haskell/haskell7.hs
  5. 27 0
      scheme/scheme4.rkt
  6. 14 1
      scheme/scheme5.rkt

+ 17 - 0
haskell/haskell4.hs

@@ -1,3 +1,4 @@
+<<<<<<< HEAD
 lenght :: [a] -> Integer
 lenght [] = 0
 lenght (x:xs) = 1 + lenght xs
@@ -63,3 +64,19 @@ transpose [] = []
 transpose ls = let hs = map head ls
                    ts = filter (not . null) $ map tail ls
                in hs : transpose ts
+=======
+module Haskell4 where
+
+import Control.Applicative
+import Control.Monad
+
+exmon :: (Monad m, Num r) => m r -> m r -> m r 
+exmon m1 m2 = do x <- m1
+                 y <- m2
+                 return $ x-y
+
+main = exmon (do putStr "?> "
+                 x <- getLine;
+                 return (read x :: Int))
+             (return 10)    
+>>>>>>> 66d23fcd2717ce0805f5442c6cf4888114f9b38c

+ 68 - 0
haskell/haskell5.hs

@@ -1,3 +1,4 @@
+<<<<<<< HEAD
 import Control.Applicative
 import Control.Monad
 import Control.Monad.State
@@ -133,3 +134,70 @@ popM = do
     x:xs <- get
     put xs
     return x
+=======
+module EsHs4 where
+
+import Control.Applicative
+import Control.Monad
+
+--Logger
+type Log = [String]
+newtype Logger a = Logger { runLogger :: (a, Log) }
+
+--Define an instance of Show for Logger
+instance Show a => Show (Logger a) where
+        show (Logger a) = show a
+
+--Define an instance of Functor for logger
+class Functor f where
+        fmap :: (a -> b) -> f a -> f b
+
+loggerMap :: ( a -> b) -> (Logger a) -> (Logger b)
+loggerMap f lg =
+        let (v, l) = runLogger lg
+            n = f v
+        in Logger (n, l)
+
+instance Functor Logger where
+        fmap = loggerMap
+
+loggerApp :: Logger (a -> b) -> Logger a -> Logger b
+loggerApp lf lg =
+        let (f, s) runLogger lf
+            nl = loggerMap f lg
+            (n, l) = runLogger nl
+        in Logger (n, l ++ s)
+
+
+instance Applicative Logger where
+        pure a = Logger (a, [])
+        (<*>) = loggerApp
+
+instance Monad Logger where
+        m >>= f = let(a, w) = runLogger m
+                     n = f a
+                     (b, x) = runLogger n
+                  in Logger (b, w ++ x)
+
+--Define a function that takes a number, add one and log the op:
+
+logPlusOne :: (Num a) => a -> Logger a
+logPlusOne a = Logger (a+1, ["added one"])
+
+logMultiplyTwo :: (Num a) => a -> Logger a
+logMultiplyTwo = Logger (a*2, ["Multiplied by two"])
+
+--Define logOps
+
+logOps :: (Num a) => Logger a -> Logger a
+logOps lg = do
+        v <- lg
+        p1 <- logPlusOne v
+        m2 <- logMultiplyTwo p1
+        return m2
+
+-- Define a record function to record things in the log
+record :: String -> Logger()
+record s = Logger ((), [s])
+
+>>>>>>> 66d23fcd2717ce0805f5442c6cf4888114f9b38c

+ 48 - 2
haskell/haskell6.hs

@@ -1,5 +1,3 @@
-
-
 data Bilist a = Bilist [a] [a] deriving (Show, Eq)
 
 bilist_ref (Bilist l r) pos = (l !! pos, r !! pos )
@@ -46,3 +44,51 @@ checklist lst (Lt size ltf) =
 
 instance Functor Lt where
     fmap f (Lt k lst) = Lt k $ map (\x -> map f x) lst
+
+module Haskell6 where
+
+import Control.Monad.ST
+--State Monad
+
+----Stack
+type Stack = [Int]
+
+--define the pop function
+pop :: Stack -> (Int,Stack)
+pop (x:xs) = (x,xs)
+
+--define the push function
+push :: Int -> Stack -> ((),Stack)
+push a xs = ((), a:xs)
+
+--Define Stackmanipulator
+stackManip :: Stack -> (Int, Stack)
+stackManip stack = let
+        (a, newstack1) = pop stack
+        (b, newstack2) = pop newstack1
+        ((), newstack3) = push 100 newstack2
+        (c, newstack4) = pop newstack3
+     in pop newstack4
+
+--Define the pop function using the state monad
+popM :: State Stack Int
+popM = do
+        x:xs <- get
+        put xs
+        return x
+
+--Define the push function using the state monad
+pushM :: Int -> State Stack ()
+push a = do
+        xs <- get
+        put (a:xs)
+        return ()
+
+--Redefine the stackManip using the monadic functions
+stackManipM :: Stack State Int
+stackManipM = do
+        popM
+        popM
+        pushM 100
+        popM
+        popM

+ 33 - 0
haskell/haskell7.hs

@@ -1,3 +1,4 @@
+<<<<<<< HEAD
 import Control.Monad.State
 type Stack = [Int]
 
@@ -47,3 +48,35 @@ stackStuff = do
             pushM 3
             pushM 8
             get
+=======
+--Define the Bilist type
+data Bilist a = Bilist [a] [a] deriving (Show, Eq)
+
+bilist_ref (Bilist l r) pos = (l !! pos, r !! pos)
+
+--Define oddeven
+
+oddeven :: [a] -> Bilist a
+oddeven l = oddevenh l [] []
+
+oddevenh :: [a] -> [a] -> [a] -> Bilist a
+oddevenh [] ev od = Bilist ev od
+oddevenh (x:xs) ev od = oddevenh xs od (ev++[x])
+
+--Define the inverse of oddeven
+
+inv_oddeven :: Bilist a -> [a]
+inv_oddeven (Bilist [] []) = []
+inv_oddeven (Bilist (l:ls) (r:rs)) = [l] ++ [r] ++ (inv_oddeven (Bilist ls rs))
+
+inv_oddeven2 :: Bilist a -> [a]
+inv_oddeven2 (Bilist l r) = foldl (++) [] $ map (\(x,y) -> [x,y]) $ zip l r
+
+bilist_max (Bilist (l:ls) (r:rs)) = bilist_maxh (Bilist ls rs) 2 (l+r) 1
+
+bilist_maxh (Bilist (l:ls) (r:rs)) pos curmax maxpos |
+        l+r > curmax = bilist_maxh (Bilist ls rs) (pos+1) (l+r) pos
+bilist_maxh (Bilist (l:ls) (r:rs)) pos curmax maxpos =
+        bilist_maxh (Bilist ls rs) (pos+1) curmax maxpos
+bilist_maxh (Bilist [] []) pos curmax maxpos = maxpos
+>>>>>>> 66d23fcd2717ce0805f5442c6cf4888114f9b38c

+ 27 - 0
scheme/scheme4.rkt

@@ -37,3 +37,30 @@
 (take 10 (iterate (lambda (x) (+ x 1)) 5))
 (define succession (iterate (lambda (x) (+ x 1)) 3))
 (print succession)
+(newline)
+(car (force (cdr (force (cdr (force succession))))))
+
+(define lista '(1 2))
+(print lista)
+(newline)
+(car lista)
+(abs (car (cdr lista)))
+
+(define (re-map f L cond?)
+  (let loop ((res '())
+             (cur L))
+    (if (null? cur)
+        res
+        (let* ((k #f)
+               (v (call/cc
+                   (lambda (cont)
+                     (set! k cont)
+                     (f (car cur))))))
+          (if (cond? v)
+              (cons k v)
+              (loop (append res (list v))
+                    (cdr cur)))))))
+
+;(define V (re-map (lambda (x) (+ x 1)) '(0 1 -4 3 -6 5) negative?))
+
+(define mini-lista (cons 1 2))

+ 14 - 1
scheme/scheme5.rkt

@@ -102,4 +102,17 @@
     (if (< i n)
            (loop (cons (if (= i k) 1 0) r)
                  (+ i 1))
-    r)))
+    r)))
+
+(define (infinity)
+  (+ 1 (infinity)))
+
+(define (fst x y) x)
+
+(define lazy-infinity (delay(infinity)))
+;(fst 3 (infinity))
+(force (fst 3 lazy-infinity))
+(fst 3 lazy-infinity)
+(define res (delay (+ 2 5)))
+(force res)
+(force (delay (fst 3 lazy-infinity)))