Skip to content

Instantly share code, notes, and snippets.

@bendisposto
Created January 10, 2014 06:28
Show Gist options
  • Save bendisposto/8347796 to your computer and use it in GitHub Desktop.
Save bendisposto/8347796 to your computer and use it in GitHub Desktop.
summe [] = 0
summe (x:xs) = x + summe xs
prod [x] = x
prod (x:xs) = x * prod xs
factorial 0 = 1
factorial n = n * factorial (n-1)
foo 0 = 1
foo n = 7
foo 2 = 6
first (a,_) = a
second (_,b) = b
-- max' :: (Ord a) => a -> a -> a
max' a b
| a > b = a
| otherwise = b
a `myCompare` b
| a > b = GT
| a == b = EQ
| otherwise = LT
fib 0 = 1
fib 1 = 1
fib n = fib (n-1) + fib (n-2)
echo x = do n <- getLine
putStrLn (x ++" " ++ n)
return n
data Shape = Rectangle Float Float Float Float |
Circle Float Float Float
surface (Circle _ _ r) = pi * r ^ 2
surface (Rectangle x1 y1 x2 y2) = (x2-x1) * (y2-y1)
data Vielleicht a = Ein a | Nix deriving Show
instance Functor Vielleicht where
fmap _ Nix = Nix
fmap f (Ein a) = Ein (f a)
mydiv :: Int -> Int -> Maybe Int
mydiv a b = if b == 0
then Nothing
else Just (a `div` b)
data Vector a = Vector a a
instance (Eq a) => Eq (Vector a) where
(Vector x1 y1) == (Vector x2 y2) = x1==x2 && y1 == y2
data Liste a = Leer | Paar a (Liste a) deriving Show
instance Functor Liste where
fmap f Leer = Leer
fmap f (Paar x xs) = Paar (f x) (fmap f xs)
(ns repl.core
(:require [schema.core :as s]))
(def AName [s/Keyword])
(def APlace {AName s/Number})
(def ATransition AName)
(def ANet {:places #{APlace} :transitions #{ATransition}})
(s/defn add-place :- ANet
"Add a place to a net, returns a new net"
[net :- ANet, p :- APlace]
(update-in net [:places] #(conj % p)))
(s/defn bad-add-place :- ANet
"Add a place to a net, returns a new net"
[net :- ANet, p :- APlace]
{:places (conj (:places net) p)})
(comment
(s/validate AName :a)
(s/validate AName [:a :b])
(s/validate ANet {})
(s/validate ANet {:places #{} :transitions #{}})
(def n1 {:places #{{[:n1 :p1] 2} {[:n1 :p2] 1}}, :transitions #{}})
(add-place n1 "")
(s/with-fn-validation (add-place n1 ""))
;; (s/defn ^:always-validate add-place :- ANet
;; [net :- ANet, p :- APlace]
;; (update-in net [:places] #(conj % p)))
)
(ns repl.core-test
(:require [clojure.test :refer :all]
[repl.core :refer :all]
[schema.test]))
(deftest adding-test1
(testing "I succeed."
(add-place {:places #{{[:n1 :p1] 2} {[:n1 :p2] 1}},
:transitions #{}} {[:n1 :p3] 21} )))
(deftest adding-test2
(testing "I fail."
(add-place {:places #{{[:n1 :p1] 2} {[:n1 :p2] 1}},
:transitions #{}} {:p 21} )))
(deftest adding-test3
(testing "I fail."
(bad-add-place {:places #{{[:N1 :p1] 2} {[:n1 :p2] 1}},
:transitions #{}} {[:n1 :p3] 21} )))
(run-tests)
(use-fixtures :once schema.test/validate-schemas)
(defproject repl "0.1.0-SNAPSHOT"
:description "FIXME: write description"
:url "http://example.com/FIXME"
:license {:name "Eclipse Public License"
:url "http://www.eclipse.org/legal/epl-v10.html"}
:dependencies [[org.clojure/clojure "1.5.1"]
[prismatic/schema "0.1.10"]
[org.clojure/core.typed "0.2.21"]])
(ns repl.typed
(:use clojure.core.typed))
(ann foo [Number -> String])
(defn foo [n] (inc n))
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment