f# - How to create a "Pair" function to match against a string list? -
I was doing an exercise (scroll down) to create the pair method. / P>
I could add an integer list without any problem but for a string list an F # exception was thrown for me. F # For me it is very secret to understand what an exception means for the beginning .
This pair on fsi.exe
& gt; Add 2 = - REC Loop ACC = Give Function - | [] - & gt; ACC - | (HD1 :: HD2 :: TL) - & gt; Loop ((hd1, hd2) :: ACC) tl - list.rev (Loop [] L) - - printfn "% A" ([1..10] | pair) - printfn "% A" ([" One ";" two ";" three ";" four ";" five "] | gt; pairs) ;; REC Loop ACC = Function ----------------------- ^ stdin (2,24): Warning FS0025: This expression matches the incomplete pattern. For example, the value '[_]' will not be matched by the val pair: 'a list - & gt; ('A *' a) list [(1, 2); (3, 4); (5, 6); (7, 8); (9, 10)] Microsoft.FSharp.Core.MatchFailureException: The exception of the type 'Microsoft.FSharp.Core.MatchFailureException' was thrown. FSI_0002.clo@2T.Invoke (List 1AC, List `1 _arg1) on FSI_0002.pair [T] (List` 1L) & lt; Startup code $ FSI_0002 & gt; $ FSI_0002._main () stopped due to error then pair works on integer version
and the signature of the function
val pair: 'a list - & gt; ('A *' a) List Indicates that pair runs on the normal list.
Question: [ANSWER] (My version)
< > Else The case (_) did the trick.
and warnings are also taken care of.
Give two pairs = REC loop ACC = function // | [] - & gt; ACC | (HD1 :: HD2 :: TL) - & gt; Loop ((HD1, HD2) :: ACC) TL | _ - & gt; ACC List. Red (Loop [] L) Printfun "% A" ([1..10] | pair) pair of printfun "% A" (["a"; "two"; "three"; "four"; "Five"] | pair) [EDIT2] Okay, I post my version of Unpair for completeness Can I use LP in (A, B) for AnApiere L = [
A :: B :: []] There is some defective benchmarking by using the solution version against me for 1 million item lists
#light open System; Let pn l = printfn "% A" l Specify the duration = startTime = DateTime.Now; Return value = F () and time = datetime. Now; Printfn "Duration (ms):% F" (end time - start time). Give the total amilicands return value = [yield one for 1.000000 (a)] Let's tl = [1 to 1.000000 in yield (A, A)] two plus 1 l = REC loop acc = function. [] | [_] - & gt; List REV ACC | H1 :: H2 :: TL - & gt; Loop ((H1, H2) :: ACC) TL Loop [] L Unpair 1L Let REC Loop ACC = Function | [] - & gt; List REV ACC | (H1, H2) :: TL - & gt; Loop (H2 :: H1 :: ACC) TL Loop [] L Two Pair 2L = REC Loop ACC = Function | (HD1 :: HD2 :: TL) - & gt; Loop ((HD1, HD2) :: ACC) TL | _ | [_] - & gt; ACList.Rave (Loop [] L) Unpear 2L = [L (for A, B)! A :: b :: []] pn (duration (funny () - gt; ll;> pair 1)) pn (period (funny) -> gt; tl;> unpair1)) pn (Funny) ->;; pair 2)) PN (period (funny) - gt; TL; & gt; Unpair 2)) Benchmark results:
solution version pair - & gt; Period (ms): 255.000000 UNPAIR - & gt; Period (ms): 840.000000 Pair my version - & gt; Period (ms): 220.000000 UNPAIR - & gt; Duration (ms): 1624.000000= "post-text" itemprop = "text">I do not think the version of your pair will work On a list of weird numbers, you have to test an odd number of ints and strings of a single number. I think your second argument for the match comes out with a list of at least two members, so you get 2 breaks Break off off 2 and enter a list with 2 entries and no match is yours The Minister.
[_] There is 1 list of items in it. You should provide that skeptic who matches him.
Comments
Post a Comment