An SASL Prelude  (cf. Appendix A of the notes)

Some programming tasks are so common and repeating when dealing with functional languages (well, this applies to programming in general) that it is a good habit to provide the compiler with a library of predefined functions, often referred to as prelude. A well-designed prelude can significantly increase the user-friendliness and usefulness of the SASL system. The prelude gets loaded at the compiler's startup so that user's may refer to names defined in the prelude when they start writing their own programs.

Below, we list the some of the functions that deserve a place in a decent SASL prelude. It is common practice to collect these definitions in one file that is presented to the compiler before it sees the user's program (the interactive SASL compiler interface gives you the choice to do so).


Index

  • Standard functions
  • List processing
  • Curried versions of arithmetic and relational operators

  • Standard functions

  • id (idendity) def id x = x
  • until (iterate applications of f to x until p is met) def until p f x = if p x then x else until p f (f x)
  • comp (f o g) def comp f g x = f (g x)

  • List processing

  • map (apply f to all elements of list l) def map f l = if l=nil then nil else f x:map f xs where x = hd l; xs = tl l
  • fold (compute m x1 (m x2 (m x3 ... (m xn z)...)) with l = [x1,x2,x3, ..., xn]) def fold m z l = if l=nil then z else m x (fold m z xs) where x = hd l; xs = tl l
  • append (concatenation of l1 and l2) def append l1 l2 = if l1=nil then l2 else x:append xs l2 where x = hd l1; xs = tl l1
  • reverse (list reversal) def reverse l = if l=nil then nil else append (reverse (tl l)) [hd l]
  • filter (list of elements of list l meeting p) def filter p l = if l=nil then nil else if p x then x:filter p xs else filter p xs where x = hd l; xs = tl l
  • sort (insertion sort with respect to relation p; contributed by Andres) def sort p l = if l=nil then nil else insert p (hd l) (sort p (tl l)) where insert pp e ll = if ll=nil then [e] else if pp e (hd ll) then e:ll else (hd ll):insert pp e (tl ll)
  • drop (l, but without its first n elements) def drop n l = if n<=0 then l else if l=nil then nil else drop (n-1) (tl l)
  • take (prefix of l of max. length n) def take n l = if n=0 or l=nil then nil else x:take (n-1) xs where x = hd l; xs = tl l
  • at (n-th element of l) def at n l = if n=0 then hd l else at (n-1) (tl l)
  • length (length of l) def length l = if l=nil then 0 else 1+length (tl l)
  • null (is l the empty list?) def null l = l=nil
  • init (l with its last element removed) def init l = if xs=nil then nil else x:init xs where x = hd l; xs = tl l
  • iterate (the infinite list [x, f x, f (f x), f (f (f x)), ...]) def iterate f x = x : iterate f (f x)
  • repeat (the infinite list [x, x, x, x, ...]) def repeat x = xs where xs=x:xs
  • cycle (the infinite list append(l, append(l, append(l, ...)...))) def cycle xs = xs1 where xs1=append xs xs1
  • splitAt (split l into two lists at element position n and return the two pieces) def splitAt n l = if n<=0 then []:l else if l=nil then []:[] else ((hd l):xs1):xs2 where xs = splitAt (n-1) (tl l); xs1 = hd xs; xs2 = tl xs
  • takeWhile (longest prefix of l whose elements meet p) def takeWhile p l = if l=nil then nil else if p x then x:takeWhile p xs else nil where x = hd l; xs = tl l
  • sum (sum of l's elements) def sum = fold plus 0
  • product (product of l's elements) def product = fold mul 1

  • Curried versions of arithmetic and relational operators

  • def plus x y = x+y def mul x y = x*y def div x y = x/y def div2 y x = x/y def minus x y = x-y def minus2 y x = x-y def lt x y = x<y def leq x y = x<=y def eq x y = x=y def neq x y = x~=y def geq x y = x>=y def gt x y = x>y

  • SASL course web home
    © Torsten Grust <Torsten.Grust@uni-konstanz.de>
    Last modified: Sep 29, 1997