@@ -382,7 +382,7 @@ definition:
383 vadd : Vect n Int -> Vect n Int -> Vect n Int
383 vadd : Vect n Int -> Vect n Int -> Vect n Int
384 vadd [] [] = []
384 vadd [] [] = []
385− vadd (x :: idris xs) (y :: ys) = x + y :: vadd xs ys
385+ vadd (x :: xs) (y :: ys) = x + y :: vadd xs ys
387Using , we can set up a program so that it reads input from a user,
387Using , we can set up a program so that it reads input from a user,
388checks that the input is valid (i.e both vectors contain integers, and
388checks that the input is valid (i.e both vectors contain integers, and
@@ -410,10 +410,28 @@ following type:
411This returns a dependent pair of a length, and a vector of that
411This returns a dependent pair of a length, and a vector of that
412length, because we cannot know in advance how many integers the user
412length, because we cannot know in advance how many integers the user
413−is going to input. One way to implement this function, using ``-1`` to
414−indicate the end of input, is shown in Listing [readvec]. This uses a
413+is going to input. We can use ``-1`` to indicate the end of input:
415+.. code-block:: idris
417+ read_vec : Eff (p ** Vect p Int) [STDIO]
418+ read_vec = do putStr "Number (-1 when done): "
419+ case the (Maybe Int) $ run (parseNumber (trim !getStr)) of
420+ Nothing => do putStrLn "Input error"
422+ Just v => if (v /= -1)
423+ then do (_ ** xs) <- read_vec
427+ parseNumber : String -> Eff Int [EXCEPTION String]
429+ = if all (\x => isDigit x || x == '-') (unpack str)
430+ then pure (cast str)
431+ else raise "Not a number"
415−variation on ``parseNumber`` which does not require a number to be
433+This uses a variation on ``parseNumber`` which does not require a
416−within range.
434+number to be within range.
418Finally, we write a program which reads two vectors and prints the
436Finally, we write a program which reads two vectors and prints the
419result of pairwise addition of them, throwing an exception if the
437result of pairwise addition of them, throwing an exception if the
@@ -434,23 +452,6 @@ equal. This does not stop us reading vectors from user input, but it
434does require that the lengths are checked and any discrepancy is dealt
452does require that the lengths are checked and any discrepancy is dealt
435with gracefully.
453with gracefully.
437−.. code-block:: idris
439− read_vec : Eff (p ** Vect p Int) [STDIO]
440− read_vec = do putStr "Number (-1 when done): "
441− case run (parseNumber (trim !getStr)) of
442− Nothing => do putStrLn "Input error"
444− Just v => if (v /= -1)
445− then do (_ ** xs) <- read_vec
449− parseNumber : String -> Eff Int [EXCEPTION String]
451− = if all (\x => isDigit x || x == '-') (unpack str)
452− then pure (cast str)
453− else raise "Not a number"
455Example: An Expression Calculator
456Example: An Expression Calculator
456=================================
457=================================