From b5950f78e150448d20e5e12cdc9b116207defa4e Mon Sep 17 00:00:00 2001 From: Mart Lubbers Date: Wed, 14 Oct 2015 20:54:12 +0200 Subject: [PATCH] alles werkt behalve 4, super vaag --- a6/mart/Makefile | 2 +- a6/mart/skeleton6a.icl | 6 +++--- a6/mart/skeleton6b.icl | 34 ++++++++++++++++++++++++++-------- 3 files changed, 30 insertions(+), 12 deletions(-) diff --git a/a6/mart/Makefile b/a6/mart/Makefile index 85e6c86..ed1d028 100644 --- a/a6/mart/Makefile +++ b/a6/mart/Makefile @@ -1,4 +1,4 @@ -ITASKSPRJS:=skeleton6a #skeleton6b.exe +ITASKSPRJS:=skeleton6a skeleton6b CPM:=cpm all: $(ITASKSPRJS) diff --git a/a6/mart/skeleton6a.icl b/a6/mart/skeleton6a.icl index d6af8d4..d4bcf61 100644 --- a/a6/mart/skeleton6a.icl +++ b/a6/mart/skeleton6a.icl @@ -35,7 +35,7 @@ class iTasksLite a | print a & parse a & TC a :: Description :== String :: StoreID a :== String :: *TaskResult a :== (a, TaskState) -:: *TaskState = { console :: *File +:: *TaskState = { console :: !*File , store :: Map String Dynamic } @@ -54,7 +54,7 @@ viewInformation d a ts=:{console} = enterInformation :: Description TaskState -> TaskResult a | iTasksLite a enterInformation d ts=:{console} -# (a, console) = freadline (console <<< "Enter the answer: ") +# (a, console) = freadline (console <<< d <<< ": ") = case parse a of Just x = (x, {ts & console=console}) Nothing = enterInformation d {ts & console=console <<< "Wrong format, try again.\n"} @@ -125,7 +125,7 @@ where Start world # (console, world) = stdio world console = console <<< "Welcome to iTasksLite\n\n" - (r, console) = eval task4 console + (r, console) = eval task3 console console = console <<< "\nThe result of the task is " <<< print r <<< ".\n" (_, world) = fclose console world = world diff --git a/a6/mart/skeleton6b.icl b/a6/mart/skeleton6b.icl index 9e1c5cf..6e73895 100644 --- a/a6/mart/skeleton6b.icl +++ b/a6/mart/skeleton6b.icl @@ -34,7 +34,7 @@ class iTasksLite a | print a & parse a & TC a :: Description :== String :: StoreID a :== String -:: Task a = // define type here +:: Task a = Task (*TaskState -> *(a, *TaskState)) :: *TaskState = { console :: !*File , store :: Map String Dynamic } @@ -50,28 +50,46 @@ retrieve_ sid store = case get sid store of instance Functor Task where fmap :: (a -> b) (Task a) -> Task b - fmap _ _ = undef + fmap f (Task g) = Task \st.let (x, st1) = g st in (f x, st1) instance Applicative Task where pure :: a -> Task a - pure _ = undef + pure x = Task \st.(x, st) (<*>) infixl 4 :: (Task (a -> b)) (Task a) -> Task b - (<*>) _ _ = undef + (<*>) (Task f) (Task g) = Task \st.let (x, st1) = g st in let (y, st2) = f st1 in (y x, st2) instance Monad Task where bind :: (Task a) (a -> Task b) -> Task b - bind _ _ = undef + bind (Task f) g = Task \st.let (x, st1) = f st in let (Task y) = g x in y st1 eval :: (Task a) *File -> (a, *File) | iTasksLite a eval (Task taskFunc) console # (r, {console}) = taskFunc {store = newMap, console = console} = (r, console) +viewInformation :: Description a -> Task a | iTasksLite a +viewInformation d x = Task \st=:{console}.(x, {st & console=console <<< d <<< ": " <<< print x <<< ".\n"}) + +enterInformation :: Description -> Task a | iTasksLite a +enterInformation d = Task f + where + f st=:{console} + # (a, console) = freadline (console <<< d <<< ": ") + = case parse a of + Just x = (x, {st & console=console}) + Nothing = f {st & console=console <<< "Wrong format, try again.\n"} + +store :: a (StoreID a) -> Task a | iTasksLite a +store v s = Task \st=:{store}.(v, {st & store=store_ v s store}) + +retrieve :: (StoreID a) -> Task a | iTasksLite a +retrieve s = Task \st=:{store}.(retrieve_ s store, st) + task0 :: Task Int task0 = return 42 -/*task1 :: Task Int +task1 :: Task Int task1 = viewInformation "The answer is" 42 task2 :: Task Int @@ -106,12 +124,12 @@ where >>| addIdea ideaStore :: StoreID [String] - ideaStore = "ideas"*/ + ideaStore = "ideas" Start world # (console, world) = stdio world console = console <<< "Welcome to iTasksLite" <<< "\n\n" - (r, console) = eval task0 console + (r, console) = eval task4 console console = console <<< "\n" <<< "The result of the task is " <<< print r <<< ".\n" (_, world) = fclose console world = world -- 2.20.1