Initializing help system before first use

Local declarations in with statements


Type: Programming
Rating: 2 (easy-medium)
Description: Definition of 'with' statements for local declaration of scalar and structured types, including for the enumeration of arrays with unnamed index sets.
File(s): usingwith.mos


usingwith.mos
(!******************************************************
   Mosel User Guide Example Problems
   ================================= 

   file usingwith.mos 
   `````````````````` 
   Definition of 'with' statements for local declaration
   of structured types.
 
   (c) 2022 Fair Isaac Corporation
       author: S. Heipcke, Feb. 2022
*******************************************************!)

model "using with statements"

!**** 'with' can take a comma-separated list of several local declarations 
 L:=[1,4,2,10,1]
 with first=L.first, last=L.last do
   writeln("L starts with value 1: ", first=1)
   writeln("L starts and ends with same value: ", first=last)
 end-do

!**** Local declaration as shorthand to save access time for elements of
!**** complex structures when using them repeatedly within a block of code
 declarations
   NB: dynamic array(R:range) of set of integer   
 end-declarations

 NB(1):={2,3,4}; NB(2):={1,3,5,6}; NB(6):={2,4}
 forall(i in R | exists(NB(i)) )
   with S=NB(i), el=getelt(S) do
     writeln(i, ": Size of set=", S.size, ", contains 1:", 1 in S,
             ", an element the set:", el, " is different from 0:", el<>0)
   end-do

! Local declaration of scalars is also possible in 'forall' statements, but
! this form cannot be used for structured types
 forall(i in R | exists(NB(i)), el=getelt(NB(i)))
   with S=NB(i) do
     writeln(i, ": Size of set=", S.size, ", contains 1:", 1 in S,
             ", an element the set:", el)
   end-do

!**** Aliasing of an array and its index set(s): efficient use of 'exists' 
!**** for enumerating an array with unnamed indexing sets
 declarations
   A1:dynamic array(range,string) of integer  ! Array with unnamed indexing sets
 end-declarations

 A1(1,'a'):=1; A1(4,'c'):=4; A1(6,'d'):=6

 with A(I,J)=A1 do
   forall(i in I, j in J | exists(A(i,j))) write(" ", A(i,j))
 end-do
 writeln 

end-model

© 2001-2024 Fair Isaac Corporation. All rights reserved. This documentation is the property of Fair Isaac Corporation (“FICO”). Receipt or possession of this documentation does not convey rights to disclose, reproduce, make derivative works, use, or allow others to use it except solely for internal evaluation purposes to determine whether to purchase a license to the software described in this documentation, or as otherwise set forth in a written software license agreement between you and FICO (or a FICO affiliate). Use of this documentation and the software described in it must conform strictly to the foregoing permitted uses, and no other use is permitted.