PDA

View Full Version : Haskell



sb89
12-04-11, 14:41
Anyone here know any Haskell? I've got a function (working correctly)


function a b [] = False
function a b ((m,n):xs)
.....
......
blah blah

Obviously this function takes 3 inputs ( a, b and a list). Is there anyway to make the function use the list without having to pass it as a parameter?

andyn
12-04-11, 14:46
Nobody in the real world knows Haskell. Student? :P

I did a course on the original functional language, Miranda, in university (where the lecturer was David Turner, the guy who basically invented functional programming languages). However it's not been very relevant in the 15 years since leaving education ;).

sb89
12-04-11, 14:49
Nobody in the real world knows Haskell. Student? :P

I did a course on the original functional language, Miranda, in university (where the lecturer was David Turner, the guy who basically invented functional programming languages). However it's not been very relevant in the 15 years since leaving education ;).

Yeah. It's one of my assignments. I've got it all working (I'm not here looking for it to be done for me) it's just that I'm not too happy with the way it looks.

I found it mind boggling (still do). I guess it's because I've been programming using imperative languages for like 6 years while I've studied functional languages for 3 months.

andyn
12-04-11, 14:55
Yeah. It's one of my assignments. I've got it all working (I'm not here looking for it to be done for me) it's just that I'm not too happy with the way it looks.

I found it mind boggling (still do). I guess it's because I've been programming using imperative languages for like 6 years while I've studied functional languages for 3 months.

Yeah they are an interesting change from procedural and OO programming, and probably a good teaching tool, just bog-all use for solving most real-world problems outside of academia :).

However, to answer your question, I would have thought that the the haskell way to do it was to use the list parameter. If you get the function to use the list without it having been passed as a parameter, then effectively you are talking about a global variable, which is evil and wrong. Ideally a function should be a 'black box' which returns values based solely on it's argument(s).

kataras
12-04-11, 15:45
ive done a haskell unit at uni and as far as i rememeber if you want for your function to work with the list (or any other var for that matter), you will have to pass list to it as a parameter.
of course might have completely brain-farted just there but i think you thats how it works

Do you have specific problem in mind?

Myn
13-04-11, 12:43
You can have a list as a local variable but you would need to initialise it within the function. The only way to use a list that has been initialised outside of the function 'X' is to use a global variable.

Alternatively return the list from another function 'Y' and call 'Y' from within 'X'...I can't remember any Haskell at all so some terrible pseudocode:

function_y

list_variable = aList;
return list_variable;


function_x (a , b)

local_list_variable = function_y; Declares the variable and initialises it.
Bla;
Bla;

By far the best way is to pass it as an argument to the function though. :)