4.21 Program Collection

%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
%%  
%%  Program collection on functional programming in Oz
%%
%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
 
%%%%%%%%%%%%%% browse values %%%%%%%%%%%%%%%%%%%%%%%%%%

declare A = 'Maria' 
declare L = [A 'Klaus']
declare R = address(street:'Talstrasse'  
                    name:unit(first:hans  
                              second:kamp)
                    kids:L)
declare T = tuple(A L R)
{Inspect T}
 
 
 

%%%%%%%%%%%%%% browse types %%%%%%%%%%%%%%%%%%%%%%%%%%%%

{Inspect {IsRecord R}}
{Inspect {IsRecord F}}
 
{Inspect {Or {IsRecord ~100}
          {IsBool ~100}}}
{Inspect {And {And
               {IsNumber ~100}
               {IsInt ~100}}
          {IsFloat ~100}}}
 
{Inspect {Not {IsRecord false}}}
{Inspect {IsRecord {IsRecord false}}}
 
{Inspect {And
          {And
           {IsList L}
           {IsTuple L}}
          {IsRecord L}}}
 
 

%%%%%%%%%%%%%% browse status %%%%%%%%%%%%%%%%%%%%%%%%%%%

{Inspect [{Value.status R}
          {Value.status T}
          {Value.status L}
          {Value.status F}]}
 
 

%%%%%%%%%%%%%% test equality %%%%%%%%%%%%%%%%%%%%%%%%%%%

{Inspect atom==atom()}
{Inspect {IsRecord atom}}
 
{Inspect {And true==true()
          {And unit==unit()
           false==false()}}}
 
{Inspect "hiho"==[104 105 104 111]}

%%%%% procedures  %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
%% functions on numbers

declare Square = fun{$ N} N*end 

%% dollar free syntax

declare fun{Square N} N*end 

%% application of functions

{Inspect {Square 4}}

%% functions with serveral inputs

%% an arithmetic test  
declare Square = fun{$ X} X*end 
declare fun{Test X Y Z}
           {Square X} + {Square Y} == {Square Z}
        end 
{Inspect {Test 3 4 5}}

%% relational procedures

declare proc{MyInspect X}
           {Show X}
           {Inspect ['MyInspect: ' X]}
        end 
{MyInspect hiho}

%% browsing functional procedures

declare Square = fun{$ X} X*end 
{Inspect ['Browsing fun{$ X} X*X end yields <P/2>' Square]}  
{Inspect {Square 111}}

%% functions for type coercion

declare fun{NumberToFloat N}
           if {IsFloat N}  
           then N  
           else {IntToFloat N}  
           end 
        end 
%% summation of floats to integers requires type conversion
{Inspect {NumberToFloat 3.3}+{NumberToFloat 2}}

%% recursive functions on integers

%%    {Factorial N} = N * ... * 1
 
declare fun{Factorial N}
           if N==0
           then 1  
           else N*{Factorial N-1}
           end 
        end 
{Inspect {Factorial 3}}
{Inspect {Factorial 1111}}

%%%%% records %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%

word(cat:noun phon:[girl] subcat:determiner)  

{Inspect word(cat:noun phon:[girl] subcat:determiner).phon}
{Inspect word(cat:noun phon:[girl] subcat:determiner).phon.1}

{Inspect FD}
{Inspect Record}  

 
%%%%% lists %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%

%% a list representing a simple lexicon
declare WordReps=[[mary noun nil]
                  [john noun nil]
                  [girl noun determiner]
                  [nice adjective nil]
                  [pretty adjective nil]
                  [the determiner nil]
                  [laughs verb noun]
                  [meets verb [noun noun]]
                  [kisses verb [noun noun]]
                  [embarrasses verb [noun noun]]
                  [thinks verb [verb noun]]
                  [is verb [adjective noun]]
                  [met adjective nil]
                  [kissed adjective nil]
                  [embarrassed adjective nil]]
{Inspect WordReps}

%% convert lists to records
declare fun{Convert [P C S]}
           word(phon:[P] cat:C subcat:S)
        end 
declare Words = {Map WordReps Convert}
{Inspect Words}

declare Verbs = {Filter Words fun{$ W}
                                 W.cat == verb
                              end}
{Inspect Verbs}

%%%%% pattern matching %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%

%% decomposing data structures
 
declare unit(hi:X ho:unit(Y)) = unit(hi:'blue'('tain') ho:unit(jazz))
{Inspect [X Y]}  

%% pattern matching conditionals and recursion
 
declare fun{SquareList Ints}
           case Ints
           of     I|Is then I*| {SquareList Is}
           elseof nil then nil
           end 
        end   
{Inspect {SquareList [1 2 3 4 5]}}
{Inspect {SquareList {SquareList [1 2 3 4 5]}}}

%%%%% loops %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%

declare proc{InspectAll List}

           for X in List do {Inspect X} end
        end
{InspectAll [5 4 3 2 1]}

%%%%%%%%%%%%%% free variables%%%%%%%%%%%%%%%%%%%%%%%%%%

%%%%%%%%% Free, Kinded, and Determined Variables %%%%%%%%%%%%%%%%
 
declare X Y Z=1
:: 1#7
{Browse status(x:{Value.status X}
               y:{Value.status Y}
               z:{Value.status Z})}
 
{Wait X} {Browse 'will browse if you feed the line X=1 separately'}
 
X=1
 
{Browse newStatus(x:{Value.status X}
                  y:{Value.status Y}
                  z:{Value.status Z})}
 
 

%%%%% abstract data types, modules, and stacks %%%%%%%%%

%% maybe change the ADS_URL to an appropriate local filename
declare AuthorURL = 'http://www.ps.uni-sb.de/~niehren' 
declare CourseDir = 'Web/Vorlesungen/Oz-NL-SS01/vorlesung' 
declare CourseURL = AuthorURL#'/'#CourseDir
declare ADS_URL= URL#'/Functors/&pickle.version/Abstract.ozf' 
%% load the module with the abstract data structures
declare [ADS_Module] = {Module.link [ADS_URL]}
%% select the abstract data structure for the stack (class)
declare NewStack = ADS_Module.newStack
%% create a new stack (object)
declare Stack = {NewStack}
 
%% now we can use this stack
{Inspect {Stack.isEmpty}}
{Stack.push 5}
{Stack.push 27}
{Inspect {Stack.isEmpty}}
{Inspect {Stack.pop}}
 
 
 


Denys Duchier, Claire Gardent and Joachim Niehren
Version 1.3.99 (20050412)