Programming C, C++, Java, PHP, Ruby, Turing, VB
Computer Science Canada 
Programming C, C++, Java, PHP, Ruby, Turing, VB  

Username:   Password: 
 RegisterRegister   
 Quick Functions Questions
Index -> Programming, Turing -> Turing Help
View previous topic Printable versionDownload TopicSubscribe to this topicPrivate MessagesRefresh page View next topic
Author Message
richcash




PostPosted: Thu Sep 07, 2006 11:24 pm   Post subject: Quick Functions Questions

1. (I've had this question for a while, but just assumed the answer. Now I want to verify it.) I never knew that predefined subroutines were ANY different from the functions and procedures that we create in the code, but apparently they are. For some reason this can't be done in Turing :
code:
function double_after (n : real, function f (n : real) : real) : real
    result f (n) * 2
end double_after
put double_after (9, sqrt)

So, instead I have to do this :
code:
function double_after (n : real, function f (n : real) : real) : real
    result f (n) * 2
end double_after
function Sqrt (n : real) : real
    result n ** 0.5
end Sqrt
put double_after (9, Sqrt)

Is it true that there is no way to pass a predefined function as a parameter?

2. If I have an array of functions, I don't know how to directly assign each function (I'm sure there is a way!). Instead I have to make a separate function for each element (function) of the array and assign them individually, which gets tedious when you have over 5 functions. This is how I currently do it :
code:
var functions : array 1 .. 1 of function f (n : real) : real
function f (n : real) : real
    result n ** 2 + 5
end f
functions (1) := f
put functions (1) (3)

I'm sure there is a better way!
And, as is similar in #1, I can not assign functions (1) with a predefined function like sqrt (just verifying!)

Any help appreciated to any of my questions!
Sponsor
Sponsor
Sponsor
sponsor
TokenHerbz




PostPosted: Fri Sep 08, 2006 12:43 am   Post subject: (No subject)

i dont think you can call fcn's with fcn's, tho i could be mistaken. i try not to over complicate things, and would look for another rout myself.

As for array's of fcn's, i assume the objects will all be resembling? Class's would come into play here.
Cervantes




PostPosted: Fri Sep 08, 2006 7:59 am   Post subject: (No subject)

TokenHerbz wrote:
i dont think you can call fcn's with fcn's, tho i could be mistaken. i try not to over complicate things, and would look for another rout myself.

As for array's of fcn's, i assume the objects will all be resembling? Class's would come into play here.

TokenHerbz, before responding to a post like this, you should read the tutorial on this: [url=]Turing as a Functional Programming Language[/url].


Interesting observations, richcash. It seems you're right in that we can't use most predefined functions for this. However, it seems we can use the ones that are written in Turing. Str.Upper and Math.Distance, for instance, can be used here, since they are written in Turing code. The functions that are written in C and referenced using external cannot be used, it seems.

As for your next question, you could define your functions a lot easier if we had anonymous functions. Take this ruby code:

Ruby:

functions = Array.new(5) {|i| lambda {|x| x ** 2 + i}}
puts functions[2][5]    # Outputs 5^2 + 2 = 27


Without anonymous functions, this sort of power is beyond Turing, to the best of my knowledge.
richcash




PostPosted: Fri Sep 08, 2006 11:32 am   Post subject: (No subject)

Quote:
i dont think you can call fcn's with fcn's, tho i could be mistaken.

You are. As Cervantes said, read his tutorial. Besides, the last two source codes that I provided do work.

Thanks, Cervantes for verifying. And as for #2, that's not good, maybe Holtsoft will fix it one day. Anyway, Ruby sure is great, I'm gonna learn it for sure after Java!
Andy




PostPosted: Fri Sep 08, 2006 4:28 pm   Post subject: (No subject)

Turing doesnt really have distinct software layers.. so there isnt really a need to impliment callbacks
richcash




PostPosted: Sat Sep 09, 2006 1:10 pm   Post subject: (No subject)

Yeah true, not that I really know what callbacks are (reads at wikipedia), but at least if they had any way to assign an element of an array of functions. At least let us do this :
code:
var functions : array 1 .. 10 of /*keyword*/ function f (x : real) : real
functions (1) (x : real) : real
%body and result
end functions (1)

I know it's conceptually wrong, because the functions (1) has already been declared, but then let us put a keyword before function in our array to show it will be decalred again later. I just don't like having ten useless lines of declarations : functions (1) := Sqrt, functions (2) := sine, etc.
Display posts from previous:   
   Index -> Programming, Turing -> Turing Help
View previous topic Tell A FriendPrintable versionDownload TopicSubscribe to this topicPrivate MessagesRefresh page View next topic

Page 1 of 1  [ 6 Posts ]
Jump to:   


Style:  
Search: