<META name=ProgId content=Word.Document>

<META name=Generator content="Microsoft Word 12">

<META name=Originator content="Microsoft Word 12"><LINK rel=File-List href="file:///C:%5CUsers%5CADMINI%7E1%5CAppData%5CLocal%5CTemp%5Cmsohtmlclip1%5C01%5Cclip_filelist.xml"><LINK rel=themeData href="file:///C:%5CUsers%5CADMINI%7E1%5CAppData%5CLocal%5CTemp%5Cmsohtmlclip1%5C01%5Cclip_themedata.thmx"><LINK rel=colorSchemeMapping href="file:///C:%5CUsers%5CADMINI%7E1%5CAppData%5CLocal%5CTemp%5Cmsohtmlclip1%5C01%5Cclip_colorschememapping.xml">

<STYLE>

<!--

/* Font Definitions */

@font-face

{font-family:"Cambria Math";

panose-1:2 4 5 3 5 4 6 3 2 4;

mso-font-charset:1;

mso-generic-font-family:roman;

mso-font-format:other;

mso-font-pitch:variable;

mso-font-signature:0 0 0 0 0 0;}

@font-face

{font-family:Calibri;

panose-1:2 15 5 2 2 2 4 3 2 4;

mso-font-charset:0;

mso-generic-font-family:swiss;

mso-font-pitch:variable;

mso-font-signature:-520092929 1073786111 9 0 415 0;}

/* Style Definitions */

p.MsoNormal, li.MsoNormal, div.MsoNormal

{mso-style-unhide:no;

mso-style-qformat:yes;

mso-style-parent:"";

margin-top:0in;

margin-right:0in;

margin-bottom:10.0pt;

margin-left:0in;

line-height:115%;

mso-pagination:widow-orphan;

font-size:11.0pt;

font-family:"Calibri","sans-serif";

mso-ascii-font-family:Calibri;

mso-ascii-theme-font:minor-latin;

mso-fareast-font-family:Calibri;

mso-fareast-theme-font:minor-latin;

mso-hansi-font-family:Calibri;

mso-hansi-theme-font:minor-latin;

mso-bidi-font-family:"Times New Roman";

mso-bidi-theme-font:minor-bidi;}

.MsoChpDefault

{mso-style-type:export-only;

mso-default-props:yes;

mso-ascii-font-family:Calibri;

mso-ascii-theme-font:minor-latin;

mso-fareast-font-family:Calibri;

mso-fareast-theme-font:minor-latin;

mso-hansi-font-family:Calibri;

mso-hansi-theme-font:minor-latin;

mso-bidi-font-family:"Times New Roman";

mso-bidi-theme-font:minor-bidi;}

.MsoPapDefault

{mso-style-type:export-only;

margin-bottom:10.0pt;

line-height:115%;}

@page Section1

{size:8.5in 11.0in;

margin:1.0in 1.0in 1.0in 1.0in;

mso-header-margin:.5in;

mso-footer-margin:.5in;

mso-paper-source:0;}

div.Section1

{page:Section1;}

-->

</STYLE>

I want to change the shiftArray to be able to use on array of tuples, but I can not figure it out.

The only reason you cannot use tuples with your version is that you have added type annotations that constrain it to work only with floats. Just replace the float in your type annotations with an underscore and it should work. Alternatively, use Array.length fArray instead of fArray.Length and do away with the type annotations altogether!

FWIW, arrays in F# are mutable (but not extensible) and there is absolutely nothing wrong with using them: arrays are great.

By on 3/17/2010 5:13 PM ()

<META name=ProgId content=Word.Document>

<META name=Generator content="Microsoft Word 12">

<META name=Originator content="Microsoft Word 12"><LINK rel=File-List href="file:///C:%5CUsers%5CADMINI%7E1%5CAppData%5CLocal%5CTemp%5Cmsohtmlclip1%5C01%5Cclip_filelist.xml"><LINK rel=themeData href="file:///C:%5CUsers%5CADMINI%7E1%5CAppData%5CLocal%5CTemp%5Cmsohtmlclip1%5C01%5Cclip_themedata.thmx"><LINK rel=colorSchemeMapping href="file:///C:%5CUsers%5CADMINI%7E1%5CAppData%5CLocal%5CTemp%5Cmsohtmlclip1%5C01%5Cclip_colorschememapping.xml">

