F# solver for newspaper puzzle

9 views
Skip to first unread message

Matthew Moloney

unread,
Sep 5, 2011, 9:50:21 AM9/5/11
to Martin Paulo, scala-tutori...@googlegroups.com
Hi folks,

I finished the first prototype in F#, I will translate it to Scala at a later date. 

I'm not sure if the correct result is "Trouble Foot" or "Tearful" "Bozo" :)

This style of coding isn't really helpful to those just starting FP as it is probably better to work on one idiom at a time.

open System.IO
open System.Collections.Generic

let words = Directory.GetFiles(@"C:\DLOAD\Data\Dict") 
                |> Seq.map (fun x -> File.ReadLines(x))
                |> Seq.concat
                |> Seq.map (fun x -> x.ToUpper())
                |> Seq.distinct
                |> Seq.groupBy (fun x -> x.Length)
                |> fun xs ->
                    let dict = Dictionary<int,string []>()
                    xs |> Seq.iter (fun x -> dict.Add(fst x, snd x |> Seq.toArray))
                    dict                

let search (start:(int*int list*string) seq) (finish:int list) = 
            let toString (chars:seq<char>) = new string [|for c in chars -> c|]
            let merge (xs:string seq) (ys:string seq) = seq { for x in xs do for y in ys do yield x + y}
            start
                |> Seq.map (fun (x,y,z) -> (x,y,z |> Seq.sort |> toString))
                |> Seq.map (fun (x,y,z) ->
                            words.[x] 
                                |> Seq.filter (fun u -> u |> Seq.sort |> toString = z)
                                |> Seq.map (fun v -> y |> Seq.map (fun i -> v.[i]) |> toString)) 
                |> Seq.fold merge (Seq.singleton "")
                |> Seq.map (fun letters ->
                            let filter (str:string) (xs:string seq) = xs |> Seq.filter (fun x -> x |> Seq.exists (fun z -> str |> Seq.exists (fun y -> z = y) |> not) |> not)
                            let match' (x:string) (y:string) = Seq.zip (x |> Seq.sort) (y |> Seq.sort) |> Seq.exists (fun (x,y) -> x <> y) |> not
                            finish |> Seq.map (fun x -> words.[x] |> filter letters)
                                   |> Seq.fold merge (Seq.singleton "")
                                   |> Seq.filter (match' letters))
                |> Seq.concat
                |> Seq.map (fun str -> 0::finish |> Seq.pairwise |> Seq.map (fun (x,y) -> str.Substring(x,y)))
                
search [(5,[0;2;3],"EFNOL");(5,[2;3],"RATAO");(6,[1;4;5],"TOZALE");(6,[1;2;3],"BLOUED")] [7;4]

results:
[|seq ["TROUBLE"; "FOOT"]; seq ["FLOUTER"; "BOOT"]; seq ["FOOTLER"; "BOUT"];
    seq ["TEARFUL"; "BOZO"]; seq ["FLOUTER"; "BOAZ"]|]


On Mon, Sep 5, 2011 at 7:06 PM, Matthew Moloney <molo...@gmail.com> wrote:
Deprecating the Observer Pattern http://lamp.epfl.ch/~imaier/pub/DeprecatingObserversTR2010.pdf


On Mon, Sep 5, 2011 at 6:26 PM, Matthew Moloney <molo...@gmail.com> wrote:
I'm here, where in Aconex? I can't find anyone else

Sent from my Windows Phone

From: Martin Paulo
Sent: Monday, September 05, 2011 9:41 AM
To: scala-tutori...@googlegroups.com
Subject: Re: Reminder :Scala user group today

I'll be there: somewhat behind schedule with my learning, but what the :-)

BTW, if people on this tutorial group don't subscribe to the main scala user group mailing list (?), the subset of the book by Cay Horstman that TypeSafe are giving away in exchange for your e-mail address is proving to be quite useful to me (http://www.typesafe.com/resources/scala-for-the-impatient).

Martin

On 5 September 2011 07:24, Gokul Krishnan <goku...@gmail.com> wrote:
Hi All,

Just a reminder that the scala user group meeting is today at 6:15  at Aconex - 696 Bourke street

Cheers
Gokul



--
==================================================================

Martin Paulo, BSc. 
Software Developer

Tel :         +61-3-9434 2508 (Home)
Tel :          04 205 20339      (Mobile)
Site:          http://www.thepaulofamily.net

"Nobody goes there any more. It's too crowded" - Yogi Berra.


Martin Paulo

unread,
Sep 6, 2011, 5:03:30 AM9/6/11
to Matthew Moloney, scala-tutori...@googlegroups.com
Hi Matthew

You might have a point about this not being helpful to those of us who are starting out in FP - I am scratching my head over some of the stuff in it. But it is giving me ideas as to ways in which I can improve my Scala code, so I am learning from it!

The answer they were looking for was "trouble" a "foot". It is a very punny puzzle...

Thanks for sending this through.

Martin

Gokul Krishnan

unread,
Sep 6, 2011, 6:51:18 AM9/6/11
to scala-tutori...@googlegroups.com
That does look concise... btw what is the |> operator ?

Matthew Moloney

unread,
Sep 6, 2011, 7:33:13 AM9/6/11
to scala-tutori...@googlegroups.com
It is the forward pipe operator for F#, it takes the value from the left and applies the function on the right. e.g. [1;2;3] |> Seq.map (fun x -> 3 * x) === Seq.map (fun x -> 3 * x) [1;2;3]

In Scala you can use the "." notation, e.g. List(1,2,3) . map(_ * 3)
Reply all
Reply to author
Forward
0 new messages