diff --git a/chapter2/average b/chapter2/average new file mode 100644 index 0000000..509941d Binary files /dev/null and b/chapter2/average differ diff --git a/chapter2/hello b/chapter2/hello new file mode 100644 index 0000000..581e49c Binary files /dev/null and b/chapter2/hello differ diff --git a/chapter3/PatternMatching.idr b/chapter3/PatternMatching.idr index 7dbeaaa..1bba202 100644 --- a/chapter3/PatternMatching.idr +++ b/chapter3/PatternMatching.idr @@ -42,4 +42,13 @@ mutual isOdd Z = False isOdd (S k) = isEven k - + + + +displayList : Show a => List a -> String +displayList xs = "[" ++ showItems xs ++ "]" + where + showItems : Show a => List a -> String + showItems [] = "" + showItems (x :: []) = show x + showItems (x :: next) = show x ++ "," ++ showItems next diff --git a/chapter4/DataStore.idr b/chapter4/DataStore.idr index e3f966a..342ed71 100644 --- a/chapter4/DataStore.idr +++ b/chapter4/DataStore.idr @@ -18,10 +18,16 @@ addToStore (MkData size items) newitem = MkData _ (addToData items) addToData [] = [newitem] addToData (x :: xs) = x :: addToData xs +search : DataStore -> String -> List String +search store query = let predicate = isInfixOf query + allItems = toList (items store) in -- can't be bothered to work with dependant pairs + filter predicate allItems + data Command = Add String | Get Integer | Size + | Search String | Quit parseCommand : (cmd : String) -> (args : String) -> Maybe Command @@ -31,6 +37,7 @@ parseCommand "get" val = case all isDigit (unpack val) of False => Nothing parseCommand "quit" args = Just Quit parseCommand "size" args = Just Size +parseCommand "search" args = Just (Search args) parseCommand _ _ = Nothing parse : (input : String) -> Maybe Command @@ -52,6 +59,9 @@ processInput store inp = case parse inp of Just ("ID " ++ show (size store) ++ "\n", addToStore store item) Just (Get pos) => getEntry pos store Just Size => Just ("Size: " ++ show (size store) ++ "\n", store) + Just (Search query) => case (search store query) of + [] => Just ("Item Not Found\n", store) + items => Just (show items ++ "\n", store) Just Quit => Nothing