Prelude Libarary
The prelude can be imported into every L4 program with IMPORT prelude and provides foundational functions for working with lists, Maybe types, Booleans, and more.
Location
Key Functions
List Functions
Construction and Deconstruction:
null- Check if list is emptyreverse- Reverse a listreplicate- Create list of n copiesrange- Generate numeric range
Transformation:
map- Apply function to each elementfilter- Keep elements matching predicatetake- First n elementsdrop- All but first n elementstakeWhile/dropWhile- Conditional take/drop
Combination:
append- Concatenate two listsconcat- Flatten list of listszip/zipWith- Combine two listspartition- Split by predicate
Aggregation:
foldr/foldl- Fold (reduce) a listsum- Sum of numbersproduct- Product of numbersmaximum/minimum- Largest/smallest elementand/or- Logical aggregationall/any- Check if all/any satisfy predicate
Searching:
elem- Check if element is in listat- Get element at indexlookup- Find value by key in association list
Sorting:
sort- Sort numberssortBy- Sort with custom comparatorinsertBy- Insert maintaining order
Uniqueness:
nub/nubBy- Remove duplicatesdelete/deleteBy- Remove element
Maybe Functions
isJust/isNothing- Check Maybe statusfromMaybe- Extract with defaultmaybe- Fold over MaybeorElse- Alternative MaybemapMaybe- Filter mapcatMaybes- Extract all JUST valuesasum/firstJust- First successful MaybemaybeToList/listToMaybe- Convert between Maybe and List
Either Functions
either- Fold over Either
Pair Functions
pmap/mapSnd- Map over second elementfmap/mapPairs- Map over list of pairs
Dictionary Functions
Construction:
emptyDict- Create empty dictionarysingleton/singleToDict- Single key-value entrypairToDict- From pairlistToDict/fromList- From association listfromListGrouped- Group values by key
Query:
dictLookup- Find value by keydictMember/dictNotMember- Check key existencedictFindWithDefault- Lookup with defaultdictKeys- All keysdictElems- All valuesdictToList- Convert to association listdictSize- Number of entriesdictIsEmpty- Check if empty
Modification:
dictInsert- Add or update entrydictInsertWith- Insert with combining functiondictDelete- Remove entrydictAdjust- Modify value at keydictUpdate- Modify or delete
Combination:
dictUnion- Merge dictionariesdictUnionWith- Merge with combining function
Higher-Order:
mapDict- Map over valuesdictMapWithKey- Map with key accessfilterDict- Filter by value predicatedictFilterWithKey- Filter by key-value predicatefoldrDict/foldlDict- Fold over valuesdictFoldrWithKey/dictFoldlWithKey- Fold with keys
Grouping:
insertValue- Insert into grouped pairsgroupPairs- Group flat pairs by key
Utility Functions
id- Identity functionconst- Constant functioneven/odd- Number paritymax/min- Maximum/minimum of two valuesTBD- Polymorphic placeholder
Example: Using Prelude Functions
-- Prelude Example: Demonstrating core list and Maybe patterns
-- The prelude is automatically imported and provides foundational types
-- List construction using FOLLOWED BY (cons) and EMPTY
DECIDE `first few numbers` IS 1 FOLLOWED BY 2 FOLLOWED BY 3 FOLLOWED BY EMPTY
#EVAL `first few numbers` -- LIST 1, 2, 3
-- Alternative: LIST syntax
DECIDE `more numbers` IS LIST 4, 5, 6
#EVAL `more numbers` -- LIST 4, 5, 6
-- Pattern matching on lists with CONSIDER
GIVEN list IS A LIST OF NUMBER
GIVETH A NUMBER
`length of list` list MEANS
CONSIDER list
WHEN EMPTY THEN 0
WHEN x FOLLOWED BY xs THEN 1 PLUS `length of list` xs
#EVAL `length of list` `first few numbers` -- 3
-- Map: Apply function to each element
GIVEN list IS A LIST OF NUMBER
GIVETH A LIST OF NUMBER
`double all values` list MEANS
CONSIDER list
WHEN EMPTY THEN EMPTY
WHEN x FOLLOWED BY xs THEN (x TIMES 2) FOLLOWED BY `double all values` xs
#EVAL `double all values` `first few numbers` -- LIST 2, 4, 6
-- Filter: Keep elements matching predicate
GIVEN list IS A LIST OF NUMBER
GIVETH A LIST OF NUMBER
`keep only even numbers` list MEANS
CONSIDER list
WHEN EMPTY THEN EMPTY
WHEN x FOLLOWED BY xs THEN
IF x MODULO 2 EQUALS 0
THEN x FOLLOWED BY `keep only even numbers` xs
ELSE `keep only even numbers` xs
#EVAL `keep only even numbers` (LIST 1, 2, 3, 4, 5, 6) -- LIST 2, 4, 6
-- Fold: Reduce list to single value (sum)
GIVEN list IS A LIST OF NUMBER
GIVETH A NUMBER
`sum of all values` list MEANS
CONSIDER list
WHEN EMPTY THEN 0
WHEN x FOLLOWED BY xs THEN x PLUS `sum of all values` xs
#EVAL `sum of all values` `first few numbers` -- 6
-- Maybe type: JUST for present values, NOTHING for absent
DECIDE `maybe has value` IS JUST 42
DECIDE `no value present` IS NOTHING
#EVAL `maybe has value` -- JUST 42
#EVAL `no value present` -- NOTHING
-- Pattern matching on Maybe
GIVEN maybe IS A MAYBE NUMBER
GIVETH A NUMBER
`get value or default to zero` maybe MEANS
CONSIDER maybe
WHEN NOTHING THEN 0
WHEN JUST x THEN x
#EVAL `get value or default to zero` `maybe has value` -- 42
#EVAL `get value or default to zero` `no value present` -- 0
-- Either type: LEFT or RIGHT
DECIDE `successful result` IS RIGHT 100
DECIDE `error result` IS LEFT "error"
#EVAL `successful result` -- RIGHT 100
#EVAL `error result` -- LEFT "error"
-- Element membership check
GIVEN x IS A NUMBER
list IS A LIST OF NUMBER
GIVETH A BOOLEAN
`is element in list` x list MEANS
CONSIDER list
WHEN EMPTY THEN FALSE
WHEN y FOLLOWED BY ys THEN
IF x EQUALS y THEN TRUE ELSE `is element in list` x ys
#EVAL `is element in list` 2 `first few numbers` -- TRUE
#EVAL `is element in list` 9 `first few numbers` -- FALSE
See prelude.l4 source for all functions.