yesterday's challenge

21 views
Skip to first unread message

Staffan Eketorp

unread,
Apr 26, 2012, 2:11:41 PM4/26/12
to functional-pro...@googlegroups.com
Hello everyone and thanks for coming yesterday! Big thanks to Jayway for hosting, to Hans and Christian for presentations and to (sorry I don't remember your name) for an insightful piece of object/closure wisdom.

I promised to send you all the C# code for the little challenge I put up. I changed it a little tiny bit so that it would be simplier to read and just included my 3rd solution. Please tell me if you want the other ones and I'll post them as well. Anyway the code is here: http://pastebin.com/sK5NukAj

As a little recap of what I wanted:
We have a sequence of Parts (containing a Heading, a Text and a IsColumnLayout property saying whether they are to use a column layout or a wide layout). Our task is to map any given sequence of Parts into a sequence of sequence of parts, where every inner sequence contains

1) either just one part if it's a non-column layout
2) two parts with both having column layout if they're consecutive
3) one part using column layout if it has no consecutive "column partner"

An example in ASCII art (o = part w/ wide layout, | = part w/ column layout)

o
o
|
|
o
|
o
|
|
|

should be mapped to

{ o }
{ o }
{ | | }
{ o }
{ | }
{ o }
{ | | }
{ | }

Please give us a really neat functional solution!

Uwe Dauernheim

unread,
Apr 26, 2012, 2:30:55 PM4/26/12
to functional-pro...@googlegroups.com
I already posted an Erlang solution yesterday, but here again and this time also on pastebin: http://pastebin.com/uKznCD0U If you have any questions, don't hesitate to ask. I'd also like to know what non-erlanger think about this when they see the solution or in general the code.

Staffan Eketorp

unread,
Apr 26, 2012, 2:52:19 PM4/26/12
to functional-pro...@googlegroups.com
Ah great! 

I think it's really interesting to see how the problem can be modeled differently. As I'm really not used to Erlang it's interesting to see how central the pattern matching is for your solution. Regarding the readibility and pedagogic values I personally think that explaining things by examples is the best way most of the times. I realize that pattern matching is really sort of based on that concept, which is cool. So I suppose the code should be very clear, but I'm very new to Erlang and I have to admit it takes some time to read and I haven't really dug into version 2 yet :) If you care to say a few words about them, I'd be willing to listen.

Short - I suppose r is for red (= non column) and g = green ( column). Furthermore I guess that T is short for "tail" as in tail of list, but what is R and B abbrevations for? I'm guessing R = Result or return value, b.

Also - would the solution look roughly the same if the symbols r/g were record/types such as the Part I described?

/S

2012/4/26 Uwe Dauernheim <u...@dauernheim.net>

Jan Henry Nystrom

unread,
Apr 26, 2012, 3:12:57 PM4/26/12
to functional-pro...@googlegroups.com
Hi,

The solution would look very much similar with records, however I would suggest that the Erlang way to
solve this problem is:

% layout.erl
-module(layout).

-export([do/1]).

% r is for red (= non column) and g = green ( column)

do([]) -> [];
do([g, g | T]) -> [{g, g} | do(T)];
do([H | T]) -> [H | do(T)].

/Cheers Henry

Staffan Eketorp

unread,
Apr 26, 2012, 3:24:28 PM4/26/12
to functional-pro...@googlegroups.com
Nice and neat! Inspired by your pattern matching I had to write an F# version as well - available here: 


/S

2012/4/26 Jan Henry Nystrom <janhenr...@gmail.com>

Daniel Larsson

unread,
Apr 26, 2012, 4:04:48 PM4/26/12
to functional-pro...@googlegroups.com
Here's a haskell solution. Ended up the same as Jan Henry Nyström's

http://pastebin.com/RGC1G5bE

2012/4/26 Staffan Eketorp <staffan...@gmail.com>:

Hans Sterby

unread,
Apr 26, 2012, 4:15:24 PM4/26/12
to functional-pro...@googlegroups.com
Cool, I was on my way to do a F# solution to you but you were first.
Nice to some Erlang to, I must read my book "Erlang and OTP in Action"  ( http://www.manning.com/logan/ ).

/rgds Hasse

Staffan Eketorp

unread,
Apr 27, 2012, 2:49:53 AM4/27/12
to functional-pro...@googlegroups.com
I actually learned the function version of pattern matching (as apposed to the match syntax) in F# by Henrik the other day, so Henrik: with that in mind the code got slightly terser:  http://pastebin.com/FqdVDJQj 

/S

2012/4/26 Hans Sterby <hasse...@gmail.com>

Uwe Dauernheim

unread,
Apr 27, 2012, 3:57:20 AM4/27/12
to functional-pro...@googlegroups.com
Where is "• let b = true • let c = not b" used?

/Uwe

Staffan Eketorp

unread,
Apr 27, 2012, 6:02:52 AM4/27/12
to functional-pro...@googlegroups.com
Haha...rubbish that wasn't meant to be there. Sorry

2012/4/27 Uwe Dauernheim <u...@dauernheim.net>

Christian Jacobsen

unread,
Jun 1, 2012, 3:32:12 AM6/1/12
to functional-programming-sthlm
A bit late in to the challenge but here's a tail-recursive version:
https://gist.github.com/2846874

/Christian

On 27 Apr, 08:49, Staffan Eketorp <staffan.eket...@gmail.com> wrote:
> I actually learned the function version of pattern matching (as apposed to
> the match syntax) in F# by Henrik the other day, so Henrik: with that in
> mind the code got slightly terser:  http://pastebin.com/FqdVDJQj
>
> /S
>
> 2012/4/26 Hans Sterby <hasseti...@gmail.com>
>
>
>
>
>
>
>
> > Cool, I was on my way to do a F# solution to you but you were first.
> > Nice to some Erlang to, I must read my book "Erlang and OTP in Action"  (
> >http://www.manning.com/logan/).
>
> > /rgds Hasse
>
> > On Thu, Apr 26, 2012 at 10:04 PM, Daniel Larsson <
> > daniel.j.lars...@gmail.com> wrote:
>
> >> Here's a haskell solution. Ended up the same as Jan Henry Nyström's
>
> >>http://pastebin.com/RGC1G5bE
>
> >> 2012/4/26 Staffan Eketorp <staffan.eket...@gmail.com>:
> >> > Nice and neat! Inspired by your pattern matching I had to write an F#
> >> > version as well - available here:
>
> >> >http://pastebin.com/Jw244hFz
>
> >> > /S
>
> >> > 2012/4/26 Jan Henry Nystrom <janhenrynyst...@gmail.com>
> >> >> > time also on pastebin:http://pastebin.com/uKznCD0UIf you have any
Reply all
Reply to author
Forward
0 new messages