Nervous about Mathematica

My computational biology class requires us to use Mathematica. I'm not very familiar with it yet, but I've discovered a couple basic design flaws that make me extremely uneasy using it.

Mathematica makes no syntactic distinction between symbols and variables. For example, if I want a function named Foo to return the symbol "NA" when attempting to divide by zero, I can just write:

Foo[x_, y_] := If[y == 0, NA, x/y]

This works as long as NA doesn't have a value assigned to it. However if NA were to be set to, say, 3, then the function would just return 3. The same behavior causes problems if you try to use the "/." operator as if it's like a where clause in Haskell. For example, the following will return x+1 as long as q doesn't already have a value:

Add1[x_] := x + q /. q -> 1

But if q were to be set to 4, then Add1[2] would return 6. Sigh.

And while I'm at it, what's up with requiring underscores for the formal parameter list of a function? If you omit the underscore, you get some really weird behavior. Consider the following function definition:

Foo[qwerty] := 5

If qwerty doesn't have a value yet, then this function will return the value 5 if and only if you pass it the symbol "qwerty". However, suppose qwerty had the value 9. Then the function will return 5 if and only if you pass 9. Now for the kicker: suppose qwerty was undefined when the function was defined, but someone assigned a value to qwerty at some point later on. Now the expression "Foo[qwerty]" will fail, and just return "Foo[qwerty]". Gah!

I'm not a careful enough programmer to be able to deal with this kind of crazyness. Especially in the absence of lexical scoping. I officially hate this language (opinion subject to change should I receive a lobotomy at some point in the future).

Posted on October 11, 2006 08:43 PM
More languages articles

Comments

Mathematica is bizarre, but there is method to the madness. For help with your local variable woes, heck out "Scoping" in the online documentation:

http://documents.wolfram.com/mathematica/Built-inFunctions/Programming/Scoping/index.en.html

I don't know if there's anything you can do about your NA example, but in the second one you can guarantee that q is unbound by using Module.

The underscores for formal parameters reflect the fact that they're not really formal parameters as we are accustomed to using in other languages. Mathematica uses lisp-inspired tree structures to represent all expressions, but they are evaluated using a pattern matcher (something along the lines of Prolog). Underscores are used to introduce variables ("blanks") in the patterns; these blanks can be named (just like we might refer to bits of matched pattern in regular expression substitutions) giving the illusion of formal parameters. You can also put on qualifiers to ensure that the blank only matches a pattern satisfying some predicate by putting the predicate after the underscore.

Overall I find the syntax dissatisfying (at least it never *looks* pretty; they adopted Lisp-like expressions but no Lisp-like indentation standards) but it can be fun once you figure out how it works.

Here are a few of my own ponderings while learning Mathematica: http://nibot-lab.livejournal.com/tag/mathematica

Posted by: Tobin Fricke at October 11, 2006 10:14 PM

I think the rationale behind Mathematica's syntax was that they wanted to use Lisp, but hide the Lispiness of it as much as possible. They really go out of their way to avoid () parentheses if at all possible.

Posted by: crzwdjk at October 11, 2006 10:20 PM

Use Block or Module to localize variables and protect against non localized variables from elsewhere.

If you want to work with global variables in a session, then you can prevent them being assigned values later in the session (or from values being changed or cleared) with Protect.

Posted by: Jon at October 27, 2006 12:40 PM

Underscores are used to introduce variables ("blanks") in the patterns; these blanks can be named Ed Hardy (just like we might refer to bits of matched pattern in regular expression substitutions) giving the illusion of formal parameters. tiffany jewelry You can also put on qualifiers to ensure that the blank only matches a pattern satisfying ed hardy some predicate by putting the predicate after the underscore.

Posted by: at September 30, 2009 03:22 AM

Your article very interesting, I have introduced a lot of friends look at this article, the content of the articles there will be a lot of attractive people to appreciate, I have to thank you such an article.

Posted by: ugg boots sale at October 23, 2009 10:12 PM

There is nothing that touches these for just lazing around at home. Bar sleeping I don’t take mine off now that its getting colder, they are not quite as stiff as they were when they first arrived but still extremely comfortable. It would be nice if they had a little more arch support but other than that I’m very pleased. I am finding a nice ugg boots!

Posted by: ugg boots at November 5, 2009 02:42 AM

Just wanted to say great job with the blog, today is my first visit here and I’ve enjoyed reading your posts so far

Mukluk boots