<STYLE>

<!--

/* Font Definitions */

@font-face

{font-family:"Cambria Math";

panose-1:2 4 5 3 5 4 6 3 2 4;

mso-font-charset:1;

mso-generic-font-family:roman;

mso-font-format:other;

mso-font-pitch:variable;

mso-font-signature:0 0 0 0 0 0;}

@font-face

{font-family:Calibri;

panose-1:2 15 5 2 2 2 4 3 2 4;

mso-font-charset:0;

mso-generic-font-family:swiss;

mso-font-pitch:variable;

mso-font-signature:-520092929 1073786111 9 0 415 0;}

/* Style Definitions */

p.MsoNormal, li.MsoNormal, div.MsoNormal

{mso-style-unhide:no;

mso-style-qformat:yes;

mso-style-parent:"";

margin-top:0in;

margin-right:0in;

margin-bottom:10.0pt;

margin-left:0in;

line-height:115%;

mso-pagination:widow-orphan;

font-size:11.0pt;

font-family:"Calibri","sans-serif";

mso-ascii-font-family:Calibri;

mso-ascii-theme-font:minor-latin;

mso-fareast-font-family:Calibri;

mso-fareast-theme-font:minor-latin;

mso-hansi-font-family:Calibri;

mso-hansi-theme-font:minor-latin;

mso-bidi-font-family:"Times New Roman";

mso-bidi-theme-font:minor-bidi;}

.MsoChpDefault

{mso-style-type:export-only;

mso-default-props:yes;

mso-ascii-font-family:Calibri;

mso-ascii-theme-font:minor-latin;

mso-fareast-font-family:Calibri;

mso-fareast-theme-font:minor-latin;

mso-hansi-font-family:Calibri;

mso-hansi-theme-font:minor-latin;

mso-bidi-font-family:"Times New Roman";

mso-bidi-theme-font:minor-bidi;}

.MsoPapDefault

{mso-style-type:export-only;

margin-bottom:10.0pt;

line-height:115%;}

@page Section1

{size:8.5in 11.0in;

margin:1.0in 1.0in 1.0in 1.0in;

mso-header-margin:.5in;

mso-footer-margin:.5in;

mso-paper-source:0;}

div.Section1

{page:Section1;}

-->

</STYLE>

I want to change the shiftArray to be able to use on array of tuples, but I can not figure it out.

The only reason you cannot use tuples with your version is that you have added type annotations that constrain it to work only with floats. Just replace the float in your type annotations with an underscore and it should work. Alternatively, use Array.length fArray instead of fArray.Length and do away with the type annotations altogether!

FWIW, arrays in F# are mutable (but not extensible) and there is absolutely nothing wrong with using them: arrays are great.

That '0.0' in the else would still force the function to take only Array<float>*float, even when there is no annotation. it can be replace with 'avalue' given the logic though.

The reason I suggested not to use Array was not that there is anything wrong with it but if it really is needed. Most of the time, it is not.

I suspect that the '0.0' was inserted as a 'placeholder' which points to a typical imperative style and would have no chance of happening if newArray is immutable(that thought of a placeholder then modify it later).

By on 3/17/2010 6:40 PM ()
1
2
3
4
 

#r "FSharp.PowerPack"
let f a e = if Array.contains e a then a else Array.concat [a.[1..(a.Length - 1)];[| e |] ]

