| | |
Functional Programming in CAML light
![]() |
•
•
Join Date: Feb 2005
Posts: 1
Reputation:
Solved Threads: 0
Hi all, I've been working hard at the programming, but have come to a stand still.
Anyway, I'm having trouble with lists. This is just one of many many many questions I have, so I will keep it down to the minimum.
I have to state whether a list is true or false, based on the integers in the list.
e.g
[1;2;3;4;5;6;7;8] would come up as TRUE#
[1;2;3;4;5;6;7;8;8;7] would come up as FALSE as this list contains two 7's and two 8's.
I have had a few ideas as to how to do this problem, and one of them is as a follows:-
If I could somehow subtract every value in the TL of the list from the HD, this would mean that if the list were as follows:-
[1;2;3;4;1], then:
1-2 = -1
1-3 = -2
1-4 = -3
1-1= 0
so if the value of the calculation is 0, then FALSE is displayed as a result.
Now I would need to do thsi recursivly, in order to take the hd(tl(tl(tl(tl(x))))
etc.....
So it would subtract every number from a 'HD', and so even if the following list was used, the recursive function would work it out:-
[1;2;3;4;5;6;7;7]
I hope that makes sense. Basically I'm having alot of trouble coding it, but I thinik that the method of solving the problem I have come up with will work.
Can anyone give me some advice and/or help?
Thanks,
Stuart.
Anyway, I'm having trouble with lists. This is just one of many many many questions I have, so I will keep it down to the minimum.
I have to state whether a list is true or false, based on the integers in the list.
e.g
[1;2;3;4;5;6;7;8] would come up as TRUE#
[1;2;3;4;5;6;7;8;8;7] would come up as FALSE as this list contains two 7's and two 8's.
I have had a few ideas as to how to do this problem, and one of them is as a follows:-
If I could somehow subtract every value in the TL of the list from the HD, this would mean that if the list were as follows:-
[1;2;3;4;1], then:
1-2 = -1
1-3 = -2
1-4 = -3
1-1= 0
so if the value of the calculation is 0, then FALSE is displayed as a result.
Now I would need to do thsi recursivly, in order to take the hd(tl(tl(tl(tl(x))))
etc.....
So it would subtract every number from a 'HD', and so even if the following list was used, the recursive function would work it out:-
[1;2;3;4;5;6;7;7]
I hope that makes sense. Basically I'm having alot of trouble coding it, but I thinik that the method of solving the problem I have come up with will work.
Can anyone give me some advice and/or help?
Thanks,
Stuart.
•
•
Join Date: Sep 2005
Posts: 133
Reputation:
Solved Threads: 58
Hi!
Ok, this is a very old thread, and I guess you have found a solution already.
Anyway, here is how I would do it (I hope I understood you right, you just want to know if the list contains only unique elements or not, right?):
Regards, mawe
Ok, this is a very old thread, and I guess you have found a solution already.
Anyway, here is how I would do it (I hope I understood you right, you just want to know if the list contains only unique elements or not, right?):
let l = [1;2;3;4];; let rec check l = match l with | [] -> true | h::t -> if (List.mem h t) then false else (check t) ;; if (check l) then print_endline "no doubles" else print_endline "doubles found"
Regards, mawe
![]() |
Similar Threads
- Is it possible to build a time machine through the use of programming? (Geeks' Lounge)
- Object Oriented Programming (Computer Science)
- Coolest programming language? (IT Professionals' Lounge)
- Haskell (Computer Science)
- Installing Caml light on windowxp (Legacy and Other Languages)
Other Threads in the Legacy and Other Languages Forum
- Previous Thread: Windows XP & QBASIC
- Next Thread: Simple Download To Fix QBasic on WinXP
| Thread Tools | Search this Thread |





