Riccardo GUIDA wrote: > > Just to say that my original question to Ralf was a much more low level quest > for clarifications in the user guide: > > 1) A user reads: > > section 5.3 pag 149 > "An if-then-else expression always returns a value. If the else clause is > missing then the entire expression returns the unique value of Void. If both > clauses are present, the type of the value returned by if is obtained by > resolving the types of the values of the two clauses. See Section 2.10 on > page 109 for more information." (Check also the boxed text above this > statement.) > > 2) The user does not really understand the statement and, if he is motivated, > tries to experiment: > > (1) -> if 1>2 then 1 -- here he expects Void > Type: Void > (2) -> if 2>1 then 1 -- here he expects Void but gets PositiveInteger > > (2) 1 > Type: PositiveInteger > (3) -> if 2>1 then 1 else [1,2,3] -- here he expects common supertype, but > which one? > > (3) 1 > Type: PositiveInteger > (4) -> if 1>2 then 1 else [1,2,3] -- here he expects common supertype, which > one? > > (4) [1, 2, 3] > Type: List(PositiveInteger) > > 3) The user is puzzled because
The command line is not representative of FriCAS language. On command line interpreter evaluates things so can use actual values to guess what would be useful. In the process it breaks several normal rules. Try: f(x) == if (x > 1) then 1 Type: Void Time: 0 sec (7) -> f(2) Compiling function f with type PositiveInteger -> Void Type: Void Time: 0.02 (OT) = 0.02 sec (8) -> f(0) Compiling function f with type NonNegativeInteger -> Void Type: Void Time: 0.01 (OT) = 0.01 sec In both cases you now get Void. Unlike Spad compiler trying to declare type does not change the result: (9) -> f2(x : Integer) : Integer == if (x > 1) then 1 Function declaration f2 : Integer -> Integer has been added to workspace. Type: Void Time: 0 sec (10) -> f2(2) Compiling function f2 with type Integer -> Integer Conversion failed in the compiled user function f2 . Cannot convert the value from type Void to Integer . (10) -> f2(0) Conversion failed in the compiled user function f2 . Cannot convert the value from type Void to Integer . -- Waldek Hebisch -- You received this message because you are subscribed to the Google Groups "FriCAS - computer algebra system" group. To unsubscribe from this group and stop receiving emails from it, send an email to fricas-devel+unsubscr...@googlegroups.com. To post to this group, send email to fricas-devel@googlegroups.com. Visit this group at https://groups.google.com/group/fricas-devel. For more options, visit https://groups.google.com/d/optout.