once again, array is usually the wrong choice of data structure(95% of the time in F# unless you know why you need it).

there are also lots of optimization that needs to be done(this is extremely inefficient but is a straight translation of your code). You effectively want an ordered(by insertion order) Set.

A side note, by looking at my code, I am wondering if it is more natural to have the array index to be 1-based instead of 0-based in F#(or even C#/VB or other high level language). Lua table is 1-based, 0-based seems like a quirk from the C/Assembly days where it was used as an offset to a pointer. Below is nicer IMO

1
2
3
4
5
 

let f a e = if Array.contains e a then a else Array.concat [a.[2..a.Length];[| e |] ]

By on 1/28/2010 1:55 PM ()
1
2
3
4
 

#r "FSharp.PowerPack"
let f a e = if Array.contains e a then a else Array.concat [a.[1..(a.Length - 1)];[| e |] ]

once again, array is usually the wrong choice of data structure(95% of the time in F# unless you know why you need it).

there are also lots of optimization that needs to be done(this is extremely inefficient but is a straight translation of your code). You effectively want an ordered(by insertion order) Set.

A side note, by looking at my code, I am wondering if it is more natural to have the array index to be 1-based instead of 0-based in F#(or even C#/VB or other high level language). Lua table is 1-based, 0-based seems like a quirk from the C/Assembly days where it was used as an offset to a pointer. Below is nicer IMO

1
2
3
4
5
 

let f a e = if Array.contains e a then a else Array.concat [a.[2..a.Length];[| e |] ]

Hi, I used VS2010 Beta2 and the above code worked well. However, my

PC got virus, so I re-installed my PC, and I installed VS2010 RC1 with

F# PowerPack 1.9.9.9 but the above code just does not work.

> #r "FSharp.PowerPack"
let f a e = if Array.contains e a then a else Array.concat [a.[1..(a.Length - 1)];[| e |] ];;

--> Referenced 'C:\Program Files (x86)\FSharpPowerPack-1.9.9.9\bin\FSharp.PowerPack.dll'

let f a e = if Array.contains e a then a else Array.concat [a.[1..(a.Length - 1)];[| e |] ];;
---------------------^^^^^^^^

stdin(2,22): error FS0039: The value, constructor, namespace or type 'contains' is not defined
>

It is impossible to find the Array.contains when I used intellisense.
Any idea? How I can reuse the above code, or change the code, so it will work in the new development environment: VS2010 RC1 + F# PowerPack 1.9.9.9.
Thanks,

By on 3/13/2010 1:25 PM ()

You can replace contains with

1
Array.exists ((=) e) a

(that is,

1
Array.exists (fun e' -> e' = e) a)

.

By on 3/14/2010 4:24 AM ()

Hi, Kha:
Thank you very much for your help.
But for me, it is difficult to understand it, I think Array.contains seems to be better in this case, as it is easier to understand.
Right?
Thanks again for your help!

By on 3/14/2010 2:17 PM ()

you can always write short helper functions(or even extension methods)

1
2
3
4
5
 

let contains a e = Array.exists (fun x -> x = e) a
let f a e = if contains a e then a else Array.concat [a.[2..a.Length];[| e |] ]

Though I am surprised that the behaviour under 2010 is different from the 2008 addin. I am assuming the same version of F# should behave exactly the same.

By on 3/14/2010 6:42 PM ()

Hello, Gary:
Thank you very much for your code.
Thanks for your help.
By the way, how do you know if the code is inefficient, do you check the compiled code or using something else?
I think I know most of the data structure in F# is immutable, but array is mutable, since my program needs to change the data, so I use array often.

By on 1/28/2010 2:28 PM ()

Array in F# is IMMUTABLE. They never changed once constructed. You are not modifying an array, you are creating a new array that meet your requirement. You use it the following way.

f (f [|1;2;3|] 4) 5

Look up the ResizeArray which I believe is a MUTABLE array.

Though I once again suggest that you get a better understanding about the concept of mutable/immutable and functional programming in general before you attempt to code F# as everything in F# is by default immutable. In order to get your desired behaviour, you should not be using any data structure in F# but only .NET classes.

By on 1/28/2010 2:59 PM ()

Array is mutable in that you can reset element values in place without creating a new array. It is not mutable in the sense that it is of a fixed size.

By on 2/2/2010 1:03 PM ()
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