
http://merd.net/pixel/language-study/syntax-across-languages.html CHANGES: - the page has now much more entries - one can check the missing entries per language at http://merd.net/pixel/language-study/syntax-across-languages-per-language.ht... (160KB) see especially the haskell one http://merd.net/pixel/language-study/syntax-across-languages-per-language.ht... Contributions, suggestions and fixes are welcome. -- Pixel programming languages addict http://merd.net/pixel/language-study/

On 11 Feb 2002, Pixel wrote:
http://merd.net/pixel/language-study/syntax-across-languages.html
CHANGES: - the page has now much more entries - one can check the missing entries per language at http://merd.net/pixel/language-study/syntax-across-languages-per-language.ht... (160KB) see especially the haskell one http://merd.net/pixel/language-study/syntax-across-languages-per-language.ht...
Contributions, suggestions and fixes are welcome.
Some random thoughts.... Under * Object Oriented & Reflexivity +---------------------------------------+ | class | class declaration | |-------------------+-------------------| | functional syntax | method invocation | +---------------------------------------+ I'm not sure anything under haskell can be considered OO but I'm no languages expert. You might could mention something about Type classes. This is the main (only) method of overloading functions in haskell. maybe you might make mention of polymorphic type system and how haskell uses type inferencing to determine the meaning of expressions (relates to type classes). Using data = Foo ... really should be meantioned as a way to construct new types. newtype = Foo ... (new type) type Foo = ... is only a type synonym. heres an example. type String = [Char] Under constrol structure, at least give mention to monads! Common examples (ST,IO,List,Maybe) and "do notation" Monads are defined by instanciating them under the Monad type class. (basically giving definitions for the operators >>= (aka 'bind') and >> I dont think you give list comprehensions a fair shake either. (btw, list comprehension is an example of an alternative syntax for using lists as a monad.) [f z elem | anylistfromlistoflists <- listoflists, element<-anylistfromlistoflists, elem >= 3, z <- someotherlist] References arent missing. They are implemented under both the ST and IO monads. sprintf ... show ?? (still show peruses no format specifiers so it can be kinda blah.) (!!) extracts a char from String since type String = [Char] oh, and it isnt missing those math functions either. Take another look at the Prelude (google search it as I'm to lazy to find out url for exact page in haskell 98 report) .... Oops, I see that "missing:" in your page really doesnt mean missing from language. A number of the "missing:" things (esp the math) can be had by just looking at the Haskell Report. a google search "Haskell 98 Report Prelude" probably gets you there. You Have quite a list of languages there. Seems like I've come upon your page before. I'm sure there'll be many more to bookmark it and use it. Jay Cox

Pixel
Contributions, suggestions and fixes are welcome.
The . operator is used for package scope when packages (modules) are imported "qualified". Indentation can optionally be replaced by { ; and }. In addition to . function composition, there's also $ function application, which often is more practical to use. Under "control flow", there's also case x of v0 -> e0 v1 -> e1 ... otherwise -> e and | bool0 = e0 | bool1 = e1 | ... | otherwise = en I'm not sure if "iterate" counts as loop forever? I can't remember any %-based sprintf off the top of my head, but hey, who'd want one anyway? As for "duplicate n times", I suppose you'll have to combine the two Prelude functions (concat . repeat n), while (map toUpper)/(map toLower) converts the case of strings, (!!) accesses an indexed character, and substrings of lenght l at position i can be extracted by (take l . drop i). (Remember that strings are lists!) Dictionaries can (inefficiently in some cases) be constructed using assoc lists, with the "lookup" function as accessor. (I'm not sure what you'd want to include of this, but at least some of it should be relevant, I hope.) -kzm -- If I haven't seen further, it is by standing in the footprints of giants

ketil@ii.uib.no (Ketil Z. Malde) writes: [...]
The . operator is used for package scope when packages (modules) are imported "qualified".
ok
Indentation can optionally be replaced by { ; and }.
ok
In addition to . function composition, there's also $ function application, which often is more practical to use.
I could put it in "grouping expressions": $ ... Haskell WDYT?
Under "control flow", there's also
case x of v0 -> e0 v1 -> e1 ... otherwise -> e
ok
and
| bool0 = e0 | bool1 = e1 | ... | otherwise = en
i put this one in category "if_then_else" (just like Lisp's "cond") uh... how can this be one-line written?
I'm not sure if "iterate" counts as loop forever?
I don't think there can be a "loop forever" construct in haskell? in OCaml, it is simply: let loop(f) = f() ; loop(f) and is only useful together with exceptions and side-effects.
I can't remember any %-based sprintf off the top of my head, but hey, who'd want one anyway?
me! I've always wondered how you internationalize a program without it? eg: "This encryption key is too simple (must be at least %d characters long)" "The package %s needs to be installed. Do you want to install it?" (I agree you don't need the full power of sprintf for this)
(!!) accesses an indexed character
ok
As for "duplicate n times", I suppose you'll have to combine the two Prelude functions (concat . repeat n), while (map toUpper)/(map toLower) converts the case of strings, , and substrings of lenght l at position i can be extracted by (take l . drop i). (Remember that strings are lists!)
i don't put any non-simple functions, the main aim is not to show how it can be done, but what are the various names used in various languages.
Dictionaries can (inefficiently in some cases) be constructed using assoc lists, with the "lookup" function as accessor.
there's a special entry for it: 'lookup an element in a association list' Thanks!

Jay Cox
I'm not sure anything under haskell can be considered OO but I'm no languages expert.
IMO, it's very alike "mix-ins", Java's interfaces, pure abstract classes.
You might could mention something about Type classes. This is the main (only) method of overloading functions in haskell.
maybe you might make mention of polymorphic type system and how haskell uses type inferencing to determine the meaning of expressions (relates to type classes).
syntax-across-languages doesn't favour big explainations :p
Using
data = Foo ... really should be meantioned as a way to construct new types. newtype = Foo ... (new type)
type Foo = ... is only a type synonym. heres an example. type String = [Char]
ok (i won't mention that i always have a hard time dinstiguishing them)
Under constrol structure, at least give mention to monads! Common examples (ST,IO,List,Maybe) and "do notation"
Monads are defined by instanciating them under the Monad type class. (basically giving definitions for the operators >>= (aka 'bind') and >>
please be more precise, what do i put? in which category?
I dont think you give list comprehensions a fair shake either. (btw, list comprehension is an example of an alternative syntax for using lists as a monad.)
[f z elem | anylistfromlistoflists <- listoflists, element<-anylistfromlistoflists, elem >= 3, z <- someotherlist]
i could add a category for list comprehension. is this a good approximate syntax? [ f x0 x1 ... | x0 <- a_list, pred x0, ... ]
References arent missing. They are implemented under both the ST and IO monads.
what is the syntax?
sprintf ... show ?? (still show peruses no format specifiers so it can be kinda blah.)
no, "show" is really in the 'convert something to a string' category
(!!) extracts a char from String since type String = [Char]
ok
oh, and it isnt missing those math functions either. Take another look at the Prelude (google search it as I'm to lazy to find out url for exact page in haskell 98 report)
ok, added.

Pixel
In addition to . function composition, there's also $ function application, which often is more practical to use.
I could put it in "grouping expressions":
$ ... Haskell
Yes, but that might apply to the . operator too. But yes, essentially it replaces parentheses.
| bool0 = e0 | bool1 = e1 | ... | otherwise = en
i put this one in category "if_then_else" (just like Lisp's "cond")
Yes, "cond" is exactly what it is, I think.
uh... how can this be one-line written?
Well, it needs to be part of something more, e.g. isZero x | x==0 = True | otherwise = False
I can't remember any %-based sprintf off the top of my head, but hey, who'd want one anyway?
me! I've always wondered how you internationalize a program without it?
That's easy - I don't :-) -kzm -- If I haven't seen further, it is by standing in the footprints of giants

On 11 Feb 2002, Pixel wrote:
Jay Cox
writes: Using
data = Foo ... really should be meantioned as a way to construct new types. newtype = Foo ... (new type)
type Foo = ... is only a type synonym. heres an example. type String = [Char]
ok (i won't mention that i always have a hard time dinstiguishing them)
Actually, I guess really should have explained myself a bit further. data Foo = ... only way to construct new structures (like lists,Trees, algebraic data types, enums, blah). Perhaps a (contrived) example for your syntax could be like a cross between a Tree and the Either type as in data Foo a b = FooLeft a | FooRight b | Tree (Foo a b) (Foo a b) So now you can construct values of say Foo Integer String. (I guess its a cousin to templates in C++) Or perhaps you need a separate section for syntax for constructing new datatypes.. data Foo x ... = A | B x ... | C (Foo ...) | ... the above expresses the fact that 1. Foo can "map" over any type x,y,etc 2. you can have (0 or more) argument constructors 3. datatypes can be recursive. Now, newtype Foo = ... like "type" but creates a new type as far as the type checker is concerned. type String = [Char] newtype String2 = [Char] foo:: String <=> foo :: [Char] but not foo::String2 <=> foo :: [Char] (Please dont take this as an insult by my re-explaining these things. I just want to make myself clear.)
Under constrol structure, at least give mention to monads! Common examples (ST,IO,List,Maybe) and "do notation"
Monads are defined by instanciating them under the Monad type class. (basically giving definitions for the operators >>= (aka 'bind') and >>
please be more precise, what do i put? in which category?
I guess I suggested control structure because in a way, you can "control the flow" from a semantic point of view of your program by building / choosing a specific monad. For instance, usage of the List monad brings nondeterminism into the language (although, in reality, the multiple solutions alluded to by the term are elements of a deterministically (is that a word?!) produced list) Example: do x<-[1,2,3] y<-[4,5,6] if (y<6) then return (10*x + y) else fail "" result:[14,15,24,25,34,35] aside: this is a rough and perhaps slightly incorrect translation from [10*x + y | x<-[1,2,3],y<-[4,5,6],y<6] However, the concept of a monad is language independent. It's just that Haskell does so much to accomodate them. I suppose a sollution to what to do with do-notation is to put something like do stmnt var <-stmtn etc or do {stmnt; var <-stmtn; etc} under your section named "Various Operators" with description "do statments under monad"
is this a good approximate syntax?
[ f x0 x1 ... | x0 <- a_list, pred x0, ... ]
sure.
References arent missing. They are implemented under both the ST and IO monads.
what is the syntax?
no syntax, only functions which create/manipulate them (under some monad). do{ x<-newSTRef exp; y <-readSTRef x; writeSTRef x exp} for IORefs do s/ST/IO/g to above line. I suppose that truely this isn't part of the main specs (not in Haskell Report/Haskell Library Report but implemented in ghc & Hugs iirc and modules must be imported, etc) so this concept of "Reference" in Haskell to might too be omitted, unless you want to contrast with SML. Jay Cox

[...]
(I guess its a cousin to templates in C++) Or perhaps you need a separate section for syntax for constructing new datatypes..
yes, i think so. With enums and ranges being a special case. i'll also put records [...]
(Please dont take this as an insult by my re-explaining these things. I just want to make myself clear.)
the pb is that the semantic is so different between the languages. So i just put all of them with no explaination.
Under constrol structure, at least give mention to monads! Common examples (ST,IO,List,Maybe) and "do notation"
Monads are defined by instanciating them under the Monad type class. (basically giving definitions for the operators >>= (aka 'bind') and >>
please be more precise, what do i put? in which category?
I guess I suggested control structure because in a way, you can "control the flow" from a semantic point of view of your program by building / choosing a specific monad. For instance, usage of the List monad brings nondeterminism into the language (although, in reality, the multiple solutions alluded to by the term are elements of a deterministically (is that a word?!) produced list)
"syntax across languages" is about syntax! If something needs much explaination, it can't be included :p [...]
I suppose a sollution to what to do with do-notation is to put something like [...] or do {stmnt; var <-stmtn; etc}
under your section named "Various Operators" with description "do statments under monad"
I don't accept entries where only one language fits in :p (this may change as soon as monads appear in more languages) [...]
References arent missing. They are implemented under both the ST and IO monads.
what is the syntax?
no syntax, only functions which create/manipulate them (under some monad).
do{ x<-newSTRef exp; y <-readSTRef x; writeSTRef x exp}
for IORefs do s/ST/IO/g to above line.
eurk ERROR "/usr/share/hugs/lib/exts/ST.hs":48 - Syntax error in type expression (unexpected `.') isn't there a way ST.hs would require the extensions? a pragma or something? someone not knowing the "-98" would wonder for a long time about what to do :-( anyway, the simplest examples i found: show $ runST (do { x <- newSTRef 2; writeSTRef x 3 ; readSTRef x }) so i can write: newSTRef in "reference (pointer)" "creation" readSTRef writeSTRef in "reference (pointer)" "dereference" WDYT? PS: show $ runST $ do { x <- newSTRef 2; writeSTRef x 3 ; readSTRef x } is not working :'-(

On 12 Feb 2002 14:49:16 +0100, Pixel
eurk
ERROR "/usr/share/hugs/lib/exts/ST.hs":48 - Syntax error in type expression (unexpected `.')
isn't there a way ST.hs would require the extensions? a pragma or something? someone not knowing the "-98" would wonder for a long time about what to do :-(
Being able to specify the extensions required in a source file would be very useful in a range of cases. Very often helping other people with Haskell (in the case where they are trying to use someone else's module) is just a question of saying "try '-98'" or "try '-fglasgow-exts'" or whatever. Cheers, Ganesh

Being able to specify the extensions required in a source file would be very useful in a range of cases. Very often helping other people with Haskell (in the case where they are trying to use someone else's module) is just a question of saying "try '-98'" or "try '-fglasgow-exts'" or whatever.
ghc and nhc98 already support this. ghc's mechanism is {-# OPTIONS -fglasgow-exts ... etc #-} at the top of the file - but there are quite a few restrictions on what flags are accepted in that pragma. nhc98's mechanism is {-# OPTIONS_COMPILE -nkpat ... etc #-} anywhere in the file, which has no restrictions on the options it accepts - you can use anything that can appear on the commandline. Incidentally, nhc98 also recognises the form {-# OPTIONS_LINK -L/usr/local/lib -lreadline ... etc #-} for extra link options, which are accumulated from all modules specified on the commandline. Regards, Malcolm

On 12 Feb 2002, Pixel wrote:
eurk
ERROR "/usr/share/hugs/lib/exts/ST.hs":48 - Syntax error in type expression (unexpected `.')
isn't there a way ST.hs would require the extensions? a pragma or something? someone not knowing the "-98" would wonder for a long time about what to do :-(
That could be nice, at least for ST.hs (in ghc the equivalent of hugs option "-98" isn't needed)
anyway, the simplest examples i found:
show $ runST (do { x <- newSTRef 2; writeSTRef x 3 ; readSTRef x })
so i can write:
newSTRef in "reference (pointer)" "creation" readSTRef writeSTRef in "reference (pointer)" "dereference"
WDYT?
If you understand your explanations for newSTRef etc. that's fine with me. My code in this thread (which coincidentally is a no-op) emphasizes which ones you would use "<-" with. So let me make one final adjustment so that: do{ x<-newSTRef 3; y <-readSTRef x; writeSTRef x (y+4);readSTRef x} Too long?
PS: show $ runST $ do { x <- newSTRef 2; writeSTRef x 3 ; readSTRef x } is not working :'-( _______________________________________________
Does typing in the following work? runST (do { x <- newSTRef 2; writeSTRef x 3 ; readSTRef x }) I can't explain and dont understand the error I get from the compiler with your code. (the haskell compilers I've used are not very helpful in error messages) Btw to get it to work in ghci you'd have to start: 1. ghci -package lang 2. load in some dummy file with 2 lines (so both Prelude and ST functions are available) ...(lines starting below!).... import ST main = print "hi" ....(lines ending)...... 3. type in runST ( ... ) Somebody would have to fill in the details for me about doing stuff in hugs. Jay

On Tue, 12 Feb 2002 14:43:22 +0000, Malcolm Wallace
Being able to specify the extensions required in a source file would be very useful in a range of cases. Very often helping other people with Haskell (in the case where they are trying to use someone else's module) is just a question of saying "try '-98'" or "try '-fglasgow-exts'" or whatever.
ghc and nhc98 already support this. [...]
Ah, thanks. I appeal to everyone distributing Haskell libraries to use them, then :-) Cheers, Ganesh
participants (5)
-
Ganesh Sittampalam
-
Jay Cox
-
ketil@ii.uib.no
-
Malcolm Wallace
-
Pixel