We can make a nested list in erlang by writing something like this:
NL = [[2,3], [1]].
[[2,3],[1]]
But assume we wrote it like this instead:
OL = [[2,3]|1].
[[2,3]|1]
Is OL still a list? Can someone please elaborate more what OL is?
This is called an improper list and should typically not be used. I think most library functions expects proper lists (e.g. length([1|2]) throws bad argument exception). Pattern matching with improper lists works though.
For some use cases, see Practical use of improper lists in Erlang (perhaps all functional languages)
More information about | and building list is given in Functional Programming: what is an "improper list"? .
Related
We can make a nested list in erlang by writing something like this:
NL = [[2,3], [1]].
[[2,3],[1]]
But assume we wrote it like this instead:
OL = [[2,3]|1].
[[2,3]|1]
Is OL still a list? Can someone please elaborate more what OL is?
This is called an improper list and should typically not be used. I think most library functions expects proper lists (e.g. length([1|2]) throws bad argument exception). Pattern matching with improper lists works though.
For some use cases, see Practical use of improper lists in Erlang (perhaps all functional languages)
More information about | and building list is given in Functional Programming: what is an "improper list"? .
new to F#
i need to store a bunch of lists of objects according to a float number where the collection of lists are sorted according to the float number. I know in C# i would use
SortedDictionary<float, List<obj>>
as the implementation is a red black tree, allowing for log(n) insert and search. But whats the best way to attack the situation in F#. I attempted to use SortedDitionary but i can't refer to SortedDictionary[int] to find the value so it renders it as useless essentially (i could be doing it wrong).
thanks for the help
The syntax is
sorteddictionary.[int]
then it works as you would expect
The first thig to do is read Okasaki's book Purely Functional Data Structures
It has ML implementations that may help you
You can use sorteddictionary.[int] as John Palmer already said but it may be worth pointing out that the F# standard library includes a purely functional sorted dictionary collection called Map.
I've been watching an interesting video in which type classes in Haskell are used to solve the so-called "expression problem". About 15 minutes in, it shows how type classes can be used to "open up" a datatype based on a discriminated union for extension -- additional discriminators can be added separately without modifying / rebuilding the original definition.
I know type classes aren't available in F#, but is there a way using other language features to achieve this kind of extensibility? If not, how close can we come to solving the expression problem in F#?
Clarification: I'm assuming the problem is defined as described in the previous video
in the series -- extensibility of the datatype and operations on the datatype with the features of code-level modularization and separate compilation (extensions can be deployed as separate modules without needing to modify or recompile the original code) as well as static type safety.
As Jörg pointed out in a comment, it depends on what you mean by solve. If you mean solve including some form of type-checking that the you're not missing an implementation of some function for some case, then F# doesn't give you any elegant way (and I'm not sure if the Haskell solution is elegant). You may be able to encode it using the SML solution mentioned by kvb or maybe using one of the OO based solutions.
In reality, if I was developing a real-world system that needs to solve the problem, I would choose a solution that doesn't give you full checking, but is much easier to use.
A sketch would be to use obj as the representation of a type and use reflection to locate functions that provide implementation for individual cases. I would probably mark all parts using some attribute to make checking easier. A module adding application to an expression might look like this:
[<Extends("Expr")>] // Specifies that this type should be treated as a case of 'Expr'
type App = App of obj * obj
module AppModule =
[<Implements("format")>] // Specifies that this extends function 'format'
let format (App(e1, e2)) =
// We don't make recursive calls directly, but instead use `invoke` function
// and some representation of the function named `formatFunc`. Alternatively
// you could support 'e1?format' using dynamic invoke.
sprintfn "(%s %s)" (invoke formatFunc e1) (invoke formatFunc e2)
This does not give you any type-checking, but it gives you a fairly elegant solution that is easy to use and not that difficult to implement (using reflection). Checking that you're not missing a case is not done at compile-time, but you can easily write unit tests for that.
See Vesa Karvonen's comment here for one SML solution (albeit cumbersome), which can easily be translated to F#.
I know type classes aren't available in F#, but is there a way using other language features to achieve this kind of extensibility?
I do not believe so, no.
If not, how close can we come to solving the expression problem in F#?
The expression problem is about allowing the user to augment your library code with both new functions and new types without having to recompile your library. In F#, union types make it easy to add new functions (but impossible to add new union cases to an existing union type) and class types make it easy to derive new class types (but impossible to add new methods to an existing class hierarchy). These are the two forms of extensibility required in practice. The ability to extend in both directions simultaneously without sacrificing static type safety is just an academic curiosity, IME.
Incidentally, the most elegant way to provide this kind of extensibility that I have seen is to sacrifice type safety and use so-called "rule-based programming". Mathematica does this. For example, a function to compute the symbolic derivative of an expression that is an integer literal, variable or addition may be written in Mathematica like this:
D[_Integer, _] := 0
D[x_Symbol, x_] := 1
D[_Symbol, _] := 0
D[f_ + g_, x_] := D[f, x] + D[g, x]
We can retrofit support for multiplication like this:
D[f_ g_, x_] := f D[g, x] + g D[f, x]
and we can add a new function to evaluate an expression like this:
E[n_Integer] := n
E[f_ + g_] = E[f] + E[g]
To me, this is far more elegant than any of the solutions written in languages like OCaml, Haskell and Scala but, of course, it is not type safe.
I have a field ($P{ORDER}.permit) which is Integer (0,1) and I'd like to display it as a String ("No", "Yes"). So I added below keys to ResourceBoundle:
order.permit.0=No
order.permit.1=Yes
I wrote expression $R{order.permit.$P{ORDER}.permit} but it doesn't work. An exception is thrown
net.sf.jasperreports.engine.JRException: Too many groovy classes were
generated. Please make sure that you don't use Groovy features such as
closures that are not supported by this report compiler.
I suspect that this exception is caused by nesting jasper expressions or nesting them in wrong way.
How should I write the expression to achieve desired result?
EDIT: str("order.permit." + $P{ORDER}.permit) is the answer. Details in the below post.
Use str() instead of $R{}.
See also http://jasperforge.org/plugins/espforum/view.php?group_id=102&forumid=103&topicid=54665:
$R{} and str() are largely the same thing. The functional difference
is that $R{} can only be used with fixed/static keys, while str() can
be used with dynamic message keys, e.g. str("message.prefix." +
$P{message}).
I'm quite puzzled how the syntax highlighting feature here on SO works, but I have seen similar somewhere else. How does this work?
Is there one parser which can parse multiple languages at once?
Or, are several passes of different parsers needed and the best parsing result is used?
Or, is only a shallow analysis performed and the language is then guessed based on heuristics?
And if one of these is true, how does it work?
Check out Javascript code prettifier on Google Code.