1. Phix wiki 2
- Posted by _tom (admin) Jan 24, 2021
- 967 views
- Last edited Jan 25, 2021
Routines
There are three kinds of routines: procedure, function, and type. A procedure does not return a value, a function returns a value, and a type declares and tests user defined data-types.
-- this is for action procedure hello() ? "hello" end procedure hello() -- outputs: // hello -- this returns a value function pi() return 3.14 end function -- use a function in an expression ? pi()*4*4 // 50.24 -- explicit discard is needed to ignore return {} = pi() -- this creates a user data-type type positive(number x ) if x >= 0 then return 1 else return 0 end if end type -- use to declare a variable positive ndx = 5 -- use to enforce type-checking ndx = -2 // type-check error
A function declared as part of a class works like a method in OOP style programming.
Having both procedure and function enables the interpreter to display more meaningful error messages.
Symmetrical Indexing
From a discussion on the Julia website "What’s the big deal? 0 vs 1 based indexing" comes a simple explanation:
Steven_Sagaert Dec 2016 That’s exactly it. 1-based indexing is actual indexing like in mathematics, while 0-based “indexing†isn’t indexing at all but pointer arithmetic. This comes from C where an array is just syntactic sugar for a pointer.
In symmetrical indexing items are numbered from `1` to `n` (where `n` is equal to the length of the sequence). Items can also be indexed using negative values: `-n` is the first item and `-1` is the last item.
string txt = "hello" ? txt[-1] // 'o' ? txt[3..4] // "ll" -- symmetrical indexing sequence dta = { 1, 2, "cat", {20,2} } -- 1 2 3 4 -- -4 -3 -2 -1 ? dta[ 3.. 4] // {"cat",{20,2.2}} ? dta[-2..-1] // {"cat",{20,2.2}}
- one-based indexing is intuitive
- index values are used for slicing
- intuitive symmetrical indexing and slicing
- the value zero is an obvious indexing error
- the value zero can be used for item not found
- 0'th location is a virtual head
- n+1'th location is a virtual tail
https://discourse.julialang.org/t/whats-the-big-deal-0-vs-1-based-indexing/1102
Generic operators
Operators are generic:
-- the + operator always adds atom chr = 'T' ? chr + 32 // 't' atom x = 100 ? x + 32 // 132
Operators can span strings and sequences:
string txt = "HELLO" ? sq_add( txt, 32 ) // "hello" sequence dta = { 2, 9, 34 } ? sq_mul( dta, 100 ) // {200,900,3400}
Generic routines
Sequences and strings behave alike:
string txt = "Have a nice day" ? sort(txt) // " Haaacdeeinvy" ? shuffle(txt) // "dnve aaiecH ya" ? reverse(txt) // "yad ecin a evaH" sequence lst = { "hamster", "dog", "fish", "cat" } ? sort(lst) // {"cat","dog","fish","hamster"} ? shuffle(lst) // {"fish","dog","cat","hamster"} ? reverse(lst) // {"cat","fish","dog","hamster"}
You can use the same skills for any kind of data values.
Equivalence of strings and sequences
Each character is represented by a number value. A string is actually a sepecial case of a sequence.
? sq_sqrt( "Hello" ) // {8.485281374,10.04987562,10.39230485,10.39230485,10.53565375} ? sq_sqrt( {'H','e','l','l','o'} ) // {8.485281374,10.04987562,10.39230485,10.39230485,10.53565375} ? sq_sqrt( {72, 101, 108, 108,111 } ) // {8.485281374,10.04987562,10.39230485,10.39230485,10.53565375}
2. Re: Phix wiki 2
- Posted by euphoric (admin) Jan 24, 2021
- 930 views
string txt = "hello" ? txt[3..4] // "lo"
Should that one should be "ll"?
3. Re: Phix wiki 2
- Posted by _tom (admin) Jan 24, 2021
- 941 views
string txt = "hello" ? txt[3..4] // "lo"
Should that one should be "ll"?
both - - and / / are line comments in phix
_tom
4. Re: Phix wiki 2
- Posted by euphoric (admin) Jan 24, 2021
- 940 views
string txt = "hello" ? txt[3..4] // "lo"
Should that one should be "ll"?
both - - and / / are line comments in phix
_tom
My question was not at all about comment marks.
"hello"[3..4] is "ll" not "lo"
5. Re: Phix wiki 2
- Posted by irv Jan 24, 2021
- 942 views
"A procedure is packages action," doesn't make a lot of sense.
6. Re: Phix wiki 2
- Posted by _tom (admin) Jan 25, 2021
- 932 views
My apology to euphoric and Irv.
I was 'multitasking' by watching football, which was not the best use of my time.
It now says "a procedure does not return a value"
thank you
be well
_tom
7. Re: Phix wiki 2
- Posted by petelomax Jan 27, 2021
- 853 views
atom chr = 'T' ? chr + 32 // 't'
Erm yeah, but the output is 116...