3

I am using F# Canopy to complete some web testing. I am trying to create and load a random number with or without letters, not that important and use it to paste to my website.

The code I am currently using is

let genRandomNumbers count =
    let rnd = System.Random()
    List.init count 

let l = genRandomNumbers 1

"#CompanyName" << l()

The #CompanyName is the ID of the element I am trying to pass l into. As it stands I am receiving the error 'The expression was expected to have type string but here it has type a list.

Any help would be greatly appreciated.

Amir
  • 9,091
  • 5
  • 34
  • 46
DevLife
  • 125
  • 2
  • 9
  • Well, `l` is going to return to you a cons list of size 1. Do you want the random numbers to be strings? You'll have to map that – devshorts Feb 20 '14 at 02:03
  • Right, yes I want a random string to be returned of say 10 characters. Then the random string to be enter into my #CompanyName class field. – DevLife Feb 20 '14 at 02:06

1 Answers1

2

The << operator in canopy writes a string to the selector (I haven't used it but the documentation looks pretty clear), but your function returns a list. If you want the random string to work, you could do something like this (not tested code)

let randomNumString n = genRandomNumbers n |> List.map string |> List.reduce (+)

This maps your random list to strings then concats all the strings together using the first element as the accumulator seed. You could also do a fold

let randomNumString n = genRandomNumbers n
                         |> List.fold (fun acc i -> acc + (string i)) "" 

Putting it all together

let rand = new System.Random()

let genRandomNumbers count = List.init count (fun _ -> rand.Next())

let randomNumString n = genRandomNumbers n |> List.map string |> List.reduce (+)

"#CompanyName" << (randomNumString 1)

In general, F# won't do any type promotion for you. Since the << operator wants a string on the right hand side, you need to map your list to a string somehow. That means iterating over each element, converting the number to a string, and adding all the elements together into one final string.

devshorts
  • 8,572
  • 4
  • 50
  • 73
  • Sorry, F# is quite new to me. Do I use the code about and then add a #CompanyName" << n – DevLife Feb 20 '14 at 02:19
  • Don't worry, I updated my answer to include best practices regarding random (you don't want to keep creating it from scratch or it won't be that random), and pieced your example together. Let me know if you have any questions – devshorts Feb 20 '14 at 02:26
  • I am receavinfg and error on rand second line down This expression was expected to have type int -> 'a but here has type System.Random I also tried to remove that and I then get the error List.map string This expression was expected to have type int but here has type string -> int -> unit – DevLife Feb 20 '14 at 02:31
  • BTW I don't need a list if that's any help, that was a random code that I found on the internet that I through would give me a string. – DevLife Feb 20 '14 at 02:33
  • I updated the example. The problem was that `List.init` wants to take the number of elements to create, and a function that takes an elmeent index and returns the value to use. Since we don't care about the element index I left it unbounded with a `_`. This tells f# that you don't care about this variable and to not even assign it. Told ya I didn't test it :-P – devshorts Feb 20 '14 at 02:33
  • @user3186537 there are a lot of ways of doing this, a list is just fine for this example. :) – devshorts Feb 20 '14 at 02:34
  • 1
    Fantastic, that has worked wonderfully. Thank you very much devshorts – DevLife Feb 20 '14 at 02:41