Posted by: Mukluk boots at November 5, 2009 02:43 AM

Everything dynamic and very positively!

NIKE

Posted by: NIKE at November 5, 2009 02:44 AM

Good work, thanks! My sister received the Mbt Health shoes I bought her for her birthday, she adores them. I was a little worried the calf would be tight around her legs but it hasn’t been a problem. I’ll be back before Christmas.


Posted by: Mbt Health shoes at November 5, 2009 02:44 AM

I think I will try to recommend this post to my friends and family, cuz it’s really helpful.

Thanks for this nice post, i like it very much, write this article more.Nike Air Max 2003 Shoes
.

Posted by: Nike Air Max 2003 Shoes at November 5, 2009 02:44 AM

Only One word to characterize such a great post “WOW” that was a very interesting read
Adidas

Posted by: Adidas at November 5, 2009 02:44 AM

Find Jeans ?!UGG boots On Sale ,Enjoy free shipping + bargain sale now!

UGG Classic Short,100% Authentic UGG Australia Boots!ALL of

Jeans are the cheapest and best for you!Enjoy yourself of shoping , Find Cheap Sheepskin
Jeans !



Posted by: Jeans at November 5, 2009 02:45 AM

We also supply other products such as:

Mukluk boots


Shoes


NIKE


Jeans


Handbags


Lacoste Shoes


Jeans


Watches


Handbag


Adidas


DVD .Welcome to order it.

Posted by: surprise at November 5, 2009 02:45 AM

Hey, i am very happy today, I received my Mbt shoes

just now, very impressed. They are like sort of a massive slipper, soft, easy to wear and

light.

Posted by: Mbt shoes at November 5, 2009 02:45 AM

Hey, i am very happy today, I received my Mbt shoes

just now, very impressed. They are like sort of a massive slipper, soft, easy to wear and

light.

Posted by: Mbt shoes at November 5, 2009 02:46 AM

Uggs on sale now.UGG Classic Cardy Boot makes me different form the other

girls. The UGG Bailey Button Boot is a good choice for female.

Posted by: ugg classic cardy boots at November 17, 2009 12:08 AM

Louis Vuitton , commonly referred to as Louis Vuitton Continues Its Expansion to Scotland and LV 09 FW Gossip, or sometimes shortened to 09 New Arrivals has become one of the most Burberry Agendas luxurybrands Chanel.

Posted by: 1 at November 17, 2009 01:30 AM

UGG Bailey Button bootsis a new style in 2009.The classic cardy uggs boots is another hot boots that worth of buying.And the classic tall ugg boots will make your winter amusing.And now uggs on sale,if you are looking for such a boot,the ugg boots is good choice this year.

Posted by: UGG Bailey Button Boots at November 17, 2009 01:39 AM

I love Women's UGG Classic Cardy Boot 5819 very much. And the new style UGG Mayfaire Boots is aslo a good choice this year.

Posted by: UGG Bailey Button Fancy Boots at November 17, 2009 03:29 AM

www.enjoywholesale.com

china wholesale clothing,china wholesale shoes,china wholesale electronics,china wholesale suppliers,china manufacturers

china wholesale products,light in the box,wholesale lots,wholesale ipod
china direct,china dropship,china trade,made in china

electronics wholesaler,ebay wholesaler,financial wholesaler
fashion wholesaler,clothing wholesaler,wholesaler magazine

computer wholesaler,external wholesaler,dvd wholesaler,wholesaler definition,china wholesaler,mutual fund wholesaler,define wholesaler,insurance wholesaler


discount ugg boots
forture
ugg boots
Discount store

Posted by: wholesaler business website, net shopping site, external wholesaler,dvd wholesaler,wholesaler definition,china wholesaler,mutual fund wholesaler,define wholesaler,insurance wholesaler at November 18, 2009 02:51 AM

Thanks for sharing.

Posted by: farmville cheats at November 18, 2009 07:40 AM

lxjuan08ed hardyis a famous ed hardy store which sell directly ed hardy clothing, shoes, boots, swim suit and other cheap ed hardy.

Browse through our catalogue of hundreds of ed hardy clothing,Ed hardy Shirt,Ed hardy ... Buy Ed Hardy today for a gift, for a friend, or for yourself.

ED HARDY Online Shop,Ed Hardy Store by Christian Audigier,coach handbags,rolex watches.We supply cheapest Newest ed hardy discount 2009 Commodity New styles.


