<?xml version='1.0' encoding='UTF-8'?><?xml-stylesheet href="http://www.blogger.com/styles/atom.css" type="text/css"?><feed xmlns='http://www.w3.org/2005/Atom' xmlns:openSearch='http://a9.com/-/spec/opensearchrss/1.0/' xmlns:georss='http://www.georss.org/georss'><id>tag:blogger.com,1999:blog-14566585</id><updated>2009-02-21T03:39:53.150-08:00</updated><title type='text'>Making Flan</title><subtitle type='html'></subtitle><link rel='http://schemas.google.com/g/2005#feed' type='application/atom+xml' href='http://makingflan.blogspot.com/feeds/posts/default'/><link rel='self' type='application/atom+xml' href='http://www.blogger.com/feeds/14566585/posts/default'/><link rel='alternate' type='text/html' href='http://makingflan.blogspot.com/'/><link rel='hub' href='http://pubsubhubbub.appspot.com/'/><author><name>Laurie Cheers</name><uri>http://www.blogger.com/profile/09919006779080998004</uri><email>noreply@blogger.com</email></author><generator version='7.00' uri='http://www.blogger.com'>Blogger</generator><openSearch:totalResults>6</openSearch:totalResults><openSearch:startIndex>1</openSearch:startIndex><openSearch:itemsPerPage>25</openSearch:itemsPerPage><entry><id>tag:blogger.com,1999:blog-14566585.post-770673628970696923</id><published>2007-06-05T22:49:00.000-07:00</published><updated>2008-09-10T13:40:01.787-07:00</updated><title type='text'>More Flan iteration</title><content type='html'>Perhaps the simplest kind of iteration is "print the numbers from 1 to 10". Flan's syntax for this (which even beats Ruby in terms of consciseness and clarity, if I do say so myself) is:&lt;br /&gt;&lt;br /&gt;&lt;span style="font-family:courier new;"&gt;#print(each 1..10);&lt;/span&gt;&lt;br /&gt;&lt;br /&gt;As requested, this produces &lt;span style="font-family:courier new;"&gt;12345678910.&lt;/span&gt;&lt;br /&gt;&lt;br /&gt;Let's examine it piece by piece.&lt;br /&gt;&lt;br /&gt;The &lt;span style="font-family:courier new;"&gt;..&lt;/span&gt; operator produces a type. (&lt;a href="http://makingflan.blogspot.com/2006_03_01_archive.html"&gt;Earlier&lt;/a&gt; I said it produced a list fragment; change of plan. It's less comon to need a list fragment, and you can quite trivially generate one from the type.)&lt;br /&gt;&lt;br /&gt;The type produced by &lt;span style="font-family:courier new;"&gt;a..b&lt;/span&gt; is "any integer from a to b, inclusive". In case you're wondering, there's also a range of companion operators such as &lt;span style="font-family:courier new;"&gt;a&amp;lt;..b&lt;/span&gt;: "integers from a+1 to b", and &lt;span style="font-family:courier new;"&gt;a..&amp;lt;b&lt;/span&gt;: "integers from a to b-1". The latter is probably useful more often than the straightforward &lt;span style="font-family:courier new;"&gt;..&lt;/span&gt; operator.&lt;br /&gt;&lt;br /&gt;As we saw &lt;a href="http://makingflan.blogspot.com/2007_05_01_archive.html"&gt;last time&lt;/a&gt;, the "each" keyword can be used to iterate over a list; however, it can also iterate over an enumerable type. (In fact the list iteration system is based on this.)&lt;br /&gt;&lt;br /&gt;An enumerable type is one that's finite, and has some kind of order to it. For example:&lt;br /&gt;&lt;br /&gt;&lt;span style="font-family:courier new;"&gt;@SingleDigitNumber = 0..9;&lt;/span&gt;&lt;br /&gt;&lt;span style="font-family:courier new;"&gt;@Vowel = 'a'¦'e'¦'i'¦'o'¦'u';&lt;/span&gt;&lt;br /&gt;&lt;span style="font-family:courier new;"&gt;@ListItem = thelist.Element;&lt;/span&gt;&lt;br /&gt;&lt;span style="font-family:Courier New;"&gt;@ListIndex = thelist.Index;&lt;/span&gt;&lt;br /&gt;&lt;span style="font-family:Courier New;"&gt;@ThreeVowelString = [Vowel, Vowel, Vowel];&lt;/span&gt;&lt;br /&gt;&lt;br /&gt;This can be very useful. It's a convenient way of declaring "the solution must look something like this", and then searching for a valid solution among the things that look like that.&lt;br /&gt;&lt;br /&gt;For example, in a recent discussion on Reddit, I gave the following (slightly reformatted, but otherwise the same) Flan solution to &lt;a href="http://xkcd.com/c287.html"&gt;this knapsack problem&lt;/a&gt;:&lt;br /&gt;&lt;br /&gt;&lt;span style="font-family:courier new;"&gt;@prices = [215, 275, 335, 355, 420, 580];&lt;/span&gt;&lt;br /&gt;&lt;span style="font-family:courier new;"&gt;@Solution = [0..7:6]; // a Solution is a list of 6 numbers between 0 and 7&lt;/span&gt;&lt;br /&gt;&lt;span style="font-family:courier new;"&gt;&lt;/span&gt;&lt;br /&gt;&lt;span style="font-family:courier new;"&gt;int Solution.@totalPrice &lt;/span&gt;&lt;br /&gt;&lt;span style="font-family:courier new;"&gt;{&lt;/span&gt;&lt;br /&gt;&lt;span style="font-family:courier new;"&gt;return sum(# this[each @i] * prices[i] );&lt;/span&gt;&lt;br /&gt;&lt;span style="font-family:courier new;"&gt;}&lt;/span&gt;&lt;br /&gt;&lt;span style="font-family:courier new;"&gt;&lt;/span&gt;&lt;br /&gt;&lt;span style="font-family:courier new;"&gt;@result = #first Solution:totalPrice.is 1505;&lt;/span&gt;&lt;br /&gt;&lt;br /&gt;Let's step through this to make sure it's clear -&lt;br /&gt;&lt;br /&gt;&lt;span style="font-family:courier new;"&gt;prices&lt;/span&gt; is just a list of the 6 appetizer prices. Nothing much to say here.&lt;br /&gt;&lt;span style="font-family:courier new;"&gt;Solution&lt;/span&gt; is our enumerable type. It's a list of 6 integers, representing the number of times to order each of the 6 appetizers.&lt;br /&gt;&lt;span style="font-family:courier new;"&gt;Solution.totalPrice&lt;/span&gt; declares a method belonging to the type Solution, to represent the total price of the order. I haven't shown a method declaration before, but it's more or less the same as a C++ one - including the use of &lt;span style="font-family:courier new;"&gt;this&lt;/span&gt; to refer to the Solution it's acting on. The price is calculated using an iteration, much like the &lt;span style="font-family:courier new;"&gt;dot_ab&lt;/span&gt; example I showed last time.&lt;br /&gt;&lt;br /&gt;There's only one really new feature here, and that's the construct&lt;br /&gt;&lt;span style="font-family:courier new;"&gt;#first Solution:totalPrice.is 1505&lt;/span&gt;&lt;br /&gt;&lt;br /&gt;This iterates through all the possible Solutions, looking for the first one whose totalPrice is 1505.&lt;br /&gt;(I've used the iteration controller &lt;span style="font-family:courier new;"&gt;first&lt;/span&gt;, so that it stops after the first solution is found. To generate a list of all solutions I could have used &lt;span style="font-family:courier new;"&gt;each&lt;/span&gt;.)&lt;br /&gt;&lt;br /&gt;The colon operator is filtering the iteration, similar to the ~ operator I introduced last time. In fact, I could equally well have written -&lt;br /&gt;&lt;br /&gt;&lt;span style="font-family:courier new;"&gt;@prices = [215, 275, 335, 355, 420, 580];&lt;br /&gt;@Solution = [0..7:6]; // a Solution is a list of 6 numbers between 0 and 7&lt;br /&gt;&lt;/span&gt;&lt;br /&gt;&lt;span style="font-family:courier new;"&gt;is Solution.@CorrectPrice &lt;/span&gt;&lt;br /&gt;&lt;span style="font-family:courier new;"&gt;{&lt;/span&gt;&lt;br /&gt;&lt;span style="font-family:courier new;"&gt;return sum(# this[each @i] * prices[i] ).is 1505;&lt;/span&gt;&lt;br /&gt;&lt;span style="font-family:courier new;"&gt;}&lt;/span&gt;&lt;br /&gt;&lt;span style="font-family:courier new;"&gt;@result = #first Solution ~CorrectPrice;&lt;/span&gt;&lt;br /&gt;&lt;br /&gt;&lt;br /&gt;More to come soon.&lt;div class="blogger-post-footer"&gt;&lt;img width='1' height='1' src='https://blogger.googleusercontent.com/tracker/14566585-770673628970696923?l=makingflan.blogspot.com'/&gt;&lt;/div&gt;</content><link rel='replies' type='application/atom+xml' href='http://makingflan.blogspot.com/feeds/770673628970696923/comments/default' title='Post Comments'/><link rel='replies' type='text/html' href='https://www.blogger.com/comment.g?blogID=14566585&amp;postID=770673628970696923' title='1 Comments'/><link rel='edit' type='application/atom+xml' href='http://www.blogger.com/feeds/14566585/posts/default/770673628970696923'/><link rel='self' type='application/atom+xml' href='http://www.blogger.com/feeds/14566585/posts/default/770673628970696923'/><link rel='alternate' type='text/html' href='http://makingflan.blogspot.com/2007/06/more-flan-iteration.html' title='More Flan iteration'/><author><name>Laurie Cheers</name><uri>http://www.blogger.com/profile/09919006779080998004</uri><email>noreply@blogger.com</email><gd:extendedProperty xmlns:gd='http://schemas.google.com/g/2005' name='OpenSocialUserId' value='16659230163727763737'/></author><thr:total xmlns:thr='http://purl.org/syndication/thread/1.0'>1</thr:total></entry><entry><id>tag:blogger.com,1999:blog-14566585.post-3606482393923725798</id><published>2007-05-30T07:32:00.000-07:00</published><updated>2007-06-08T01:58:00.307-07:00</updated><title type='text'>Iterators in Flan</title><content type='html'>One of the nicest features of Flan is its support for iteration. Here's an example:&lt;br /&gt;&lt;br /&gt;&lt;span style="font-family:courier new;"&gt;#println( greetings[each] );&lt;/span&gt;&lt;br /&gt;&lt;br /&gt;Even if you haven't seen the syntax before (and you haven't, because I made it up), I think this should be fairly self-explanatory. It takes a list of strings called "greetings", and prints each one (with a newline at the end, thanks to println). So if you start with a declaration such as...&lt;br /&gt;&lt;br /&gt;&lt;span style="font-family:courier new;"&gt;@greetings = [ "Hello world!", "Goodbye world!", "Don't forget to write, world!" ];&lt;/span&gt;&lt;br /&gt;&lt;br /&gt;...then the result is:&lt;br /&gt;&lt;br /&gt;&lt;span style="font-family:courier new;"&gt;Hello world!&lt;/span&gt;&lt;br /&gt;&lt;span style="font-family:courier new;"&gt;Goodbye world!&lt;/span&gt;&lt;br /&gt;&lt;span style="font-family:courier new;"&gt;Don't forget to write, world!&lt;/span&gt;&lt;br /&gt;&lt;br /&gt;&lt;br /&gt;&lt;br /&gt;&lt;p&gt;Semantically, this has the same effect as rearranging the line into a C#-style foreach loop.&lt;/p&gt;&lt;p&gt;&lt;span style="font-family:courier new;"&gt;foreach( string g in greetings )&lt;/span&gt;&lt;br /&gt;&lt;span style="font-family:courier new;"&gt;{&lt;/span&gt;&lt;br /&gt;&lt;span style="font-family:courier new;"&gt;println(g);&lt;/span&gt;&lt;br /&gt;&lt;span style="font-family:courier new;"&gt;}&lt;/span&gt;&lt;/p&gt;&lt;br /&gt;(The above is pseudo-C#, not real Flan code).&lt;br /&gt;Ok then, one question remains - what's with the # at the beginning of the line?&lt;br /&gt;&lt;br /&gt;&lt;span style="font-family:courier new;"&gt;&lt;span style="color:#ff0000;"&gt;&lt;strong&gt;#&lt;/strong&gt;&lt;/span&gt;println( greetings[each] ); &lt;/span&gt;&lt;br /&gt;&lt;br /&gt;Well, it's called the multiple execution operator, and all uses of [each] must be contained in one: otherwise the compiler will complain.&lt;br /&gt;&lt;br /&gt;This operator does two jobs: firstly, it serves as a visual warning to people reading the code, "careful, this line is not as simple as it looks". (That's why it's such a big black character.)&lt;br /&gt;&lt;br /&gt;Secondly, it's a merge point, letting you move code out of the foreach expression. Code that's syntactically "outside" the # will be executed only once, taking the list of values as a whole instead of each individual value. Hard to describe, easy to show by example:&lt;br /&gt;&lt;br /&gt;&lt;span style="font-family:courier new;"&gt;@vec = [44, 62, 10];&lt;/span&gt;&lt;br /&gt;&lt;span style="font-family:courier new;"&gt;@veclength = sqrt( sum( #square(vec[each]) ));&lt;/span&gt;&lt;br /&gt;&lt;br /&gt;This is calculating the length of the vector &lt;span style="font-family:courier new;"&gt;vec&lt;/span&gt;. The important bit is the expression &lt;span style="font-family:courier new;"&gt;#square(vec[each])&lt;/span&gt;, which evaluates to a new list: &lt;span style="font-family:courier new;"&gt;[square(44), square(62), square(10)]&lt;/span&gt;. The &lt;span style="font-family:courier new;"&gt;sum&lt;/span&gt; function then takes the sum of those items, and so on.&lt;br /&gt;&lt;br /&gt;&lt;br /&gt;Useful enough yet? We're just getting started. You can also filter the list this way, with the &lt;span style="font-family:courier new;"&gt;~&lt;/span&gt; operator. Read it as "that is".&lt;br /&gt;For example, to print the integers in a list:&lt;br /&gt;&lt;br /&gt;&lt;span style="font-family:courier new;"&gt;#println( thelist[each] ~int );&lt;/span&gt;&lt;br /&gt;&lt;br /&gt;And as you may recall a Flan type can be, in fact, any value or predicate:&lt;br /&gt;&lt;br /&gt;&lt;span style="font-family:courier new;"&gt;#println( thelist[each] ~even );&lt;/span&gt;&lt;br /&gt;&lt;span style="font-family:courier new;"&gt;#println( thelist[each] ~prime );&lt;/span&gt;&lt;br /&gt;&lt;span style="font-family:courier new;"&gt;#println( thelist[each] ~1 ));&lt;/span&gt;&lt;br /&gt;&lt;span style="font-family:courier new;"&gt;#println( thelist[each] ~ &amp;gt;0 ) ); &lt;/span&gt;&lt;br /&gt;&lt;span style="font-family:courier new;"&gt;#println( thelist[each] ~divisibleby(7) );&lt;/span&gt;&lt;br /&gt;&lt;span style="font-family:courier new;"&gt;#println( thelist[each] ~[3] ) );&lt;/span&gt;&lt;br /&gt;&lt;span style="font-family:courier new;"&gt;#println( thelist[each] ~[char*, 'hello', char*] ); &lt;/span&gt;&lt;br /&gt;&lt;br /&gt;You can also filter by index, by putting a type inside the brackets instead of outside. For example, to select alternate items in the list:&lt;br /&gt;&lt;br /&gt;&lt;span style="font-family:courier new;"&gt;#println( thelist[each ~even] );&lt;/span&gt;&lt;br /&gt;&lt;br /&gt;Or the first 10 items:&lt;br /&gt;&lt;br /&gt;&lt;span style="font-family:courier new;"&gt;#println( thelist[each ~ &amp;lt;10] );&lt;/span&gt;&lt;br /&gt;&lt;br /&gt;Flan's declare-anywhere syntax also comes in handy here. You can name the index, and refer back to it later. (For obvious reasons, identifiers declared this way will go out of scope when the loop ends.)&lt;br /&gt;&lt;br /&gt;For example, search for items in the list that are greater than their index.&lt;br /&gt;&lt;br /&gt;&lt;span style="font-family:courier new;"&gt;#println( thelist[each @i] ~ &amp;gt;i );&lt;br /&gt;&lt;/span&gt;&lt;br /&gt;Or, borrowing an example from my last post, you can iterate over two lists at the same time.&lt;br /&gt;&lt;br /&gt;&lt;span style="font-family:courier new;"&gt;@dot_ab = sum( #a[each @i] * b[i] );&lt;/span&gt;&lt;span style="font-family:courier new;"&gt;&lt;br /&gt;&lt;/span&gt;&lt;span style="font-family:courier new;"&gt;&lt;/span&gt;&lt;br /&gt;(In case you're wondering, the # operator has the lowest precedence of any operator.)&lt;br /&gt;&lt;br /&gt;&lt;br /&gt;&lt;br /&gt;In much the same way as the indices, you can name the values. For example, in the vector length example from earlier, instead of calling square, you could write:&lt;br /&gt;&lt;br /&gt;&lt;span style="font-family:courier new;"&gt;@veclength = sqrt( sum( #thelist[each] @v * v ));&lt;/span&gt;&lt;br /&gt;&lt;br /&gt;&lt;br /&gt;&lt;br /&gt;And finally, it sometimes happens that you're not interested in the whole list - 'each' is not the behaviour you want. If you just want to find one item, say 'find' instead:&lt;br /&gt;&lt;br /&gt;&lt;span style="font-family:courier new;"&gt;@x = #thelist[find] ~prime;&lt;/span&gt;&lt;br /&gt;&lt;br /&gt;(NB: rather than evaluating to a list of matches, a # expression controlled by &lt;span style="font-family:courier new;"&gt;find&lt;/span&gt; will return a single value. If the list contains no prime numbers, it will return &lt;span style="font-family:Courier New;"&gt;null&lt;/span&gt;.)&lt;br /&gt;&lt;br /&gt;&lt;br /&gt;Another type of iteration control is &lt;span style="font-family:courier new;"&gt;every&lt;/span&gt;. This is a logical AND applied to every member of the list, shortcutting at the first false result. This is mostly useful in the context of an 'if' statement. For example, to test whether every entry in the list is positive:&lt;br /&gt;&lt;br /&gt;&lt;span style="font-family:courier new;"&gt;if( #thelist[every].is(&amp;gt;0) ) &lt;/span&gt;&lt;span style="font-family:courier new;"&gt;{ &lt;/span&gt;&lt;span style="font-family:courier new;"&gt;... &lt;/span&gt;&lt;span style="font-family:courier new;"&gt;}&lt;/span&gt;&lt;br /&gt;&lt;br /&gt;And of course where you have an AND, you sometimes need OR...&lt;br /&gt;&lt;br /&gt;&lt;span style="font-family:courier new;"&gt;if( #thelist[some].is(&amp;gt;0) ) { ... }&lt;/span&gt;&lt;br /&gt;&lt;br /&gt;&lt;br /&gt;&lt;br /&gt;That's all for now. Hope this has been interesting.&lt;br /&gt;&lt;br /&gt;Oh, and since nobody will take a language design seriously if there's no compiler that runs it, I'm working on changing my interpreter into a compiler that will generate CIL, via the handy &lt;a href="http://www.mono-project.com/Cecil"&gt;Cecil&lt;/a&gt; library. This seems like the ideal compilation target for any nascent language - debuggable, (slightly) portable, and with a load of predefined libraries. I'll let you know how the conversion goes.&lt;br /&gt;&lt;br /&gt;&lt;br /&gt;&lt;/span&gt;&lt;span style="font-family:courier new;"&gt;&lt;/span&gt;&lt;div class="blogger-post-footer"&gt;&lt;img width='1' height='1' src='https://blogger.googleusercontent.com/tracker/14566585-3606482393923725798?l=makingflan.blogspot.com'/&gt;&lt;/div&gt;</content><link rel='replies' type='application/atom+xml' href='http://makingflan.blogspot.com/feeds/3606482393923725798/comments/default' title='Post Comments'/><link rel='replies' type='text/html' href='https://www.blogger.com/comment.g?blogID=14566585&amp;postID=3606482393923725798' title='0 Comments'/><link rel='edit' type='application/atom+xml' href='http://www.blogger.com/feeds/14566585/posts/default/3606482393923725798'/><link rel='self' type='application/atom+xml' href='http://www.blogger.com/feeds/14566585/posts/default/3606482393923725798'/><link rel='alternate' type='text/html' href='http://makingflan.blogspot.com/2007/05/iterators-in-flan.html' title='Iterators in Flan'/><author><name>Laurie Cheers</name><uri>http://www.blogger.com/profile/09919006779080998004</uri><email>noreply@blogger.com</email><gd:extendedProperty xmlns:gd='http://schemas.google.com/g/2005' name='OpenSocialUserId' value='16659230163727763737'/></author><thr:total xmlns:thr='http://purl.org/syndication/thread/1.0'>0</thr:total></entry><entry><id>tag:blogger.com,1999:blog-14566585.post-6982419094835663637</id><published>2007-04-19T13:24:00.000-07:00</published><updated>2007-05-29T04:05:40.097-07:00</updated><title type='text'>Zip means zip</title><content type='html'>Elegance isn't everything. I recently had a discussion on Reddit.com about this Haskell code snippet:&lt;br /&gt;&lt;br /&gt;&lt;span style="font-family:courier new;"&gt;sum $ zipWith(*) a b&lt;br /&gt;&lt;/span&gt;&lt;br /&gt;(It's calculating the dot product of vectors a and b. In other words, &lt;span style="font-family:courier new;"&gt;a[0]*b[0] + a[1]*b[1] + a[2]*b[2] + ...&lt;/span&gt;)&lt;br /&gt;&lt;br /&gt;In short - I hate it.&lt;br /&gt;I'm not talking about any intellectual or pragmatic objection - it just hits me with a gut-level "yuck", like a bowl of maggots. So what exactly is my problem with it? Here's my attempt to understand my reaction and explain it better.&lt;br /&gt;&lt;br /&gt;&lt;br /&gt;Really, the only part I object to is the &lt;span style="font-family:courier new;"&gt;zipWith&lt;/span&gt; function. It's simply poor communication. A program's job, or a large chunk of it, is to explain your intent to &lt;em&gt;other programmers&lt;/em&gt;, not to the compiler. It can be helpful to name an abstract concept like this, but it's important to keep the target in sight: you're doing this to make the program more readable. A bad abstraction is just an &lt;a href="http://zedshaw.com/rants/indirection_is_not_abstraction.html"&gt;indirection&lt;/a&gt;: it makes the program less clear, not more.&lt;br /&gt;&lt;br /&gt;&lt;br /&gt;How would you describe the dot product, in words? Perhaps something like...&lt;br /&gt;&lt;br /&gt;&lt;em&gt;"Multiply each number in A by the corresponding number in B, and total the results."&lt;br /&gt;&lt;/em&gt;&lt;br /&gt;Does "zip" appear in there at all? Would anyone use that word to describe what the dot product is doing? "It zips two lists with a multiplication"?&lt;br /&gt;&lt;br /&gt;&lt;br /&gt;Moreover, the visual metaphor of a "zip" is inappropriate. The teeth of a zip don't meet head on - they interlock, with each tooth held by the two teeth opposite. So the &lt;span style="font-family:courier new;"&gt;zipWith(*)&lt;/span&gt; operation ought to be multiplying each item by the two opposite:&lt;br /&gt;&lt;br /&gt;&lt;span style="font-family:courier new;"&gt;a[0]*b[0] + a[0]*b[1] + a[1]*b[1] + a[1]*b[2] + a[2]*b[2] + ... &lt;/span&gt;&lt;br /&gt;&lt;br /&gt;&lt;br /&gt;All right then, let's make this criticism more constructive. What &lt;em&gt;should&lt;/em&gt; this say?&lt;br /&gt;&lt;br /&gt;Let's look at what &lt;span style="font-family:courier new;"&gt;zipWith&lt;/span&gt; is actually doing. &lt;span style="font-family:courier new;"&gt;zipWith(*)&lt;/span&gt; evaluates to a function: one that multiplies the matching elements in two lists together. In other words, by applying &lt;span style="font-family:courier new;"&gt;zipWith&lt;/span&gt; we've taken a function (&lt;span style="font-family:courier new;"&gt;*&lt;/span&gt;) that multiplies numbers, and elevated it into one that multiplies lists together. List multiplication.&lt;br /&gt;&lt;br /&gt;But we can't call it &lt;span style="font-family:courier new;"&gt;list(*)&lt;/span&gt;. So how about this?&lt;br /&gt;&lt;br /&gt;&lt;span style="font-family:courier new;"&gt;sum $ listwise(*) a b&lt;br /&gt;&lt;/span&gt;&lt;br /&gt;Ok, it's not ideal; there are loads of different ways one could elevate a function to work "listwise", so the ignorant reader might wonder, what exactly is the above doing? Perhaps it's...&lt;br /&gt;&lt;br /&gt;Multiplying a single value with a list?&lt;br /&gt;&lt;span style="font-family:courier new;"&gt;a*b[0] + a*b[1] + a*b[2] + a*b[3] + ...&lt;/span&gt;&lt;br /&gt;&lt;br /&gt;Multiplying each item in list a with each in list b?&lt;br /&gt;&lt;span style="font-family:courier new;"&gt;a[0]*b[0] + a[0]*b[1] + a[0]*b[2] + ... a[1]*b[0] + a[1]*b[1] + ...&lt;/span&gt;&lt;br /&gt;&lt;br /&gt;Or even something esoteric, like multiplying each item with the two opposite? :)&lt;br /&gt;&lt;span style="font-family:courier new;"&gt;a[0]*b[0] + a[0]*b[1] + a[1]*b[1] + a[1]*b[2] + a[2]*b[2] + ...&lt;/span&gt;&lt;br /&gt;&lt;br /&gt;There are plenty of other possible patterns, too. Any of these could justifiably be called "listwise multiplication" - the name isn't really giving the reader enough information.&lt;br /&gt;&lt;br /&gt;So, we're looking for a more precise name. One that says "listwise, applying to pairs of elements with the same index". &lt;span style="font-family:courier new;"&gt;pairwise(*)&lt;/span&gt; is just as vague (when does multiplication not apply to a pair of numbers?); &lt;span style="font-family:courier new;"&gt;zipping(*)&lt;/span&gt; could work but has the same bad metaphor as before.&lt;br /&gt;&lt;br /&gt;What other metaphors could work? Teeth clenching... fingertips touching... passengers moving from one train to another... aha! How about two ships shooting cannonballs at each other?&lt;br /&gt;It's got the right metaphor (each cannonball hits the corresponding part of the opposite ship), and a snappy name that works as an adjective, and makes sense even if you don't make the mental leap to ships.&lt;br /&gt;Ladies and gentlemen, I give you - "broadside".&lt;br /&gt;&lt;br /&gt;&lt;span style="font-family:courier new;"&gt;sum $ broadside(*) a b&lt;br /&gt;&lt;/span&gt;&lt;br /&gt;This is a function, also known as zipwith, which elevates a function into its "broadside form" - in this case, broadside multiplication. Multiplying two lists together along their broadest sides.&lt;br /&gt;&lt;br /&gt;Like it? Hate it? I'd be interested to know what you think.&lt;div class="blogger-post-footer"&gt;&lt;img width='1' height='1' src='https://blogger.googleusercontent.com/tracker/14566585-6982419094835663637?l=makingflan.blogspot.com'/&gt;&lt;/div&gt;</content><link rel='replies' type='application/atom+xml' href='http://makingflan.blogspot.com/feeds/6982419094835663637/comments/default' title='Post Comments'/><link rel='replies' type='text/html' href='https://www.blogger.com/comment.g?blogID=14566585&amp;postID=6982419094835663637' title='5 Comments'/><link rel='edit' type='application/atom+xml' href='http://www.blogger.com/feeds/14566585/posts/default/6982419094835663637'/><link rel='self' type='application/atom+xml' href='http://www.blogger.com/feeds/14566585/posts/default/6982419094835663637'/><link rel='alternate' type='text/html' href='http://makingflan.blogspot.com/2007/04/zip-means-zip.html' title='Zip means zip'/><author><name>Laurie Cheers</name><uri>http://www.blogger.com/profile/09919006779080998004</uri><email>noreply@blogger.com</email><gd:extendedProperty xmlns:gd='http://schemas.google.com/g/2005' name='OpenSocialUserId' value='16659230163727763737'/></author><thr:total xmlns:thr='http://purl.org/syndication/thread/1.0'>5</thr:total></entry><entry><id>tag:blogger.com,1999:blog-14566585.post-114249765324506912</id><published>2006-03-16T00:06:00.000-08:00</published><updated>2006-05-25T07:38:38.310-07:00</updated><title type='text'>Regular expressionescence</title><content type='html'>I felt startled when I read Tim Sweeny's &lt;a href="http://www.st.cs.uni-sb.de/edu/seminare/2005/advanced-fp/docs/sweeny.pdf"&gt;language proposal (pdf)&lt;/a&gt;. Perhaps that's not exactly the word - I felt &lt;em&gt;vindicated&lt;/em&gt;. The sort of things he says he wants from a language are exactly the sorts of things I've been desigining in my experimental programming language, Flan. Not too surprising, I suppose, since the two of us are in the &lt;a href="http://www.mobygames.com/developer/sheet/view/developerId,108626/"&gt;same field&lt;/a&gt;.&lt;br /&gt;&lt;br /&gt;One of the key features of Flan is its type system. You can have type-checking applied statically or dynamically, and you can make types more-or-less as restrictive or as loose as you want. In the end, it turns out that you can use this one system to do Prolog or Haskell-style data deconstruction, SQL-style filtering and pattern-matching, Eiffel-style compile-time contracts, and a really readable version of regular expressions.&lt;br /&gt;Although such flexible type systems seem to be &lt;a href="http://weblog.raganwald.com/2006/03/ill-take-static-typing-for-800-alex.html"&gt;in vogue lately&lt;/a&gt;, I've actually been working out the details of this system for several years now.&lt;br /&gt;Let's start with a demo of Flan's static type declarations:&lt;br /&gt;&lt;br /&gt;&lt;span style="font-family:courier new;"&gt;int @a = 0; &lt;span style="color:#006600;"&gt;// declare an integer variable a, which is initially 0.&lt;/span&gt;&lt;br /&gt;&lt;/span&gt;&lt;br /&gt;To quickly summarize: the basic syntax is C. Comments are marked with &lt;span style="font-family:courier new;"&gt;//&lt;/span&gt; and &lt;span style="font-family:courier new;"&gt;/**/&lt;/span&gt;, assignment with &lt;span style="font-family:courier new;"&gt;=&lt;/span&gt;, statements delimited with &lt;span style="font-family:courier new;"&gt;;&lt;/span&gt;. (Because as far as I'm concerned, if it ain't broke, don't fix it.)&lt;br /&gt;&lt;br /&gt;As I've mentioned &lt;a href="http://makingflan.blogspot.com/2005/07/beginning.html"&gt;before&lt;/a&gt;, &lt;span style="font-family:courier new;"&gt;@foo&lt;/span&gt; means "create a new variable called foo". If you're used to Perl or Ruby it takes a little time to adjust to declaring variables with &lt;span style="font-family:courier new;"&gt;@foo&lt;/span&gt; and then using them with just &lt;span style="font-family:courier new;"&gt;foo&lt;/span&gt; - but I'm finding that the more I use this syntax, the more I like it. It turns out that it even helps people learn the language. You know when you look at a piece of code in an unfamiliar language, and you're not sure which identifiers are being declared, and which have been declared elsewhere? That simply doesn't happen in Flan.&lt;br /&gt;&lt;br /&gt;Some more examples:&lt;br /&gt;&lt;br /&gt;&lt;span style="font-family:courier new;"&gt;char @b = 'B'; &lt;span style="color:#006600;"&gt;// b can be any character - initially, 'B'. (The audience gasps!)&lt;/span&gt;&lt;/span&gt;&lt;br /&gt;&lt;span style="font-family:courier new;"&gt;value @c; &lt;span style="color:#006600;"&gt;// c can be any value - character, number, list, whatever.&lt;/span&gt;&lt;/span&gt;&lt;span style="color:#006600;"&gt;&lt;br /&gt;&lt;/span&gt;&lt;span style="font-family:courier new;"&gt;4 @d; &lt;span style="color:#006600;"&gt;// d is always the number 4.&lt;/span&gt;&lt;/span&gt;&lt;span style="color:#006600;"&gt;&lt;br /&gt;&lt;/span&gt;&lt;span style="font-family:courier new;"&gt;'E' @e; &lt;span style="color:#006600;"&gt;// e is always the character 'E'.&lt;/span&gt;&lt;/span&gt;&lt;br /&gt;&lt;span style="font-family:courier new;"&gt;"ffFF" @f; &lt;span style="color:#006600;"&gt;// f is always the string "ffFF".&lt;br /&gt;&lt;/span&gt;&lt;/span&gt;&lt;br /&gt;Notice that compile-time constants don't need special treatment in this system: they're just variables with a very constrained type.&lt;br /&gt;&lt;br /&gt;Flan supports lists as first-class values, denoted Python-style with square brackets and commas. (Yeah, that's different from what I said &lt;a href="http://makingflan.blogspot.com/2006/02/why-flan.html"&gt;last time&lt;/a&gt;; bear with me.)&lt;br /&gt;&lt;br /&gt;&lt;span style="font-family:courier new;"&gt;[4,3,2,1] @g; &lt;span style="color:#006600;"&gt;// the constant list [4,3,2,1].&lt;/span&gt;&lt;/span&gt;&lt;span style="color:#006600;"&gt;&lt;br /&gt;&lt;/span&gt;&lt;span style="font-family:courier new;"&gt;[int,int] @h = [2,7]; &lt;span style="color:#006600;"&gt;// any list of exactly two integers; initially [2,7].&lt;/span&gt;&lt;/span&gt;&lt;span style="color:#006600;"&gt;&lt;br /&gt;&lt;/span&gt;&lt;span style="font-family:courier new;"&gt;[int,1,int] @i = [200,1,-200]; &lt;span style="color:#006600;"&gt;// any list of 3 integers with 1 in the middle.&lt;br /&gt;&lt;/span&gt;&lt;/span&gt;&lt;br /&gt;If you're making a long list, this gets annoying - you don't want to have to write &lt;span style="font-family:courier new;"&gt;[int,int,int,int,int...]&lt;/span&gt;, so you can instead use &lt;span style="font-family:courier new;"&gt;:&lt;/span&gt;, the "repeat" operator.&lt;br /&gt;&lt;br /&gt;&lt;span style="font-family:courier new;"&gt;[int:7] @j = [0:7];&lt;br /&gt;&lt;/span&gt;&lt;br /&gt;...is the same as...&lt;br /&gt;&lt;span style="font-family:courier new;"&gt;&lt;/span&gt;&lt;br /&gt;&lt;span style="font-family:courier new;"&gt;[int,int,int,int,int,int,int] @j = [0,0,0,0,0,0,0];&lt;br /&gt;&lt;/span&gt;&lt;br /&gt;This is a very flexible system. For example, you can mix and match commas and colons...&lt;br /&gt;&lt;br /&gt;&lt;span style="font-family:courier new;"&gt;[1:4, 2, 3:2, 4] @k; &lt;span style="color:#006600;"&gt;// the list [1,1,1,1,2,3,3,4]&lt;/span&gt;&lt;/span&gt;&lt;span style="color:#006600;"&gt;&lt;br /&gt;&lt;/span&gt;&lt;span style="font-family:courier new;"&gt;['aci', 'e':8, 'd'] @m; &lt;span style="color:#006600;"&gt;// the string "acieeeeeeeed".&lt;/span&gt;&lt;br /&gt;&lt;/span&gt;&lt;br /&gt;&lt;br /&gt;&lt;blockquote&gt;&lt;p&gt;Aside: I hope you found that last example a bit startling. We're using the comma&lt;br /&gt;operator to build a string out of three sequences of characters -&lt;span style="font-family:courier new;"&gt; 'aci'&lt;/span&gt;, &lt;span style="font-family:courier new;"&gt;'e':8&lt;/span&gt; and &lt;span style="font-family:courier new;"&gt;'d'&lt;/span&gt;. The last one is just a single character; the other two are structures known as "list fragments". They're almost, but not quite lists.&lt;br /&gt;&lt;br /&gt;Some more examples of list fragments and the operators that generate them...&lt;br /&gt;&lt;span style="font-family:courier new;"&gt;4,5&lt;br /&gt;'h','e','l','l','o'&lt;br /&gt;1:6 &lt;span style="color:#006600;"&gt;//equivalent to 1,1,1,1,1,1.&lt;br /&gt;&lt;/span&gt;'hello' &lt;span style="color:#006600;"&gt;//equivalent to 'h','e','l','l','o'.&lt;br /&gt;&lt;/span&gt;1..5 &lt;span style="color:#006600;"&gt;//the "range" operator: equivalent to 1,2,3,4,5.&lt;br /&gt;&lt;/span&gt;'1'..'5' &lt;span style="color:#006600;"&gt;//equivalent to '12345'.&lt;br /&gt;&lt;/span&gt;/[4,5] &lt;span style="color:#006600;"&gt;//equivalent to 4,5 - the / operator is&lt;br /&gt;the inverse of [].&lt;/span&gt;&lt;/span&gt;&lt;span style="font-family:courier new;"&gt;&lt;span style="color:#006600;"&gt;&lt;br /&gt;&lt;/span&gt;&lt;/span&gt;&lt;br /&gt;Some of these look a bit like tuples in Python, but actually they're rather different: for a start, list fragments cannot contain other list fragments, because the only way to merge them is concatenation. The only thing you can make is one big list fragment, not a hierarchy.&lt;br /&gt;&lt;br /&gt;List fragments can be stored in a variable...&lt;br /&gt;&lt;span style="font-family:courier new;"&gt;@n = 1,2,3,4;&lt;br /&gt;&lt;/span&gt;&lt;br /&gt;...concatenated with the comma operator...&lt;br /&gt;&lt;span style="font-family:courier new;"&gt;44,n,99,n,44 &lt;span style="color:#006600;"&gt;// equivalent to 44,1,2,3,4,99,1,2,3,4,44; &lt;/span&gt;&lt;br /&gt;&lt;/span&gt;&lt;br /&gt;...and if you put a list fragment in square brackets, you get a full-fledged grownup list that you can index and do useful things with.&lt;br /&gt;&lt;span style="font-family:courier new;"&gt;[n] &lt;span style="color:#006600;"&gt;// equivalent to the list [1,2,3,4].&lt;/span&gt; &lt;/span&gt;&lt;/p&gt;&lt;p&gt;Yep, this is the same "every value is a single-element list" philosophy that I talked about in the last post. Unfortunately, since then I've realised that people will find it confusing that both [1,2,3] and 1,2,3 are legal (but subtly different) syntax for lists, and it'll be a pain having to convert between the two when something is supplied in the wrong form, and so on. I had to pick one as the "default" syntax: [1,2,3] won. Now a list fragment is a list that's been deliberately disabled: to index it you have to put square brackets around it.&lt;/p&gt;&lt;p&gt;Ok, where was I...? &lt;/p&gt;&lt;/blockquote&gt;&lt;br /&gt;&lt;br /&gt;Oh yeah.&lt;br /&gt;For even more flexibility, the colon operator can be followed by an arbitrary type instead of a number:&lt;br /&gt;&lt;br /&gt;&lt;span style="font-family:courier new;"&gt;[char:int] @o = []; &lt;span style="color:#006600;"&gt;// a list containing any number of characters.&lt;/span&gt;&lt;/span&gt;&lt;br /&gt;&lt;span style="font-family:courier new;"&gt;[char*] @p = []; &lt;span style="color:#006600;"&gt;// the same thing. (but more convenient.)&lt;/span&gt;&lt;/span&gt;&lt;br /&gt;&lt;br /&gt;That * operator is just one of the operators that let you construct complex types...&lt;br /&gt;&lt;br /&gt;&lt;span style="font-family:courier new;"&gt;4¦5 @q = 4; &lt;span style="color:#006600;"&gt;// can be either 4 or 5 - initially 4.&lt;/span&gt;&lt;/span&gt;&lt;br /&gt;&lt;span style="font-family:courier new;"&gt;&amp;gt;0 @r = 1; &lt;span style="color:#006600;"&gt;// any number greater than 0.&lt;/span&gt;&lt;/span&gt;&lt;br /&gt;&lt;span style="font-family:courier new;"&gt;&amp;gt;=0 @s = 0; &lt;span style="color:#006600;"&gt;// any number greater than or equal to 0.&lt;/span&gt;&lt;/span&gt;&lt;br /&gt;&lt;span style="font-family:courier new;"&gt;[int:&amp;gt;=1] @t = [0]; &lt;span style="color:#006600;"&gt;// a list of one or more integers.&lt;/span&gt;&lt;/span&gt;&lt;br /&gt;&lt;span style="font-family:courier new;"&gt;[int+] @u = [0]; &lt;span style="color:#006600;"&gt;// same thing.&lt;/span&gt;&lt;/span&gt;&lt;br /&gt;&lt;span style="font-family:courier new;"&gt;!null @v = 45; &lt;span style="color:#006600;"&gt;// any value except 'null'.&lt;/span&gt;&lt;br /&gt;&lt;/span&gt;&lt;br /&gt;You can also intersect types to make stricter types - just write them in series one after another.&lt;br /&gt;&lt;br /&gt;&lt;span style="font-family:courier new;"&gt;!0 int @w = 4; &lt;span style="color:#006600;"&gt;// a non-zero integer.&lt;/span&gt;&lt;/span&gt;&lt;br /&gt;&lt;span style="font-family:courier new;"&gt;&amp;gt;-100 &amp;lt;100 !0 int @x = 4; &lt;span style="color:#006600;"&gt;// a non-zero integer between -100 and 100.&lt;/span&gt;&lt;/span&gt;&lt;br /&gt;&lt;br /&gt;And finally, all of these types are first class values: you can store them in variables, return them from functions, and so on. Flan's equivalent of a C typedef expression is just a normal assignment:&lt;br /&gt;&lt;br /&gt;&lt;span style="font-family:courier new;"&gt;@natural = int &amp;gt;=0; &lt;span style="color:#006600;"&gt;// any natural number.&lt;/span&gt;&lt;/span&gt;&lt;br /&gt;&lt;span style="font-family:courier new;"&gt;@vowel = 'a'¦'e'¦'i'¦'o'¦'u'¦'A'¦'E'¦'I'¦'O'¦'U'; &lt;span style="color:#006600;"&gt;// any vowel.&lt;/span&gt;&lt;br /&gt;&lt;/span&gt;&lt;br /&gt;&lt;span style="font-family:courier new;"&gt;natural @y = 4; &lt;span style="color:#006600;"&gt;// a variable containing any natural number.&lt;/span&gt;&lt;/span&gt;&lt;br /&gt;&lt;span style="font-family:courier new;"&gt;vowel @z = 'o'; &lt;span style="color:#006600;"&gt;// a variable containing any vowel.&lt;/span&gt;&lt;br /&gt;&lt;/span&gt;&lt;br /&gt;Wow, that was a lot of code snippets. If it's a lot to digest, don't worry about it too much; for now let's move on to a discussion.&lt;br /&gt;&lt;br /&gt;&lt;u&gt;&lt;strong&gt;Practicalities:&lt;br /&gt;&lt;/strong&gt;&lt;/u&gt;The first question that probably comes to mind is - "Hey, didn't you call these static type declarations? How can you check a type like '&amp;gt;0' at compile time?"Well, there are several ways.&lt;br /&gt;1) Simple case: for a literal value or constant, e.g. "7", the value is known at compile time and can obviously be checked.&lt;br /&gt;2) Marginally less simple: If another variable is declared as the same type (or a stricter one), its contents are always guaranteed to be acceptable, so you can assign one to the other. And the same goes for function calls with an appropriate return type.&lt;br /&gt;3) Explicit type cast: If you're certain that a variable will satisfy a given type restriction, the ".assert" method allows you to force it to be treated as that type. Here's an example of this used in a function declaration...&lt;br /&gt;&lt;br /&gt;&lt;span style="font-family:courier new;"&gt;natural @abs(int @i)&lt;/span&gt;&lt;br /&gt;&lt;span style="font-family:courier new;"&gt;{&lt;/span&gt;&lt;br /&gt;&lt;span style="font-family:courier new;"&gt;&lt;span style="color:#ffffff;"&gt;..&lt;/span&gt;if ( i &gt;= 0 ) &lt;/span&gt;&lt;br /&gt;&lt;span style="font-family:courier new;"&gt;&lt;span style="color:#ffffff;"&gt;....&lt;/span&gt;return i.assert(natural);&lt;/span&gt;&lt;br /&gt;&lt;span style="font-family:courier new;"&gt;&lt;span style="color:#ffffff;"&gt;..&lt;/span&gt;else&lt;/span&gt;&lt;br /&gt;&lt;span style="font-family:courier new;"&gt;&lt;span style="color:#ffffff;"&gt;....&lt;/span&gt;return (-i).assert(natural);&lt;/span&gt;&lt;br /&gt;&lt;span style="font-family:courier new;"&gt;}&lt;br /&gt;&lt;/span&gt;&lt;br /&gt;Yep, this is the equivalent of a static typecast in C. As Bjarne Stroustrup has said, the syntax &lt;span style="font-family:courier new;"&gt;(type)value&lt;/span&gt; for casts in C was a rather unfortunate choice. Slightly dodgy operations like this should not be quite so hard to search for and hard to see. Unfortunately, I feel C++ overreacted with &lt;span style="font-family:courier new;"&gt;static-cast&amp;lt;type&amp;gt;&lt;type&gt;(value)&lt;/span&gt; - it's a ridiculously cumbersome construct. So far, &lt;span style="font-family:courier new;"&gt;value.assert(type)&lt;/span&gt; seems to be a good middle ground.&lt;br /&gt;&lt;br /&gt;As the name implies, the program will halt (in debug builds) if a type can't cope with the value it's given; another feature lacking from C's casts.&lt;br /&gt;&lt;br /&gt;4) Explicit type check: If you don't know whether a value satisfies a type, test it at runtime. For example:&lt;br /&gt;&lt;br /&gt;&lt;span style="font-family:courier new;"&gt;natural @abs(int @i)&lt;/span&gt;&lt;br /&gt;&lt;span style="font-family:courier new;"&gt;{&lt;/span&gt;&lt;br /&gt;&lt;span style="font-family:courier new;"&gt;&lt;span style="color:#ffffff;"&gt;..&lt;/span&gt;if ( i.is(natural) )&lt;/span&gt;&lt;br /&gt;&lt;span style="font-family:courier new;"&gt;&lt;span style="color:#ffffff;"&gt;....&lt;/span&gt;return i.assert(natural);&lt;/span&gt;&lt;br /&gt;&lt;span style="font-family:courier new;"&gt;&lt;span style="color:#ffffff;"&gt;..&lt;/span&gt;else&lt;/span&gt;&lt;br /&gt;&lt;span style="font-family:courier new;"&gt;&lt;span style="color:#ffffff;"&gt;....&lt;/span&gt;return (-i).assert(natural);&lt;/span&gt;&lt;br /&gt;&lt;span style="font-family:courier new;"&gt;}&lt;br /&gt;&lt;/span&gt;&lt;br /&gt;Every value supports the ".is" method - it's essentially the same as the "is" operator in C#. It takes a type as a parameter, and returns true or false depending on whether that type will accept the value.&lt;br /&gt;Unfortunately, as seen above, once you've determined that a value is of a given type, you usually want it to be treated as a value of that type... but in a statically-typed language, you'll have to explicitly cast it. This is a pain in the arse.To simplify cases like this, Flan lets you declare a new variable inside your pattern-match expression:&lt;br /&gt;&lt;br /&gt;&lt;span style="font-family:courier new;"&gt;natural @abs(int @i)&lt;/span&gt;&lt;br /&gt;&lt;span style="font-family:courier new;"&gt;{&lt;/span&gt;&lt;br /&gt;&lt;span style="font-family:courier new;"&gt;&lt;span style="color:#ffffff;"&gt;..&lt;/span&gt;if ( i.is(natural @n) )&lt;/span&gt;&lt;br /&gt;&lt;span style="font-family:courier new;"&gt;&lt;span style="color:#ffffff;"&gt;....&lt;/span&gt;return n;&lt;/span&gt;&lt;br /&gt;&lt;span style="font-family:courier new;"&gt;&lt;span style="color:#ffffff;"&gt;..&lt;/span&gt;else&lt;/span&gt;&lt;br /&gt;&lt;span style="font-family:courier new;"&gt;&lt;span style="color:#ffffff;"&gt;....&lt;/span&gt;return (-i).assert(natural);&lt;/span&gt;&lt;br /&gt;&lt;span style="font-family:courier new;"&gt;}&lt;br /&gt;&lt;/span&gt;&lt;br /&gt;Even cleaner, if you're doing this with a local variable, a "where" statement can be used to temporarily narrow its type:&lt;br /&gt;&lt;br /&gt;&lt;span style="font-family:courier new;"&gt;natural @abs(int @i)&lt;/span&gt;&lt;br /&gt;&lt;span style="font-family:courier new;"&gt;{&lt;/span&gt;&lt;br /&gt;&lt;span style="font-family:courier new;"&gt;&lt;span style="color:#ffffff;"&gt;..&lt;/span&gt;where i.is(natural)&lt;/span&gt;&lt;br /&gt;&lt;span style="font-family:courier new;"&gt;&lt;span style="color:#ffffff;"&gt;....&lt;/span&gt;return i;&lt;/span&gt;&lt;br /&gt;&lt;span style="font-family:courier new;"&gt;&lt;span style="color:#ffffff;"&gt;..&lt;/span&gt;else&lt;/span&gt;&lt;br /&gt;&lt;span style="font-family:courier new;"&gt;&lt;span style="color:#ffffff;"&gt;....&lt;/span&gt;return (-i).assert(natural);&lt;/span&gt;&lt;br /&gt;&lt;span style="font-family:courier new;"&gt;}&lt;br /&gt;&lt;/span&gt;&lt;br /&gt;In effect, within the body of the where statement, the type of the variable 'i' becomes (natural int).&lt;br /&gt;Frankly I'm not completely happy with the syntax of the "where" statement, but I'm definitely happy with how useful it is. As I develop the language further, I'm sure more conversion expressions will continue to suggest themselves.&lt;br /&gt;&lt;br /&gt;&lt;br /&gt;&lt;u&gt;&lt;strong&gt;Regular expressionescence: &lt;/strong&gt;&lt;/u&gt;&lt;br /&gt;We've now reached the point where I can demonstrate Flan's regular expression-like structures, as I mentioned earlier.&lt;br /&gt;&lt;br /&gt;&lt;span style="font-family:courier new;"&gt;@teststring = "hello";&lt;br /&gt;&lt;/span&gt;&lt;br /&gt;The expressions &lt;span style="font-family:courier new;"&gt;"foo"&lt;/span&gt;, &lt;span style="font-family:courier new;"&gt;['foo']&lt;/span&gt; and &lt;span style="font-family:courier new;"&gt;['f','o','o']&lt;/span&gt; are all equivalent. In other words, this defines that teststring is a list of characters: &lt;span style="font-family:courier new;"&gt;['h','e','l','l','o']&lt;/span&gt;.&lt;br /&gt;&lt;br /&gt;&lt;span style="font-family:courier new;"&gt;if ( teststring.is([char*,'o',char*]) ) ...&lt;br /&gt;&lt;/span&gt;&lt;br /&gt;Here we're defining a new type - "lists that contain some characters, then an 'o', then some more characters" - and testing whether "hello" is accepted by that type. Yes, it contains an 'o', so it's accepted.&lt;br /&gt;&lt;br /&gt;The test above is pretty cumbersome. Simple stuff should be simple, so instead of "is", you probably want to write "contains":&lt;br /&gt;&lt;br /&gt;&lt;span style="font-family:courier new;"&gt;if ( teststring.contains('o') ) ...&lt;br /&gt;&lt;/span&gt;&lt;br /&gt;What if you didn't care about 'o', but wanted to look for any vowel instead? Simple:&lt;br /&gt;&lt;br /&gt;&lt;span style="font-family:courier new;"&gt;if ( teststring.contains(vowel) ) ...&lt;br /&gt;&lt;/span&gt;(This is using the 'vowel' type I declared earlier.)&lt;br /&gt;&lt;br /&gt;What if you want to search for a particular sequence of characters, instead of a single character?&lt;br /&gt;&lt;br /&gt;&lt;span style="font-family:courier new;"&gt;if ( teststring.contains('l','l') ) ...&lt;br /&gt;&lt;/span&gt;&lt;br /&gt;Yes, "hello" does contain two consecutive 'l's.&lt;br /&gt;In case you were wondering, no, the contains function does not have a form that takes two arguments. I hope you were paying attention earlier: it's actually taking a list fragment! Notice that there aren't any square brackets here. We didn't write:&lt;br /&gt;&lt;br /&gt;&lt;span style="font-family:courier new;"&gt;if ( teststring.contains(['l','l']) ) ...&lt;br /&gt;&lt;/span&gt;&lt;br /&gt;That would treat teststring as a list of strings, and look to see whether any of them was the string "ll". (The test would fail, because teststring isn't a list of strings.)&lt;br /&gt;&lt;br /&gt;Ok, so far so good. What if you wanted to generalise this to accept any double letter? Well, I already showed how to declare temporary variables within a type. You can reuse these variables within the same pattern, like so:&lt;br /&gt;&lt;br /&gt;&lt;span style="font-family:courier new;"&gt;if ( teststring.contains(char @x, x) ) ...&lt;br /&gt;&lt;/span&gt;&lt;br /&gt;What if you don't care whether the characters are consecutive? No problem...&lt;br /&gt;&lt;br /&gt;&lt;span style="font-family:courier new;"&gt;if ( teststring.contains(char @x, char*, x) ) ...&lt;br /&gt;&lt;/span&gt;&lt;br /&gt;I could keep going, but hopefully you get the idea. (This post has already taken an unhealthy amount of time to write, anyway...)&lt;br /&gt;Although the Flan type system can't compete with the terseness of conventional regular expressions (they're pathologically terse), I think it's significantly more readable - mainly thanks to the way it lets you compose an expression out of simpler expressions.&lt;br /&gt;&lt;br /&gt;Thanks for reading! I'll leave you with some more examples of Flan code...&lt;br /&gt;&lt;br /&gt;&lt;span style="color:#006600;"&gt;&lt;span style="font-family:courier new;"&gt;// when used in a function declaration instead of the return type,&lt;/span&gt;&lt;br /&gt;&lt;span style="font-family:courier new;"&gt;// the "is" keyword signifies that it's actually a type declaration.&lt;/span&gt;&lt;br /&gt;&lt;span style="font-family:courier new;"&gt;// The resultant type can only accept values for which the function&lt;/span&gt;&lt;br /&gt;&lt;span style="font-family:courier new;"&gt;// body returns true.&lt;/span&gt;&lt;br /&gt;&lt;/span&gt;&lt;span style="font-family:courier new;"&gt;is @even( int @n )&lt;/span&gt;&lt;br /&gt;&lt;span style="font-family:courier new;"&gt;{&lt;/span&gt;&lt;br /&gt;&lt;span style="font-family:courier new;"&gt;&lt;span style="color:#ffffff;"&gt;..&lt;/span&gt;n.mod(2).is(0);&lt;/span&gt;&lt;br /&gt;&lt;span style="font-family:courier new;"&gt;}&lt;br /&gt;&lt;/span&gt;&lt;br /&gt;&lt;span style="font-family:courier new;"&gt;even @x = 2;&lt;/span&gt;&lt;br /&gt;&lt;span style="font-family:courier new;"&gt;even @y = 3; &lt;span style="color:#006600;"&gt;// compiler complains because 3.mod(2) is 1, not 0.&lt;/span&gt;&lt;br /&gt;&lt;/span&gt;&lt;br /&gt;&lt;span style="color:#006600;"&gt;&lt;span style="font-family:courier new;"&gt;// in Flan, what would be a template in some languages&lt;/span&gt;&lt;br /&gt;&lt;span style="font-family:courier new;"&gt;// is simply a function that returns a type.&lt;/span&gt;&lt;br /&gt;&lt;span style="font-family:courier new;"&gt;// oneof([1,2,3]) is equivalent to the type 1¦2¦3.&lt;/span&gt;&lt;/span&gt;&lt;br /&gt;&lt;span style="font-family:courier new;"&gt;is @oneof( [value*] @v )( value @x )&lt;/span&gt;&lt;br /&gt;&lt;span style="font-family:courier new;"&gt;{&lt;/span&gt;&lt;br /&gt;&lt;span style="font-family:courier new;"&gt;&lt;span style="color:#ffffff;"&gt;..&lt;/span&gt;v.contains(x);&lt;/span&gt;&lt;br /&gt;&lt;span style="font-family:courier new;"&gt;}&lt;/span&gt;&lt;br /&gt;&lt;br /&gt;&lt;span style="font-family:courier new;color:#006600;"&gt;// some useful types for characters:&lt;/span&gt;&lt;br /&gt;&lt;span style="font-family:courier new;"&gt;@digit = oneof(['0'..'9']);&lt;/span&gt;&lt;br /&gt;&lt;span style="font-family:courier new;"&gt;@lowercase = oneof(['a'..'z']);&lt;/span&gt;&lt;br /&gt;&lt;span style="font-family:courier new;"&gt;@uppercase = oneof(['A'..'Z']);&lt;/span&gt;&lt;br /&gt;&lt;span style="font-family:courier new;"&gt;@letter = lowercase¦uppercase;&lt;/span&gt;&lt;br /&gt;&lt;span style="font-family:courier new;"&gt;@alphanum = letter¦digit;&lt;/span&gt;&lt;br /&gt;&lt;span style="font-family:courier new;"&gt;@whitespace = ' '¦'\t'¦'\n'¦'\r';&lt;br /&gt;&lt;/span&gt;&lt;br /&gt;&lt;span style="font-family:courier new;color:#006600;"&gt;// a type that matches strings from "0" to "255"&lt;/span&gt;&lt;br /&gt;&lt;span style="font-family:courier new;"&gt;is @string0to255( [digit:1..3] @s )&lt;/span&gt;&lt;br /&gt;&lt;span style="font-family:courier new;"&gt;{&lt;/span&gt;&lt;br /&gt;&lt;span style="font-family:courier new;"&gt;&lt;span style="color:#ffffff;"&gt;..&lt;/span&gt;s.toNumber().is( &gt;=0 &lt;=255 );&lt;/span&gt;&lt;br /&gt;&lt;span style="font-family:courier new;"&gt;}&lt;/span&gt;&lt;span style="font-family:courier new;"&gt;&lt;br /&gt;&lt;/span&gt;&lt;span style="font-family:courier new;"&gt;&lt;br /&gt;string0to255 @str1 = "177";&lt;br /&gt;string0to255 @str2 = "256"; &lt;span style="color:#006600;"&gt;// compiler complains&lt;/span&gt;&lt;br /&gt;&lt;br /&gt;&lt;/span&gt;&lt;span style="font-family:courier new;color:#006600;"&gt;// a type that matches IP addresses&lt;/span&gt;&lt;br /&gt;&lt;/span&gt;&lt;span style="font-family:courier new;"&gt;@ipAddress = (/str0to255,'.'):3, /str0to255;&lt;/span&gt;&lt;br /&gt;&lt;br /&gt;&lt;span style="font-family:courier new;color:#006600;"&gt;// one (inefficient) way to implement the "contains" method we saw earlier.&lt;/span&gt;&lt;br /&gt;&lt;span style="font-family:courier new;"&gt;bool value. @contains( type @t )&lt;/span&gt;&lt;br /&gt;&lt;span style="font-family:courier new;"&gt;{&lt;/span&gt;&lt;br /&gt;&lt;span style="font-family:courier new;"&gt;&lt;span style="color:#ffffff;"&gt;..&lt;/span&gt;return this.is([value*,t,value*]);&lt;/span&gt;&lt;br /&gt;&lt;span style="font-family:courier new;"&gt;}&lt;/span&gt;&lt;div class="blogger-post-footer"&gt;&lt;img width='1' height='1' src='https://blogger.googleusercontent.com/tracker/14566585-114249765324506912?l=makingflan.blogspot.com'/&gt;&lt;/div&gt;</content><link rel='replies' type='application/atom+xml' href='http://makingflan.blogspot.com/feeds/114249765324506912/comments/default' title='Post Comments'/><link rel='replies' type='text/html' href='https://www.blogger.com/comment.g?blogID=14566585&amp;postID=114249765324506912' title='5 Comments'/><link rel='edit' type='application/atom+xml' href='http://www.blogger.com/feeds/14566585/posts/default/114249765324506912'/><link rel='self' type='application/atom+xml' href='http://www.blogger.com/feeds/14566585/posts/default/114249765324506912'/><link rel='alternate' type='text/html' href='http://makingflan.blogspot.com/2006/03/regular-expressionescence.html' title='Regular expressionescence'/><author><name>Laurie Cheers</name><uri>http://www.blogger.com/profile/09919006779080998004</uri><email>noreply@blogger.com</email><gd:extendedProperty xmlns:gd='http://schemas.google.com/g/2005' name='OpenSocialUserId' value='16659230163727763737'/></author><thr:total xmlns:thr='http://purl.org/syndication/thread/1.0'>5</thr:total></entry><entry><id>tag:blogger.com,1999:blog-14566585.post-113929530480420347</id><published>2006-02-06T21:46:00.000-08:00</published><updated>2006-02-08T03:14:03.963-08:00</updated><title type='text'>Why "Flan"?</title><content type='html'>For my university dissertation, I designed and implemented an experimental programming language. (Funny, but all my personal projects seem to end up implementing a language of some kind.)&lt;br /&gt;&lt;br /&gt;Its main experimental feature was an unusual way for lists to work. The idea: what if every primitive value was effectively a single-element list, and the only way you had to combine them was concatenation via the comma operator?&lt;br /&gt;&lt;br /&gt;&lt;span style="font-family:courier new;"&gt;a = 1 // the list [1]&lt;/span&gt;&lt;br /&gt;&lt;span style="font-family:courier new;"&gt;b = 2,3,4 // the list [2,3,4]&lt;/span&gt;&lt;br /&gt;&lt;span style="font-family:courier new;"&gt;c = a,b // the list [1,2,3,4]&lt;/span&gt;&lt;br /&gt;&lt;br /&gt;There's one immediately obvious consequence: there's no way, syntactically, to express lists of lists. In a fit of cute, I called the language Flatlang, because all its lists were flat.&lt;br /&gt;(In case you're wondering: lists of pointers to lists were legal. So in a sense, C works the same way.)&lt;br /&gt;&lt;br /&gt;Flan, my spiritual successor to &lt;strong&gt;Fla&lt;/strong&gt;tla&lt;strong&gt;n&lt;/strong&gt;g, retains this concept as the core of the language, but unlike its predecessor, it actually (I hope to show) it makes this a useful feature. (In Flatlang it wasn't, really. The rest of the language didn't really harmonize with it; in my dissertation I called it a gimmick.)&lt;br /&gt;&lt;br /&gt;To demonstrate the power of the flat list, I'm going to have to introduce some other features first. They're pretty cool, though, so I don't think you'll get bored.&lt;br /&gt;&lt;br /&gt;&lt;span style="font-family:courier new;color:#660000;"&gt;@mylist = 1,2,3,2,1;&lt;/span&gt;&lt;br /&gt;&lt;br /&gt;Oh, ok, this bit's not especially cool.&lt;br /&gt;Not much to say here - I'm simply declaring that the identifier "mylist" corresponds to the list [1,2,3,2,1]. &lt;a href="http://makingflan.blogspot.com/2005/07/beginning.html"&gt;Recall&lt;/a&gt; that the @ prefix is my syntax for declaring a new identifier.&lt;br /&gt;&lt;br /&gt;&lt;span style="font-family:courier new;color:#660000;"&gt;print(each in mylist);&lt;/span&gt;&lt;br /&gt;&lt;br /&gt;This prints &lt;span style="font-family:courier new;color:#006600;"&gt;12321&lt;/span&gt;. The "&lt;span style="font-family:courier new;color:#660000;"&gt;each&lt;/span&gt;...&lt;span style="font-family:courier new;color:#660000;"&gt;in&lt;/span&gt;" operator is very powerful. It's derived from an idea in Jonathan Blow's language &lt;a href="http://lerp.org/"&gt;Lerp&lt;/a&gt;; essentially, it wraps an expression in a C#-style "foreach" loop. So this could be rewritten as:&lt;br /&gt;&lt;br /&gt;&lt;span style="font-family:courier new;color:#660000;"&gt;foreach @x in mylist&lt;br /&gt;{&lt;br /&gt;&lt;span style="color:#ffffff;"&gt;..&lt;/span&gt;print( x );&lt;br /&gt;}&lt;/span&gt;&lt;br /&gt;&lt;br /&gt;(This is Flan, not C#, so when we declare a loop variable we use the @ syntax.)&lt;br /&gt;There's more to come...&lt;br /&gt;&lt;br /&gt;&lt;span style="font-family:courier new;color:#660000;"&gt;print(1 + each in mylist);&lt;/span&gt;&lt;br /&gt;&lt;span style="font-family:Georgia;color:#000000;"&gt;&lt;/span&gt;&lt;br /&gt;Now we're starting to get somewhere! The above prints &lt;span style="font-family:courier new;color:#006600;"&gt;23432&lt;/span&gt;. It's equivalent to writing:&lt;br /&gt;&lt;br /&gt;&lt;span style="font-family:courier new;color:#660000;"&gt;foreach @x in mylist&lt;/span&gt;&lt;br /&gt;&lt;span style="font-family:courier new;color:#660000;"&gt;{&lt;/span&gt;&lt;br /&gt;&lt;span style="font-family:courier new;color:#660000;"&gt;&lt;span style="color:#ffffff;"&gt;..&lt;/span&gt;print( 1+x );&lt;/span&gt;&lt;br /&gt;&lt;span style="font-family:courier new;color:#660000;"&gt;}&lt;/span&gt;&lt;br /&gt;&lt;br /&gt;Yes, that's a simple and (more importantly) &lt;em&gt;natural&lt;/em&gt; syntax for &lt;span style="font-family:courier new;color:#660000;"&gt;map&lt;/span&gt;. (I mean the function, not the data structure). I think the unnaturalness of many higher-level functions is one of the key obstacles to the mainstream acceptance of languages like Lisp or Haskell. There's no question that &lt;span style="font-family:courier new;color:#660000;"&gt;map&lt;/span&gt; expresses an abstraction of something that we do all the time... but every time I do it, I have to rearrange what's in my head to match how &lt;span style="font-family:courier new;color:#660000;"&gt;map&lt;/span&gt; makes me express it.&lt;br /&gt;With the syntax above, I can simply write down what I want to do.&lt;br /&gt;&lt;br /&gt;So far all we've done is transform the individual items in a list. In the real world we're going to need the ability to operate on the whole list. Come up here and take a bow, &lt;span style="font-family:courier new;color:#660000;"&gt;sum&lt;/span&gt;:&lt;br /&gt;&lt;br /&gt;&lt;span style="font-family:courier new;color:#660000;"&gt;print( sum( mylist ));&lt;/span&gt;&lt;br /&gt;&lt;br /&gt;Not much to say there. sum takes a list, adds up its elements, and returns the total. This prints &lt;span style="font-family:courier new;color:#006600;"&gt;9&lt;/span&gt;.&lt;br /&gt;&lt;br /&gt;&lt;span style="font-family:courier new;color:#660000;"&gt;print( sum( 1+each in mylist ) );&lt;/span&gt;&lt;br /&gt;&lt;br /&gt;And this prints... &lt;span style="font-family:courier new;color:#006600;"&gt;23432&lt;/span&gt; again? Oops. Well, every language has its gotchas.&lt;br /&gt;Why does this happen? Let's do the rearranging thing:&lt;br /&gt;&lt;span style="font-family:courier new;color:#660000;"&gt;&lt;/span&gt;&lt;br /&gt;&lt;span style="font-family:courier new;color:#660000;"&gt;foreach @x in mylist&lt;/span&gt;&lt;br /&gt;&lt;span style="font-family:courier new;color:#660000;"&gt;{&lt;/span&gt;&lt;br /&gt;&lt;span style="font-family:courier new;color:#660000;"&gt;&lt;span style="color:#ffffff;"&gt;..&lt;/span&gt;print(sum(1+x));&lt;/span&gt;&lt;br /&gt;&lt;span style="font-family:courier new;color:#660000;"&gt;}&lt;/span&gt;&lt;br /&gt;&lt;br /&gt;Huh... when I said that "each" wraps a foreach loop around the whole statement, I really did mean the &lt;em&gt;whole statement&lt;/em&gt;. We're calling &lt;span style="font-family:courier new;color:#660000;"&gt;sum&lt;/span&gt; five times, and passing it one number at a time. To control this we're going to need a bit more new syntax...&lt;br /&gt;&lt;br /&gt;&lt;span style="font-family:courier new;color:#660000;"&gt;print( sum #( 1+each in mylist ));&lt;/span&gt;&lt;br /&gt;&lt;br /&gt;There we go! This prints &lt;span style="font-family:courier new;color:#006600;"&gt;14&lt;/span&gt; as expected.&lt;br /&gt;# means, essentially, "put the foreach loop here". It collects up an "each" loop into a list, so that the code outside can treat it as a whole. I'd prefer to use an english term that captured this (&lt;span style="font-family:courier new;color:#660000;"&gt;all&lt;/span&gt;?), but I don't think a clear one exists.&lt;br /&gt;It's also not obvious how to rearrange this example. The nicest way to express it without using &lt;span style="font-family:courier new;color:#660000;"&gt;#&lt;/span&gt; or &lt;span style="font-family:courier new;color:#660000;"&gt;map&lt;/span&gt; is probably:&lt;br /&gt;&lt;br /&gt;&lt;span style="font-family:courier new;color:#660000;"&gt;@temp = _; &lt;span style="color:#006600;"&gt;// temp is the empty list&lt;/span&gt;&lt;/span&gt;&lt;br /&gt;&lt;span style="font-family:courier new;color:#660000;"&gt;foreach( @x in mylist )&lt;/span&gt;&lt;br /&gt;&lt;span style="font-family:courier new;color:#660000;"&gt;{&lt;/span&gt;&lt;br /&gt;&lt;span style="font-family:courier new;color:#660000;"&gt;&lt;span style="color:#ffffff;"&gt;..&lt;/span&gt;temp = temp,1+x; &lt;span style="color:#006600;"&gt;// append to temp&lt;/span&gt;&lt;/span&gt;&lt;br /&gt;&lt;span style="font-family:courier new;color:#660000;"&gt;}&lt;/span&gt;&lt;br /&gt;&lt;span style="font-family:courier new;color:#660000;"&gt;print&lt;/span&gt;&lt;span style="font-family:courier new;color:#660000;"&gt;( sum( temp ));&lt;/span&gt;&lt;br /&gt;&lt;br /&gt;Bleah.&lt;br /&gt;Anyway, one more new feature...&lt;br /&gt;&lt;br /&gt;&lt;span style="font-family:courier new;color:#660000;"&gt;print( x*x foreach @x in mylist );&lt;/span&gt;&lt;br /&gt;&lt;br /&gt;This prints the square of each number; &lt;span style="font-family:courier new;color:#006600;"&gt;14941&lt;/span&gt;. This one can be rearranged to:&lt;br /&gt;&lt;br /&gt;&lt;span style="font-family:courier new;color:#660000;"&gt;foreach( @x in mylist )&lt;/span&gt;&lt;br /&gt;&lt;span style="font-family:courier new;color:#660000;"&gt;{&lt;/span&gt;&lt;br /&gt;&lt;span style="font-family:courier new;color:#660000;"&gt;&lt;span style="color:#ffffff;"&gt;..&lt;/span&gt;print( x*x );&lt;/span&gt;&lt;br /&gt;&lt;span style="font-family:courier new;color:#660000;"&gt;}&lt;/span&gt;&lt;br /&gt;&lt;br /&gt;Or if you prefer, it can also be rearranged to&lt;br /&gt;&lt;br /&gt;&lt;span style="font-family:courier new;color:#660000;"&gt;print(x*x) foreach @x in mylist;&lt;/span&gt;&lt;br /&gt;&lt;br /&gt;Until now the "foreach" statements I've shown you have been laid out like a C# foreach loop, but Flan allows them to be rearranged to whatever feels more natural - much like &lt;span style="font-family:courier new;color:#660000;"&gt;if&lt;/span&gt; statements in perl.&lt;br /&gt;&lt;br /&gt;Phew. Ok, I know I said I'd explain the advantage of flat lists, but I think this is enough for one post.&lt;br /&gt;Thanks for reading; join me next time when I cover &lt;span style="font-family:courier new;color:#660000;"&gt;accumulate&lt;/span&gt;, and explain why flat lists are cool.&lt;br /&gt;&lt;br /&gt;&lt;br /&gt;&lt;br /&gt;PS:&lt;br /&gt;&lt;em&gt;If you're interested, why not try these exercises? Then post a comment to let me know what I've failed to explain...&lt;br /&gt;&lt;br /&gt;1) Print each number in mylist multiplied by 100.&lt;br /&gt;2) Print each number in mylist divided by 2. (use the / operator.)&lt;br /&gt;3) Print the sum of squares of the numbers in mylist. (i.e. 1*1 + 2*2 + 3*3 + 2*2 + 1*1.)&lt;br /&gt;4) Print the length of mylist.&lt;br /&gt;5) Generate a "stuttering" version of mylist, with each element repeated: [1,1,2,2,3,3,2,2,1,1].&lt;br /&gt;(Recall that I defined the comma operator to mean "concatenate".)&lt;br /&gt;&lt;/em&gt;&lt;br /&gt;Answers: (highlight to read)&lt;br /&gt;1) &lt;span style="font-family:courier new;color:#ffffff;"&gt;print( 100*each in mylist );&lt;/span&gt;&lt;br /&gt;&lt;br /&gt;2)&lt;span style="color:#000000;"&gt; &lt;/span&gt;&lt;span style="font-family:courier new;color:#ffffff;"&gt;print( (each in mylist)/2 );&lt;/span&gt;&lt;br /&gt;&lt;br /&gt;3) &lt;span style="font-family:courier new;color:#ffffff;"&gt;print( sum#( x*x foreach @x in mylist ));&lt;/span&gt;&lt;br /&gt;&lt;span style="font-family:Courier New;color:#ffffff;"&gt;&lt;/span&gt;&lt;br /&gt;4) &lt;span style="font-family:courier new;color:#660000;"&gt;&lt;span style="color:#ffffff;"&gt;print( sum#( 1 foreach in mylist ));&lt;/span&gt;&lt;/span&gt;&lt;br /&gt;&lt;br /&gt;5) &lt;span style="color:#ffffff;"&gt;&lt;span style="font-family:courier new;"&gt;x,x foreach @x in mylist;&lt;/span&gt;&lt;br /&gt;&lt;br /&gt;&lt;/span&gt;&lt;span style="color:#ffffff;"&gt;&lt;/span&gt;&lt;div class="blogger-post-footer"&gt;&lt;img width='1' height='1' src='https://blogger.googleusercontent.com/tracker/14566585-113929530480420347?l=makingflan.blogspot.com'/&gt;&lt;/div&gt;</content><link rel='replies' type='application/atom+xml' href='http://makingflan.blogspot.com/feeds/113929530480420347/comments/default' title='Post Comments'/><link rel='replies' type='text/html' href='https://www.blogger.com/comment.g?blogID=14566585&amp;postID=113929530480420347' title='2 Comments'/><link rel='edit' type='application/atom+xml' href='http://www.blogger.com/feeds/14566585/posts/default/113929530480420347'/><link rel='self' type='application/atom+xml' href='http://www.blogger.com/feeds/14566585/posts/default/113929530480420347'/><link rel='alternate' type='text/html' href='http://makingflan.blogspot.com/2006/02/why-flan.html' title='Why &quot;Flan&quot;?'/><author><name>Laurie Cheers</name><uri>http://www.blogger.com/profile/09919006779080998004</uri><email>noreply@blogger.com</email><gd:extendedProperty xmlns:gd='http://schemas.google.com/g/2005' name='OpenSocialUserId' value='16659230163727763737'/></author><thr:total xmlns:thr='http://purl.org/syndication/thread/1.0'>2</thr:total></entry><entry><id>tag:blogger.com,1999:blog-14566585.post-112161973222887659</id><published>2005-07-18T15:35:00.000-07:00</published><updated>2006-02-06T21:46:19.330-08:00</updated><title type='text'>@beginning</title><content type='html'>Well, isn't this nice? I have no idea what the good folks at Blogger are getting out of this arrangement, but I sure appreciate having a free blog page.&lt;br /&gt;&lt;br /&gt;Manifesto: This is my website for documenting the design of the programming language Flan.&lt;br /&gt;I've been thinking about it and making notes about ideas for about a year and a half now, but hopefully this will be a good way to get it all in one place. And maybe even get some good suggestions from the general public.&lt;br /&gt;&lt;br /&gt;First principles -&lt;br /&gt;This is a language designed by me, for me. It would be nice if someone else in the world found it useful... but as long as I do, I'll consider it a success. So ner.&lt;br /&gt;&lt;br /&gt;I'm most experienced as a C++ programmer, and it's becoming clear to me (and plenty of others, I'm sure) that the language has some annoying features in it. The Boost libraries go a remarkable way towards fixing them, but they're unreadable and unmaintainable. If I want a little feature that Boost doesn't provide, how do I add it? I wouldn't know where to start.&lt;br /&gt;&lt;br /&gt;My wife says I should mention her. So I am. Happy, dear?&lt;br /&gt;&lt;br /&gt;I don't have any old C programs that I want to convert, so I'm not worried about backwards compatibility. I'm just going to invent a new language from the ground up, with all the really whizzy new features that I think a language needs.&lt;br /&gt;&lt;br /&gt;Let's start with something simple and petty:&lt;br /&gt;In C++, I find declarations a little hard to spot. It would be useful to have an unambiguous, visually large prefix symbol that marks when you declare something. For instance, to declare a variable x, you could write something like "&lt;span style="font-family:courier new;"&gt;int @x = 4&lt;/span&gt;" or "&lt;span style="font-family:courier new;"&gt;int #x = 4&lt;/span&gt;". I'm going to go with @.&lt;br /&gt;(I'll be using # for some other stuff later.)&lt;br /&gt;&lt;br /&gt;Convenient results of this -&lt;br /&gt;- Declarations stand out as you read the text.&lt;br /&gt;&lt;br /&gt;- You can trivially perform a text search for the place where something is declared. (To help, I'm going to forbid whitespace between the @ and the new identifier.)&lt;br /&gt;&lt;br /&gt;- Now that declarations are self-evident even without a type specification, we don't need one if the type is implicitly specified in other ways - i.e. constants.&lt;br /&gt;For example, "&lt;span style="font-family:courier new;"&gt;const double @pi = 3.14159&lt;/span&gt;" is redundant. It can only evaluate to 3.14159, which we know is a double. So we can abbreviate it to "&lt;span style="font-family:courier new;"&gt;const @pi = 3.14159&lt;/span&gt;".&lt;br /&gt;Indeed, since only constants can be abbreviated this way (the same assumption can't be made for variables - they might need to store other data later), the "const" is implicit and we can actually just write: &lt;span style="font-family:courier new;"&gt;@pi = 3.14159&lt;/span&gt;.&lt;br /&gt;&lt;br /&gt;- Now that a constant doesn't need an explicit type specification, it makes sense to declare it in a function call. "Out" parameters usually require you to declare a variable to store their results.&lt;br /&gt;&lt;span style="font-family:courier new;"&gt;vec3d pos;&lt;/span&gt;&lt;br /&gt;&lt;span style="font-family:courier new;"&gt;getPosition(&amp;pos);&lt;/span&gt;&lt;br /&gt;I hate doing unnecessary typing, so I'm going to let you declare a constant and infer its type from the function signature, thus simplifying this down to:&lt;br /&gt;&lt;span style="font-family:courier new;"&gt;getPosition(&amp;@pos);&lt;/span&gt;&lt;br /&gt;This saves you some typing and also makes it trivially obvious to the reader that getPosition won't (or at least, shouldn't) use any predefined data stored in pos.&lt;br /&gt;Obviously the compiler will complain if the function is overloaded so that it's ambiguous what type should be used.&lt;br /&gt;&lt;br /&gt;&lt;br /&gt;That's about it for now.&lt;br /&gt;Next time I'll be taking the &lt;span style="font-family:courier new;"&gt;&amp;&lt;/span&gt; out of &lt;span style="font-family:courier new;"&gt;getPosition(&amp;amp;@pos)&lt;/span&gt;...&lt;div class="blogger-post-footer"&gt;&lt;img width='1' height='1' src='https://blogger.googleusercontent.com/tracker/14566585-112161973222887659?l=makingflan.blogspot.com'/&gt;&lt;/div&gt;</content><link rel='replies' type='application/atom+xml' href='http://makingflan.blogspot.com/feeds/112161973222887659/comments/default' title='Post Comments'/><link rel='replies' type='text/html' href='https://www.blogger.com/comment.g?blogID=14566585&amp;postID=112161973222887659' title='0 Comments'/><link rel='edit' type='application/atom+xml' href='http://www.blogger.com/feeds/14566585/posts/default/112161973222887659'/><link rel='self' type='application/atom+xml' href='http://www.blogger.com/feeds/14566585/posts/default/112161973222887659'/><link rel='alternate' type='text/html' href='http://makingflan.blogspot.com/2005/07/beginning.html' title='@beginning'/><author><name>Laurie Cheers</name><uri>http://www.blogger.com/profile/09919006779080998004</uri><email>noreply@blogger.com</email><gd:extendedProperty xmlns:gd='http://schemas.google.com/g/2005' name='OpenSocialUserId' value='16659230163727763737'/></author><thr:total xmlns:thr='http://purl.org/syndication/thread/1.0'>0</thr:total></entry></feed>