views:

261

answers:

3

I hope you can help me with these exercises I have been stuck with. I am not in particular looking for the answers. Tips that help me solve them myself are just as good :)

1. Write a function implode : char list -> string so that implode s returns the characters concatenated into a string:

let implode (cArray:char list) = List.foldBack (+) 0 cArray;;

Error. I am thinking that i need to cast every char to strings before I do the above. I bet however, that there is a much simpler and better solution.

2. Write a function palindrome : string -> bool, so that palindrome s returns true if the string s is a palindrome

let isIdentical cArray1 cArray2 = (cArray1 = cArray2);;
let palinDrome (word:string) = isIdentical(word.ToCharArray(), Array.rev(word.ToCharArray()));;

I get the value:

val palinDrome : string -> (char [] * char [] -> bool), which is not what I want.

3. Write a function combinePair : int list -> int list so that combinePair xs returns the list with elements from xs combined into pairs. If xs contains an odd number of elements, then the last element is thrown away:

I have no idea for this one.

+1  A: 

For #1, it is useful to know that the string function will convert a character into a string, e.g.

let c = 'A'
let s = string c
printfn "%s" s

and then a strategy similar to your current foldBack with (+) may work. I would name the parameter cList instead of cArray.

For #2, you defined isIdentical as a curried function (that takes arguments separated by spaces), but then called it like a tupled function (with arguments in parentheses separated by a comma). As a result, you're only passing a single (tuple) argument to isIdentical. I would omit the isIdentical function entirely (the = operator serves the same purpose for structural types like arrays).

For #3, it may be useful to make some sample inputs & outputs, e.g.

let input = [1;2;3;4;5]
let output = [(1,2); (3,4)]

If you figure out what happens for e.g. lists of lengths 0, 1, 2, you can probably figure out some recursive rules here.

Brian
+4  A: 

1. homework If you want to convert a list of chars to a string value in F#, you can use new String(cArray |> Array.ofSeq). This converts the list to an array, which is then passed to a constructor of the String class. This is an approach that you would use in practice, but using fold is great for educatinal reasons. Two hints:

  • To convert char to string value, you can use the string function
  • You'll probably need to use empty string as an initial value for folding

2. homework The problem with your code is that you're calling isIdentical with a tuple as an argument - constructed using the syntax (something, something). However, your function takes the parameters as two parameters (not as a single tuple), so you need to call it with two arguments (separated by spaces).

3. homework You'll need to process the list recursively and use pattern matching to do something with the elements (to produce the list that should be returned). The following function shows how to do this - it doesn't implement any useful behavior - it just creates a copy of the list:

let rec copyList list = 
  match list with
  | [] -> [] // for empty list, return empty list
  | x::xs -> 
    // got non-empty list (the first element is 'x' and
    // the rest of the list is 'xs'), we process the rest
    // using a recursive call
    x :: (copyList xs)

You'll need to add some logic to take two elements and create the tuple, but the structure will be similar.

Tomas Petricek
A: 

I know it's perhaps too late now. But, was looking for some F# exercises and found this posting. Here is how I would solve the problems:

let implode (chlist: char list) = List.fold_left (fun str ch -> str + string(ch)) "" chlist

let palindrome str = str = new string(str.ToCharArray() |> Array.rev)

let rec combPair = function x::y::tail -> (x, y)::(combPair tail)
                          | _ -> []

Previous guys have provided good explanations. So, I guess I won't be able to add that much. I am using F# Version 1.9.6.2 on MSVS2008, BTW. If they don't work on your F#, you might want to consider compatibility issues.

Hossein HAERI