ed hardy sale

Posted by: ed hardy at November 18, 2009 08:19 PM

We are the best online sales for the china wholesale . Here you can have a large of choices of kinds Ugg Boots,Converse Shoes,Timberland Boots,puma shoes,Nike Shox Shoes ,Nike Dunk SB Shoes,Nike Air Max,Links Of London,Tiffany Jewelry,Dior Handbags?,jimmy choo handbags ,Cartier Watches, 8GB Mp4 Players,Bluetooth Car DVDs. All our cheap online cheap goods are high quality and original packages, and best service. We offer our customers the best service, 7 days arrive at your door.Enjoy your easy and happy shopping with us.

Posted by: cheap goods sale. at November 20, 2009 12:20 AM

Do you like sports? If you like it,the NIke Air Max TN and Nike Air Max 360 is a good choice for you.And now the there is a good news,CheapNike Air Max running Shoes is waiting for you.I think The New Nike Air MAX 2009 Running Shoes makes you great comfortable.

Posted by: Nike Air Max shoes at November 20, 2009 03:38 AM

People all over the world know the abercrombie and fitch,but not everyone really knows how fashion the abercrombie is,hollister is the Legend maker. Everybody wears the hollister clothing would be the abercrombie mens and the abercrombie womens, if you want know you can search the Ruehl No.925 or abercrombie outlet in the www.google.com .

Posted by: fitch at November 21, 2009 03:51 AM

Your article is write very well, I like it very much ~
ghd Hair Straightener
ghd Hair Straighteners
ghd straighteners
GHD
hair straighteners
bose in ear headphones
bose on ear headphones
bose headphones
nike sb dunks
nike dunks
I wish you have a wonderful day!Thank you.

Posted by: ghd straighteners at November 22, 2009 08:26 PM

Timeberland boots on sale now! Do you want moreTimeberland boots information?Just move your mouse and press it to our Timberland shoes online store.

Posted by: Timberland uk at November 23, 2009 01:06 AM

Laptop Battery Laptop Battery Laptop Batteries
Laptop Batteries discount laptop battery
discount laptop battery
notebook battery notebook battery
computer battery computer battery
replacement laptop battery replacement laptop battery
notebook batteries notebook batteries

Posted by: Laptop Battery at November 24, 2009 10:15 PM

Ugg Classic Cardy

Boots

Ugg Classic Tall

Boots

Ugg Classic Short

Boots

Posted by: topuggshoes at November 26, 2009 02:32 AM

chaoying nfl jerseys
Giants Jerseys
Patriots Jerseys

Posted by: jerseysleague at November 29, 2009 04:09 AM

Paul Smith has been collaborating with a few different organizations recently and his latest venture sees him teaming up with Japanese bicycle saddle, Paul Smith bag, on a limited edition leather saddle. The release features Smith’s signature stripes where only 20 will be available at Paul Smith Clothing store in Japan and 8 at the Floral Street store in London. Get on it quick

Posted by: dasfw at November 29, 2009 11:16 AM

Just wanted to say great job with the blog, today is my first visit here and I’ve enjoyed reading your posts so far
ugg bailey button
Wow, my ugg classic mini will not be coming off now! I’ve had them on for 12hrs strait and I do not want to take them off. Thanks for everything, well worth the wait.

Posted by: ugg bailey button boots at November 30, 2009 05:38 AM

It is no doubt that Replica Louis Vuitton Speedy 30 enjoys highest reputation in the world. Louis Vuitton Speedy 30, purses, shoes, watches, clothes, and belts successfully seize loving heart of a majority of people. Speedy 30 is filled with romantic and fantasy, and it is a symbol of noble and high quality of life for gucci ever! Today, let us talk about Tivoli PM.belts have different styles and colors, such as Monogram series belts, Graffiti series belts, both men and women love them crazily. All of them match with various leisure clothes, formal business suits, and beautiful clothes very well. Whenever you wear a Louis Vuitton belt to make your public appearance, you will be naturally concerned and admired by every body. When we talk of high quality, there is no other company to compare with Louis Vuitton Replica Speedy 30. All Louis Vuitton belts adopt top quality cow leather, high quality hard ware, and the color will never be faded. The belts are so durable, and you can wear them to match your clothes for several years. Also, you can clearly see an obvious LV logo as a buckle of each belt.

Posted by: fanqin at December 1, 2009 01:02 AM
Post a comment









Remember info?




Prove you're human. Type "human":