\* (newfuntype ATAN (number --> number)) (newfuntype CEILING (number --> number)) (newfuntype CODE-CHAR { number --> character }) (newfuntype COS (number --> number)) (newfuntype EXP(number --> number)) (newfuntype FLOOR (number --> number)) (newfuntype LOG (number --> number)) (newfuntype ALPHA-CHAR-P ( character --> A)) The Qi Prelude abs type: abs :: Num a => a -> a description: returns the absolute value of a number. definition: *\ (define abs { number --> number } X -> (if (>= X 0) X (- 0 X)) ) \* Lisp version: (define abs X -> (ABS X)) usage: (0-) (abs -3) 3 all type: all :: (a -> Bool) -> [a] -> Bool description: applied to a predicate and a list, returns True if all elements of the list satisfy the predicate, and False otherwise. Similar to the function any. definition: *\ (define all { ( A --> boolean ) --> [A] --> boolean } P Xs -> (and-list (map P Xs)) ) \* usage: (0-) (all (/. X (< X 11)) (range 1 10)) true (0-) (all number? [1 2 3 a b c] ) false and-list (renamed and for lists) type: and :: [Bool] -> Bool description: takes the logical conjunction of a list of boolean values (see also `or'). definition: *\ (define and-list { [boolean] --> boolean } Xs -> (foldr and true Xs) ) \* usage: (0-) (and-list [true true false true]) false (0-) (and-list [true, true, true, true]) true (0-) (and-list []) true any type: any :: (a -> Bool) -> [a] -> Bool description: applied to a predicate and a list, returns True if any of the elements of the list satisfy the predicate, and False otherwise. Similar to the function all. definition: dfdf *\ (define any { ( A --> boolean ) --> [ A ] --> boolean } P Xs -> (or-list (map P Xs)) ) \* usage: (0-) (any (/. X (< X 11)) (range 1 10)) true (0-) (any number? [1 2 3 a b c] ) true (0-) (any number? [a b c] ) false atan type: atan :: Floating a => a -> a description: the trigonometric function inverse tan. definition: *\ (define atan { number --> number } X -> (ATAN X) ) \* usage: (0-) (atan (pi)) 1.26263 break type: break :: (a -> Bool) -> [a] -> ([a],[a]) description: given a predicate and a list, breaks the list into two lists (returned as a tuple) at the point where the predicate is first satisfied. If the predicate is never satisfied then the first element of the resulting tuple is the entire list and the second element is the empty list ([]). definition: break p xs = span p' xs where p' x = not (p x) *\ (define break { ( A --> boolean) --> [ A ] --> (@p [ A ] [ A]) } P Xs -> (span (/. X (not (P X))) Xs) ) \* usage: (0-) break isSpace "hello there fred" ("hello", " there fred") (0-) break isDigit "no digits here" ("no digits here","") ceiling type: ceiling :: (RealFrac a, Integral b) => a -> b description: returns the smallest integer not less than its argument. definition: *\ (define ceiling { number --> number } X -> (CEILING X) ) \* usage: (0-) (ceiling 3.8) 4 (0-) (ceiling -3.8) -3 see also: floor chr type: chr :: Int -> Char description: applied to an integer in the range 0 -- 255, returns the character whose ascii code is that integer. It is the converse of the function ord. An error will result if chr is applied to an integer outside the correct range. definition *\ (define chr { number --> character } C -> (CODE-CHAR C)) \* usage: (0-) (chr 65) 'A' (0-) (= (ord (chr 65)) 65) true see also: ord compare type: compare :: Ord a => a -> a -> Ordering description: applied to to values of the same type which have an ordering defined on them, returns a value of type Ordering which will be: EQ if the two values are equal; GT if the first value is strictly greater than the second; and LT if the first value is less than or equal to the second value. definition: *\ (define compare { A --> A --> character } X Y -> eq where (= X Y) X Y -> lt where (< X Y) _ _ -> gt ) \* usage: (0-) (compare "aadvark" "zebra") LT compose (NEW FUNCTION) type: [(a -> a)] -> (a -> a) description: Takes a non-empty list of 1-place functions and forms their composition definition: *\ (define compose { [ ( A --> A) ] --> ( A --> A) } [F] -> F [F | Xs] -> (/. X (F ((compose Xs) X))) ) \* usage: (0)> ((compose [(/. X (+ 1 X)) sqrt]) 5) 3.236068 concat-list (renamed concat) type: concat :: [[a]] -> [a] description: applied to a list of lists, joins them together using the ++ operator. definition: *\ (define concat-list { [[ A ]] --> [ A ] } Xs -> (foldr append [] Xs) ) \* usage: (0-) (concat-list [[1 2 3] [4] [] [5 6 7 8]]) [1 2 3 4 5 6 7 8] concatMap type: concatMap :: (a -> [b]) -> [a] -> [b] description: given a function which maps a value to a list, and a list of elements of the same type as the value, applies the function to the list and then concatenates the result (thus flattening the resulting list). definition: *\ (define concatMap { ( A --> [B] ) --> [A] --> [B] } F -> (compose [concat-list (map F) ]) ) \* usage: (0-) concatMap show [1,2,3,4] "1234" const type: const :: const :: a -> b -> a description: creates a constant valued function which always has the value of its first argument, regardless of the value of its second argument. It is the K in SKI. definition: *\ (define const { A --> B --> A } K _ -> K ) \* usage: (0-) (const 12 "lucky") 12 cos type: cos :: Floating a => a -> a description: the trigonometric cosine function, arguments are interpreted to be in radians. definition: *\ (define cos { number --> number } X -> (COS X) ) \* usage: (0-) (cos (pi) -1.0 (0-) (cos (/ (pi) 2)) -4.37114e-08 digitToInt type: digitToInt :: Char -> Int description: converts a digit character into the corresponding integer value of the digit. definition: *\ (define digitToInt { character --> number } C -> 0 where (= C #\0) C -> 1 where (= C #\1) C -> 2 where (= C #\2) C -> 3 where (= C #\3) C -> 4 where (= C #\4) C -> 5 where (= C #\5) C -> 6 where (= C #\6) C -> 7 where (= C #\7) C -> 8 where (= C #\8) C -> 9 where (= C #\9) C -> 10 where (or (= C #\a) (= C #\A)) C -> 11 where (or (= C #\b) (= C #\B)) C -> 12 where (or (= C #\c) (= C #\C)) C -> 13 where (or (= C #\d) (= C #\D)) C -> 14 where (or (= C #\e) (= C #\E)) C -> 15 where (or (= C #\f) (= C #\F)) ) \* usage: (0-) 3 div type: div :: Integral a => a -> a -> a description: computes the integer division of its integral arguments. definition: *\ (define div { number --> number --> number } X Y -> (floor (* 1.0 (/ X Y))) ) \* usage: (0-) 16 `div` 9 1 (0-) (-12) `div` 5 -3 notes: `div` is integer division such that the result is truncated towards negative infinity. drop type: drop :: Int -> [a] -> [a] description: applied to a number and a list, returns the list with the specified number of elements removed from the front of the list. If the list has less than the required number of elements then it returns []. definition: *\ (define drop { number --> [A] --> [A] } 0 Xs -> Xs _ [] -> [] N [_ | Xs] -> (drop (- N 1) Xs) where (> N 0) _ _ -> (error "drop: negative argument") ) \* usage: (0-) (drop 3 (range 1..10)) [4, 5, 6, 7, 8, 9, 10] dropWhile type: dropWhile :: (a -> Bool) -> [a] -> [a] description: applied to a predicate and a list, removes elements from the front of the list while the predicate is satisfied. definition: *\ (define dropWhile { ( A --> boolean) --> [A] --> [A] } P [] -> [] P [ X | Xs] -> (dropWhile P Xs) where (P X) P L -> L ) \* usage: (0-) (dropWhile (/. X (< X 5)) (range 1 10)) [5 6 7 8 9 10] elem type: elem :: Eq a => a -> [a] -> Bool description: applied to a value and a list returns True if the value is in the list and False otherwise. The elements of the list must be of the same type as the value. definition: *\ (define elem { A --> [A] --> boolean } X L -> (element? X L) ) \* usage: (0-) (elem 5 (range 1 10)) true (0-) (elem "rat" ["fat" "cat" "sat" "flat"]) false error type: error :: String -> a description: applied to a string creates an error value with an associated message. Error values are equivalent to the undefined value (undefined), any attempt to access the value causes the program to terminate and print the string as a diagnostic. definition: defined internally. usage: (error "this is an error message") even type: even :: Integral a => a -> Bool description: applied to an integral argument, returns True if the argument is even, and False otherwise. definition: *\ (define even { number --> boolean } X -> (= (rem X 2) 0) ) \* usage: (0-) (even 2) true (0-) (even (* 11 3)) false exp type: exp :: Floating a => a -> a description: the exponential function (exp n is equivalent to en). definition: *\ (define exp { number --> number } X -> (EXP X) ) \* usage: (0-) (exp 1) 2.71828 filter type: filter :: (a -> Bool) -> [a] -> [a] description: applied to a predicate and a list, returns a list containing all the elements from the argument list that satisfy the predicate. definition: *\ (define filter { ( A --> boolean) --> [A] --> [A] } P [] -> [] P [ X | Xs ] -> [ X | (filter P Xs)] where (P X) P [ X | Xs ] -> (filter P Xs) ) \* usage: (0-) (filter number? [f a t 1 2 3 c a t 4 5 6]) [1 2 3 4 5 6] flatten type: flatten [a] -> [a] description: takes in a list and takes only the non-cons elements and puts them in a list *\ (define flatten { [A] --> [A] } [] -> [] X -> [X] where (not (cons? X)) [X | Xs] -> (append (flatten X) (flatten Xs)) ) \* usage: (0-) (flatten [ [ 1 [2]] [2]]) [1 2 2] flip type: flip :: (a -> b -> c) -> b -> a -> c description: applied to a binary function, returns the same function with the order of the arguments reversed. definition: *\ (define flip { ( A --> B --> C ) --> B --> A --> C } F X Y -> (F Y X) ) \* usage: (0-) (flip elem (range 1 10) 5) true floor type: floor :: (RealFrac a, Integral b) => a -> b description: returns the largest integer not greater than its argument. definition: *\ (define floor {number --> number} X -> (FLOOR X)) \* usage: (0-) (floor 3.8) 3 (0-) (floor -3.8) -4 see also: ceiling foldl type: foldl :: (a -> b -> a) -> a -> [b] -> a description: folds up a list, using a given binary operator and a given start value, in a left associative manner. foldl op r [a, b, c] ? ((r `op` a) `op` b) `op` c definition: *\ (define foldl { ( A --> B --> A ) --> A --> [B] --> A } _ Z [] -> Z F Z [ X | Xs ] -> (foldl F (F Z X) Xs) ) \* usage: (0-) (foldl + 0 (range 1 10)) 55 (0-) (foldl (flip cons) [] (range 1 10)) [10, 9, 8, 7, 6, 5, 4, 3, 2, 1] foldl1 type: foldl1 :: (a -> a -> a) -> [a] -> a description: folds left over non--empty lists. definition: *\ (define foldl1 { ( A --> A --> A ) --> [A] --> A } F [ X | Xs] -> (foldl F X Xs) ) \* usage: (0-) (foldl1 max [1 10 5 2 -1]) 10 foldr type: foldr :: (a -> b -> b) -> b -> [a] -> b description: folds up a list, using a given binary operator and a given start value, in a right associative manner. foldr op r [a, b, c] ? a `op` (b `op` (c `op` r)) definition: *\ (define foldr { ( A --> B --> B ) --> B --> [A] --> B } _ Z [] -> Z F Z [ X | Xs ] -> (F X (foldr F Z Xs)) ) \* usage: (0-) (foldr append [] [[1 2 3] [4 5 6] [7 8] [9 10 11]]) "concatenate" foldr1 type: foldr1 :: (a -> a -> a) -> [a] -> a description: folds right over non--empty lists. definition: *\ (define foldr1 { ( A --> A --> A ) --> [A] --> A } F [X] -> X F [X | Xs] -> (F X (foldr1 F Xs)) ) \* usage: (0-) (foldr1 * (range 1 10)) 3628800 fromInt type: fromInt :: Num a => Int -> a description: Converts from an Int to a numeric type which is in the class Num. usage: (0-) (fromInt 3)::Float 3.0 fromInteger type: fromInteger :: Num a => Integer -> a description: Converts from an Integer to a numeric type which is in the class Num. usage: (0-) (fromInteger 10000000000)::Float 1.0e+10 fst type: fst :: (a, b) -> a description: returns the first element of a two element tuple. definition: defined internally usage: (0-) (fst (@p "harry" 3)) "harry" gcd type: gcd :: Integral a => a -> a -> a description: returns the greatest common divisor between its two integral arguments. definition: *\ (define gcd { number --> number --> number } 0 0 -> (error "gcd: GCD 0 0 is undefined") X Y -> (gcd-help (abs X) (abs Y)) ) (define gcd-help { number --> number --> number } X 0 -> X X Y -> (gcd-help Y (rem X Y)) ) \* usage: (0-) (gcd 2 10) 2 (0-) (gcd -7 13) 1 head type: head :: [a] -> a description: returns the first element of a non--empty list. If applied to an empty list an error results. definition: defined internally usage: (0-) (head (range 1 10)) 1 (0-) (head ["this" "and" "that"]) "this" id type: id :: a -> a description: the identity function, returns the value of its argument. definition: *\ (define id { A --> A } X -> X ) \* usage: (0-) (id 12) 12 (0-) (id (id "fred")) "fred" (0-) (= (map id (range 1 10)) (range 1 10)) true init type: init :: [a] -> [a] description: returns all but the last element of its argument list. The argument list must have at least one element. If init is applied to an empty list an error occurs. definition: *\ (define init { [A] --> [A] } [X] -> [] [X|Xs] -> [X | (init Xs)] ) \* usage: (0-) (init (range 1 10)) [1, 2, 3, 4, 5, 6, 7, 8, 9] isAlpha type: isAlpha :: Char -> Bool description: applied to a character argument, returns True if the character is alphabetic, and False otherwise. definition: *\ (define isAlpha {character --> boolean} C -> (if (= (ALPHA-CHAR-P C) []) false true) ) \* usage: (0-) (isAlpha 'a') true (0-) (isAlpha '1') false isDigit type: isDigit :: Char -> Bool description: applied to a character argument, returns True if the character is a numeral, and False otherwise. definition: *\ (define isDigit {character --> boolean} C -> (if (= (DIGIT-CHAR-P C) []) false true) ) \* usage: (0-) (isDigit '1') true (0-) (isDigit 'a') false isLower type: isLower :: Char -> Bool description: applied to a character argument, returns True if the character is a lower case alphabetic, and False otherwise. definition: *\ (define isLower {character --> boolean} C -> (if (= (LOWER-CASE-P C) []) false true) ) \* usage: (0-) (isLower 'a') True (0-) (isLower 'A') False (0-) (isLower '1') False isSpace type: isSpace :: Char -> Bool description: returns True if its character argument is a whitespace character and False otherwise. definition: *\ (define isSpace {character --> boolean} C ->(or-list [ (= C #\Space) (= C #\Tab) (= C #\Newline) (= C #\Linefeed) (= C #\Page) (= C #\Return)] ) ) \* usage: Prelude> (dropWhile isSpace [ ' ' ' ' 'e' ]) isUpper type: isUpper :: Char -> Bool description: applied to a character argument, returns True if the character is an upper case alphabetic, and False otherwise. definition: isUpper c = c >= 'A' && c <= 'Z' usage: (0-) isUpper 'A' True (0-) isUpper 'a' False (0-) isUpper '1' False *\ (define isUpper {character --> boolean} C -> (if (= (UPPER-CASE-P C) []) false true) ) \* last type: last :: [a] -> a description: applied to a non--empty list, returns the last element of the list. definition: *\ (define last { [A] --> A } [X] -> X [_|Xs] -> (last Xs) ) \* usage: (0-) (last (range 1 10)) 10 lcm type: lcm :: Integral a => a -> a -> a description: returns the least common multiple of its two integral arguments. definition: *\ (define lcm { number --> number --> number } _ 0 -> 0 0 _ -> 0 X Y -> (abs (* (quot X (gcd X Y)) Y)) ) \* usage: (0-) (lcm 2 10) 10 (0-) (lcm 2 11) 22 length type: length :: [a] -> Int description: returns the number of elements in a finite list. definition: defined internally usage: (0-) (length (range 1 10)) 10 lines type: lines :: String -> [String] description: applied to a list of characters containing newlines, returns a list of lists by breaking the original list into lines using the newline character as a delimiter. The newline characters are removed from the result. definition: lines [] = [] lines (x:xs) = l : ls where (l, xs') = break (== '/n') (x:xs) ls | xs' == [] = [] | otherwise = lines (tail xs') usage: (0-) lines "hello world/nit's me,/neri/n" ["hello world", "it's me,", "eric"] *\ (define lines { string --> [string] } S -> (reverse (lines-prime (s2l S) [])) ) (define lines-prime { [character] --> [string] } [] Ls -> Ls [X|Xs] Ls ->(let Xsp (break (= #\Newline) [X|Xs]) (if (= (snd Xsp) []) [(fst Xsp) | Ls] (lines-prime (tail (snd Xsp)) [(fst Xsp) | Ls]))) ) \* lisp-truth type: lisp-truth :: a -> Bool description: Takes a truth value from lisp and converts it into Qi true or false definition: *\ (define lisp-truth { A --> boolean } X -> (if (= X []) false true) ) \* log type: log :: Floating a => a -> a description: returns the natural logarithm of its argument. definition: *\ (define log { number --> number } X -> (LOG X) ) \* usage: (0-) (log 1) 0.0 (0-) (log 3.2) 1.16315 map type: map :: (a -> b) -> [a] -> [b] description: given a function, and a list of any type, returns a list where each element is the result of applying the function to the corresponding element in the input list. definition: defined internally usage: (0-) (map sqrt (range 1 5)) [1 1.4142135 1.7320508 2 2.236068] max type: max :: Ord a => a -> a -> a description: applied to two values of the same type which have an ordering defined upon them, returns the maximum of the two elements according to the operator >=. definition: *\ (define max { number --> number --> number } X Y -> X where (>= X Y) X Y -> Y ) \* usage: (0-) (max 1 2) 2 maximum type: maximum :: Ord a => [a] -> a description: applied to a non--empty list whose elements have an ordering defined upon them, returns the maximum element of the list. definition: *\ (define maximum { [ number ] --> number } XS -> (foldl1 max XS) ) \* usage: (0-) (maximum [ -10 0 5 22 13] ) 22 min type: min :: Ord a => a -> a -> a description: applied to two values of the same type which have an ordering defined upon them, returns the minimum of the two elements according to the operator <=. definition: *\ (define min { number --> number --> number } X Y ->(MIN X Y) ) \* usage: (0-) (min 1 2) 1 minimum type: minimum :: Ord a => [a] -> a description: applied to a non--empty list whose elements have an ordering defined upon them, returns the minimum element of the list. definition: *\ (define minimum { [ number ] --> number } XS -> (foldl1 min XS) ) \* usage: (0-) (minimum [ -10 0 5 22 13]) -10 mod type: mod :: Integral a => a -> a -> a description: returns the modulus of its two arguments. definition: *\ (define mod { number --> number --> number } X Y -> (MOD X Y)) \* usage: (0-) (mod 16 9) 7 newline type: newline :: a => String description: returns a newline string definition: *\ (define newline { string } -> (make-string "~%")) \* not type: not :: Bool -> Bool description: returns the logical negation of its boolean argument. definition: defined internally usage: (0-) (not (= 3 4)) true (0-) (not (> 10 2)) false notElem description: returns True if its first argument is not an element of the list as its second argument. definition: *\ (define notElem X Y -> (not (element? X Y)) ) \* usage: (0-) (notElem 3 [1 2 3] ) false (0-) (notElem 4 [1 2 3] ) true null type: null :: [a] -> Bool description: returns True if its argument is the empty list ([]) and False otherwise. definition: *\ (define null [] -> true [_|_] -> false ) \* usage: (0-) (null []) True (0-) (null (take 3 (range 1 10))) False odd type: odd :: Integral a => a -> Bool description: applied to an integral argument, returns True if the argument is odd, and False otherwise. definition: *\ (define odd X -> (not (even X)) ) \* usage: (0-) (odd 1) true (0-) (odd (* 2 12)) false or-list (renamed or) type: or :: [Bool] -> Bool description: applied to a list of boolean values, returns their logical disjunction (see also `and'). definition: *\ (define or-list { [boolean] --> boolean } Xs -> (foldr or false Xs) ) \* usage: (0-) (or-list [false false true false] ) true (0-) (or-list [false false false false] ) false (0-) (or-list [] ) false ord type: ord :: Char -> Int description: applied to a character, returns its ascii code as an integer. definition: defined internally. usage: *\ (define ord {character --> number } C -> (CHAR-CODE C)) \* (0-) (ord 'A') 65 (0-) (= (chr (ord 'A')) 'A') true see also: chr pi type: pi :: Floating a => a description: the ratio of the circumference of a circle to its diameter. definition: defined internally. usage: (0-) pi 3.14159 (0-) cos pi -1.0 *\ (define pi -> 3.141592653589793L0) \* pred type: pred :: Enum a => a -> a description: applied to a value of an enumerated type returns the predecessor (previous value in the enumeration) of its argument. If its argument is the first value in an enumeration an error will occur. usage: (0-) pred 1 0 (0-) pred True False putStr type: putStr :: String -> IO () description: takes a string as an argument and returns an I/O action as a result. A side-effect of applying putStr is that it causes its argument string to be printed to the screen. definition: defined internally. usage: (0-) putStr "Hello World/nI'm here!" Hello World I'm here! product type: product :: Num a => [a] -> a description: applied to a list of numbers, returns their product. definition: *\ (define product { [number] --> number } Xs -> (foldl * 1 Xs) ) \* usage: (0-) (product (range 1 10)) 3628800 quot type: quot :: Integral a => a -> a -> a description: returns the quotient after dividing the its first integral argument by its second integral argument. definition: defined internally. usage: (0-) 16 `quot` 8 2 (0-) quot 16 9 1 *\ (define quot { number --> number --> number } X Y -> (FLOOR (* 1.0 (/ X Y))) ) \* range description: Copy of the .. opperator in Haskell definition: *\ (define range { number --> number --> [ number ] } X Y -> (range-prime X Y 1 <= ) where (< X Y) X Y -> (range-prime X Y -1 >= ) ) (define range-prime { number --> number --> number --> ( number --> number --> boolean) --> [ number ] } X Y _ P -> [] where (not (P X Y)) X Y N P -> [X | (range-prime (+ N X) Y N P ) ] ) \* rem type: rem :: Integral a => a -> a -> a description: returns the remainder after dividing its first integral argument by its second integral argument. definition: defined internally. usage: (0-) 16 `rem` 8 0 (0-) rem 16 9 7 notes: The following equality holds: (x `quot` y)*y + (x `rem` y) == x *\ (define rem { number --> number --> number } X Y -> (REM X Y) ) \* repeat type: repeat :: a -> [a] description: given a value, returns an infinite list of elements the same as the value. definition: repeat x = xs where xs = x:xs usage: (0-) repeat 12 [12, 12, 12, 12, 12, 12, 12, 12, 12, 12, 12, 12 ....] replicate type: replicate :: Int -> a -> [a] description: given an integer (positive or zero) and a value, returns a list containing the specified number of instances of that value. definition: *\ (define replicate { number --> A --> [A]} N X -> (replicate* N X []) ) (define replicate* { number --> A --> [A] --> [A]} 0 X Xs -> Xs N X Xs -> (replicate* (- N 1) X [ X | Xs ]) ) \* usage: (0-) (replicate 3 "apples") ["apples" "apples" "apples"] reverse type: reverse :: [a] -> [a] description: applied to a finite list of any type, returns a list of the same elements in reverse order. definition: reverse = foldl (flip (:)) [] usage: (0-) reverse [1..10] [10, 9, 8, 7, 6, 5, 4, 3, 2, 1] round type: round :: (RealFrac a, Integral b) => a -> b description: rounds its argument to the nearest integer. usage: (0-) (round 3.2) 3 (0-) (round 3.5) 4 (0-) (round -3.2) -3 show type: show :: Show a => a -> String description: converts a value (which must be a member of the Show class), to its string representation. definition: defined internally. usage: (0-) "six plus two equals " ++ (show (6 + 2)) "six plus two equals 8" sin type: sin :: Floating a => a -> a description: the trigonometric sine function, arguments are interpreted to be in radians. definition: defined internally. usage: (0-) sin (pi/2) 1.0 (0-) ((sin pi)^2) + ((cos pi)^2) 1.0 *\ (define sin { number --> number } X -> (SIN X)) \* snd type: snd :: (a, b) -> b description: returns the second element of a two element tuple. definition: defined internally usage: (0-) (snd (@p "harry" 3)) 3 span type: span :: (a -> Bool) -> [a] -> ([a],[a]) description: given a predicate and a list, splits the list into two lists (returned as a tuple) such that elements in the first list are taken from the head of the list while the predicate is satisfied, and elements in the second list are the remaining elements from the list once the predicate is not satisfied. definition: *\ (define span { (A --> boolean ) --> [A] --> (@p [A] [A]) } P [] -> (@p [] []) P [X|Xs] -> (let Yz (span P Xs) (@p [X|(fst Yz)] (snd Yz)) ) where (P X) P Xs -> (@p [] Xs) ) \* usage: (o-) (span isDigit (s2l "123abc456")) (@p ['1' '2' '3'] ['a' 'b' 'c' '4' '5' '6']) splitAt type: splitAt :: Int -> [a] -> ([a],[a]) description: given an integer (positive or zero) and a list, splits the list into two lists (returned as a tuple) at the position corresponding to the given integer. If the integer is greater than the length of the list, it returns a tuple containing the entire list as its first element and the empty list as its second element. definition: *\ (define splitAt { number --> [A] --> (@p [A] [A]) } 0 Xs -> (@p [] Xs) _ [] -> (@p [] []) N [X|Xs] -> (let Aftersplit (splitAt (- N 1) Xs) (@p [X|(fst Aftersplit)] (snd Aftersplit)) ) where (> N 0) _ _ -> (error "splitAt: negative arg or non list") ) \* usage: (0-) (splitAt 3 (range 1 10)) (@p [1 2 3] [4 5 6 7 8 9 10]) (0-) (splitAt 5 (s2l "abc")) (@p ['a' 'b' 'c'] NIL) s2l and l2s description: Turns a string into a list or vise versa definition: *\ (define s2l { string --> [character]} S -> (s2l* S 0 []) ) (define s2l* { string --> number --> [character] --> [character]} S N L -> (reverse L) where (= (LENGTH S) N) S N L -> (s2l* S (+ N 1) (cons (CHAR S N) L) ) ) (define l2s { [character] --> string } [] -> "" S -> (read-chars-as-stringlist (reverse S) (/. X X)) ) \* sqrt type: sqrt :: Floating a => a -> a description: returns the square root of a number. definition: defined internally usage: (0-) (sqrt 16) 4 subtract type: subtract :: Num a => a -> a -> a description: subtracts its first argument from its second argument. definition: *\ (define subtract { number --> number --> number } X Y -> (- Y X)) \* usage: (0-) (subtract 7 10) 3 sum type: sum :: Num a => [a] -> a description: computes the sum of a finite list of numbers. definition: *\ (define sum { [ number ] --> number } Xs -> (foldl + 0 Xs) ) \* usage: (0-) (sum (range 1 10)) 55 tail type: tail :: [a] -> [a] description: applied to a non--empty list, returns the list without its first element. definition: defined internally usage: (0-) (tail [1 2 3]) [2 3] take type: take :: Int -> [a] -> [a] description: applied to an integer (positive or zero) and a list, returns the specified number of elements from the front of the list. If the list has less than the required number of elements, take returns the entire list. definition: *\ (define take { number --> [A] --> [A] } 0 _ -> [] _ [] -> [] N [X|Xs] -> (cons X (take (- N 1) Xs)) where (> N 0) _ _ -> (error "take: negative argument or non list") ) \* usage: (0-) (take 2 [1 2 3]) [1 2] (0-) (take 5 [1 2 3]) [1 2 3] takeWhile type: takeWhile :: (a -> Bool) -> [a] -> [a] description: applied to a predicate and a list, returns a list containing elements from the front of the list while the predicate is satisfied. definition: *\ (define takeWhile { (A --> boolean) --> [A] --> [A] } P [] -> [] P [X|Xs] -> [X | (takeWhile P Xs)] where (P X) _ _ -> [] ) \* usage: (0-) (takeWhile (/. X (< X 5)) [1 2 3 10 4 2]) [1 2 3] tan type: tan :: Floating a => a -> a description: the trigonometric function tan, arguments are interpreted to be in radians. definition: *\ (define tan { number --> number } X -> (TAN X) ) \* usage: (0-) (tan (/ (pi) 4)) 0.99999999999999988074L0 toLower type: toLower :: Char -> Char description: converts an uppercase alphabetic character to a lowercase alphabetic character. If this function is applied to an argument which is not uppercase the result will be the same as the argument unchanged. definition: *\ (define toLower { character --> character } C -> (CHAR-DOWNCASE C) ) \* usage: (0-) toLower 'A' 'a' (0-) toLower '3' '3' toUpper type: toUpper :: Char -> Char description: converts a lowercase alphabetic character to an uppercase alphabetic character. If this function is applied to an argument which is not lowercase the result will be the same as the argument unchanged. definition: *\ (define toUpper { character --> character } C -> (CHAR-UPCASE C) ) \* usage: (0-) toUpper 'a' 'A' (0-) toUpper '3' '3' truncate type: truncate :: (RealFrac a, Integral b) => a -> b description: drops the fractional part of a floating point number, returning only the integral part. definition: *\ (define truncate { number --> number } X -> (TRUNCATE X) ) \* usage: (0-) truncate 3.2 3 (0-) truncate (-3.2) -3 unlines type: unlines :: [String] -> String description: converts a list of strings into a single string, placing a newline character between each of them. It is the converse of the function lines. definition: *\ (define unlines { [ string ] --> string } Xs -> (foldl (/. OldString (/. Current (make-string "~A~A~%" OldString Current))) "" Xs) ) \* usage: (0-) (unlines ["hello world" "it's me," "eric"]) "hello world it's me, eric " until type: until :: (a -> Bool) -> (a -> a) -> a -> a description: given a predicate, a unary function and a value, it recursively re--applies the function to the value until the predicate is satisfied. If the predicate is never satisfied until will not terminate. definition: *\ (define until { (A --> boolean) --> ( A --> A) --> A --> A } P F X -> X where (P X) P F X -> (until P F (F X)) ) \* usage: (0-) (until (/. X (> X 1000)) (* 2) 1) 1024 unwords type: unwords :: [String] -> String description: concatenates a list of strings into a single string, placing a single space between each of them. definition: *\ (define unwords { [string] --> string } Xs -> (foldl1 (/. OldString (/. Current (make-string "~A ~A" OldString Current))) Xs) ) \* usage: (0-) (unwords ["the" "quick" "brown" "fox"]) "the quick brown fox" words type: words :: String -> [String] description: breaks its argument string into a list of words such that each word is delimited by one or more whitespace characters. definition: words s | findSpace == [] = [] | otherwise = w : words s'' where (w, s'') = break isSpace findSpace findSpace = dropWhile isSpace s usage: (0-) (words "the quick brown/n/nfox" ["the", "quick", "brown", "fox"] *\ (define words { string --> [string] } Xs ->(read-chars-as-stringlist (s2l Xs) (/. X (isSpace X)) ) ) \* zip type: zip :: [a] -> [b] -> [(a,b)] description: applied to two lists, returns a list of pairs which are formed by tupling together corresponding elements of the given lists. If the two lists are of different length, the length of the resulting list is that of the shortest. definition: zip xs ys = zipWith pair xs ys where pair x y = (x, y) usage: (0-) zip [1..6] "abcd" [(1, 'a'), (2, 'b'), (3, 'c'), (4, 'd')] *\ (define zip { [A] --> [B] --> [(@p A B)] } Xs Ys -> (zipWith @p Xs Ys) ) \* zipWith type: zipWith :: (a -> b -> c) -> [a] -> [b] -> [c] description: applied to a binary function and two lists, returns a list containing elements formed be applying the function to corresponding elements in the lists. definition: *\ (define zipWith { (A --> B --> C) --> [A] --> [B] --> [C] } Z [A|As] [B|Bs] -> (cons (Z A B) (zipWith Z As Bs)) _ _ _ -> [] ) \* usage: (0-) (zipWith + (range 1 5) (range 6 10)) [7 9 11 13 15] *\