Why does this code not give a concatenate str+list Type runtime error? - join

res=[]
res+=''.join(['a','b'])
print(res)
#output>>>['a', 'b']
but when I do:
res=[]
res=res+''.join(['a','b'])
print(res)
#it gives type error as expected

Related

a = "stackoverflow" does not work in QBasic

The qbasic code returns a type mismatch error.
a="StackOverflow"
print left$(a,5)
print right$(a,8)
What is the cause of this error and how can I rectify it?
The error is caused by the way you have named the variable.
"StackOverflow" is a string and cannot be assigned to variables of any other type.
In Qbasic, string variables must end with a $ symbol. So try a$ instead of a.
So try this code instead.
a$="StackOverflow"
print left$(a$,5)
print right$(a$,8)
You could define the variable as string first:
DIM a AS STRING
a = "StackOverflow"
PRINT LEFT$(a, 5)
PRINT RIGHT$(a, 8)

ContainsKey returns unit and not bool?

It's odd to me that the following code throws a compile time error. I'm not sure why ContainsKey is returning unit. The documentation says it returns bool.
open System.Collections.Generic
let mydict = new Dictionary<string,'a>()
if(mydict.ContainsKey "mykey") then
mydict.["mykey"] = newkey
error FS0001: This expression was expected to have type
'bool' but here has type
'unit'
Am I missing something here?
if is an expression so both branches must have the same type. If the else branch is not specified an empty one of type unit is inserted. This means your then branch must also have type unit. However mydict.["mykey"] = newkey has type bool. If you want to insert a new value for mykey you should use <- instead of =:
if(mydict.ContainsKey "mykey") then
mydict.["mykey"] <- newkey

Why do I get an error that sprintf is incompatible with type string? [duplicate]

I feel like a total noob for having to ask this but it's got me stumped.
I set a format string like this:
let fs = "This is my format test %s"
Then I attempt to use it like so:
let s = sprintf fs "testing"
When I do so I get this error:
//stdin(26,17): error FS0001: The type 'string' is not compatible with the type 'Printf.StringFormat<('a -> 'b)>'
So I then tried this:
let s = sprintf (Printf.StringFormat fs) "test"
to which the REPL responded:
//stdin(28,18): error FS1124: Multiple types exist called 'StringFormat', taking different numbers of generic parameters. Provide a type instantiation to disambiguate the type resolution, e.g. 'StringFormat<_>'.
So I then tried this:
let s = sprintf (Printf.StringFormat<string> fs) "test"
And I get this:
//stdin(29,18): error FS0001: The type ''a -> 'b' does not match the type 'string'
Am I missing something painfully obvious? This is using F# 3.0 on the Mac from the Xamarin Studio F# Interactive Window.
So you actually need to create a StringFormat which has a function type as follows
> sprintf (Printf.StringFormat<string->string>("Hello %s")) "World";;
val it : string = "Hello World"
In Section 6.3.16 of the spec, an example of this is shown.

How Do I Use A Variable As The Formatting String With Sprintf?

I feel like a total noob for having to ask this but it's got me stumped.
I set a format string like this:
let fs = "This is my format test %s"
Then I attempt to use it like so:
let s = sprintf fs "testing"
When I do so I get this error:
//stdin(26,17): error FS0001: The type 'string' is not compatible with the type 'Printf.StringFormat<('a -> 'b)>'
So I then tried this:
let s = sprintf (Printf.StringFormat fs) "test"
to which the REPL responded:
//stdin(28,18): error FS1124: Multiple types exist called 'StringFormat', taking different numbers of generic parameters. Provide a type instantiation to disambiguate the type resolution, e.g. 'StringFormat<_>'.
So I then tried this:
let s = sprintf (Printf.StringFormat<string> fs) "test"
And I get this:
//stdin(29,18): error FS0001: The type ''a -> 'b' does not match the type 'string'
Am I missing something painfully obvious? This is using F# 3.0 on the Mac from the Xamarin Studio F# Interactive Window.
So you actually need to create a StringFormat which has a function type as follows
> sprintf (Printf.StringFormat<string->string>("Hello %s")) "World";;
val it : string = "Hello World"
In Section 6.3.16 of the spec, an example of this is shown.

list_to_float error

I understand that list_to_float("123") gives a badarg error but why list_to_float(["12.34"]) gives also the same error ?
Try
list_to_float("12.34")
list_to_float accepts a String and it returns a float whose text representation is the String.

Resources