Hi,

I can't guess what you wanted to do with the "List function" of l - but there are several ways.

First you must understand that your

1
2
double1

IS typesave (it's just generalized to be generic).

Second take a look at your problem - I guess you want to write something like

1
2
l.Count

or whatever - so of course the complier don't know the type and will complain. So either way you use the solution provided by the other answer OR you go the functional road and use the functions from the List module (so you would write

1
2
l |> List.Length

or

1
2
List.Length l

or whatever and voila - the compiler will find the function to bei "List<'a> -> List<'a> * List<'a>".

I hope this helps a bit.

By on 5/5/2010 5:58 AM ()

Hi,

I can't guess what you wanted to do with the "List function" of l...

I'm trying to write something like:

1
2
3
4
5
6
7
// Split the list into two separate lists divided by token "p"
let rec split (l: list<token>) (p: token) (leftRetVal: list<token>) = 
    if (l.Head.m_tokenType = p.m_tokenType) then
        (leftRetVal, l.Tail)
    else
        let augmentedLeftList = leftRetVal @ l.Head::[]
        split l.Tail p augmentedLeftList

The above code "mostly" works, but it really should split on the "last" occurance of token instead of the first. I'm guessing there is a List.Rev function somewhere (See Mr. CKoenig's comments about the List Module Below)?

First you must understand that your

1
2
double1

IS typesave (it's just generalized to be generic).

I think Mr. CKoenig is right. The code probably is typesafe. I'm just not used to F#/SML figuring out types for me.

I guess you want to write something like

1
2
l.Count

Yup, I just wanted Intellisense to work, because I am lazy. If I wasn't so lazy, maybe I would have less bogers in my nose.

So either way you use the solution provided by the other answer OR you go the functional road and use the functions from the List module (so you would write

1
2
l |> List.Length

or

1
2
List.Length l

or whatever and voila - the compiler will find the function to bei "List<'a> -> List<'a> * List<'a>".

I didn't know I could access the List Module via List.Length. I wonder what else is in that module that I might be missing out on. Could there be a List.Split function? Or a List.Explode? One would think that simply dereferencing a List Object would provide access to such methods...

I hope this helps a bit.

Mr. CKoenig's response actually helps quite a bit as I didn't know all of the cool stuff there was in the List Module. I don't have an F# book and I lost my SML Book so I'm kind of guessing around right now. One of the reasons I am trying F# right now is because it is still free [:D]

Thanks Mr. CKoenig!!!

By on 5/6/2010 9:21 PM ()

Be sure to check out all the modules in the Microsoft.FSharp.Collections namespace. In addition to List, there are modules for sequence operations (Seq), arrays of various types, etc.

-Neil

By on 5/7/2010 7:40 AM ()

I didn't know I could access the List Module via List.Length. I wonder what else is in that module that I might be missing out on.

You can find documentation on F# Lists here:

[link:msdn.microsoft.com]

By on 5/7/2010 5:37 AM ()

It's a bit confusing, but you've actually annotated the <i>return<i> type of the function double2, and not the argument's type. The compiler is telling you that it knows that the function is supposed to return a list (based on the annotated return type), but as written it clearly returns a pair, which is incompatible. Instead, you want: <code lang=fsharp> let rec double2 (l:list<token>) = (l,l) </code>

By on 5/4/2010 9:34 PM ()

<quote> ...you've actually annotated the <i>return<i> type of the function double2, and not the argument's type. The compiler is telling you that it knows that the function is supposed to return a list (based on the annotated return type), but as written it clearly returns a pair, which is incompatible. Instead, you want: <code lang=fsharp> let rec double2 (l:list<token>) = (l,l) </code> </quote> Thanks! That fix'd er up!!!

By on 5/6/2010 9:00 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