Hi,
I'm not sure if I understand what exactly do you mean by "anagram program", but I'm sure we'll be happy to help you with porting the Java code. Can you please send the parts of the program that you have troubles porting to F# - If you already started, then just send the F# code (even if it doesn't work), so we can fix it!

Regarding your second question - The F# Interactive Console is very useful for quickly testing some code and Visual Studio is a priceless for writing larger projects. The best thing to do is (IMHO) to use Visual Studio as an editor and use the F# Interactive Console integrated in Visual Studio for testing - You can select any block of F# code in VS and execute it in the integrated FSI console using "Alt + Enter".

By on 8/25/2007 3:33 PM ()

Hi tomasp,

here is my java program but in f# i did nothing worth tobe mentioned.

public class anagram{

public static boolean areAnagrams(String string1, String string2){
String workingCopy1 = removeunwanted(string1);
String workingCopy2 = removeunwanted(string2);
workingCopy1 = workingCopy1.toLowerCase();
workingCopy2 = workingCopy2.toLowerCase();
workingCopy1 = sort(workingCopy1);
workingCopy2 = sort(workingCopy2);
return workingCopy1.equals(workingCopy2);
}

protected static String removeunwanted(String string){
int i, len = string.length();
StringBuffer dest = new StringBuffer(len);
char c;
for (i = (len - 1); i >= 0; i--){
c = string.charAt(i);
if (Character.isLetterOrDigit(c)){
dest.append(c);
}
}
return dest.toString();
}

protected static String sort(String string){
int length = string.length();
char[] charArray = new char[length];
string.getChars(0, length, charArray, 0);
java.util.Arrays.sort(charArray);
return new String(charArray);
}

public static void main(String[] args){
String string1 = "Silent";
String string2 = "Listen";
System.out.println();
System.out.println("Testing whether the following strings are anagrams:");
System.out.println("String 1: " + string1);
System.out.println("String 2: " + string2);
System.out.println();
if (areAnagrams(string1, string2)){
System.out.println(" anagrams!");
}
else{
System.out.println(" not anagrams!");
}
System.out.println();
}
}

thanks.

gordon

By on 8/25/2007 7:08 PM ()

Is this what you're looking for?

let areAnagrams s1 s2 =
let charsIn (s : string) =
let sChars = List.of_array (s.ToCharArray())
List.sort Char.compare sChars
charsIn s1 = charsIn s2

> areAnagrams "pet" "top";;
val it : bool = false
> areAnagrams "pot" "top";;
val it : bool = true

By on 8/25/2007 8:43 PM ()

thanx ravx but i modified the program to accept a list of strings is this syntax ok:

let areAnagrams =List[]

By on 8/26/2007 11:50 AM ()

Gordon,

Here's a function that will work with a list of words. If you pass it an empty list or a list with just one word, it returns false. Otherwise it returns true only if all of the words that you passed it are anagrams of each other.

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
 

#light
let areAnagrams words =
    let charsIn (s : string) =
        let sChars = List.of_array (s.ToCharArray())
        List.sort Char.compare sChars
    match words with
    | w1 :: (w2 :: ws) as ws' ->
        let charsInHd = charsIn w1
        List.for_all (fun x -> charsIn x = charsInHd) ws'
    | _ -> false

val areAnagrams : string list -> bool

> areAnagrams [];;
val it : bool = false
> areAnagrams ["paternal"];;
val it : bool = false
> areAnagrams ["paternal"; "prenatal"];;
val it : bool = true
> areAnagrams ["paternal"; "prenatal"; "parental"];;
val it : bool = true
> areAnagrams ["paternal"; "prenatal"; "parental advisory"];;
val it : bool = false
By on 8/27/2007 4:35 AM ()
IntelliFactory Offices Copyright (c) 2011-2012 IntelliFactory. All rights reserved.
Home | Products | Consulting | Trainings | Blogs | Jobs | Contact Us | Terms of Use | Privacy Policy | Cookie Policy
Built with WebSharper