Sml tuples length - sml

I was interested in if there is a possible way to get length of tuple in sml?! See example
val tes = ((1,"test"),("test","some"))
Lenght(tes) = 2
I want it for a problem solve there is a problem which says to get students list which contains list for each student information but student information are different in two types some are like
(1,"test","nick")
and some are like
("name","nick")
So it wants to return the first element of each list in student lists see below:
((1,"test","nick"),("test2","nick2"),(2,"test3","nick3"))
Return > (1,"test2",2)
Here more info M Molbdnilo
#molbdnilo

An example of what you're most likely expected to do; define some useful sum types.
First, let's invent two ways to identify a person:
datatype Person = JustName of string
| NameAndNumber of string * int
datatype Identifier = Name of string
| Number of int
then you can get an Identifier for a Person:
fun identifier (JustName n) = Name n
| identifier (NameAndNumber (_, i)) = Number i
Let's test with some people:
- val people = [JustName "A", NameAndNumber ("B", 23), JustName "C", NameAndNumber ("D", 22)];
val people =
[JustName "A",NameAndNumber ("B",23),JustName "C",NameAndNumber ("D",22)]
: Person list
- map identifier people;
val it = [Name "A",Number 23,Name "C",Number 22] : Identifier list

Related

Converting a list to a map in Scala

I want to create a simple program that calculates someone's age after x years. so first you assign someone's current age to a variable, and then I want to use map to display the future ages.
What I have so far is:
val age = 18
val myList = (1 to 2000).toList
Basically, I want the numbers from the list and make it a map key. And for the value, it's a sum of variable and key. so the map would look like this:
1 -> 19, 2 -> 20, 3 -> 21......
How can I accomplish this?
Consider mapping to tuples
val age = 18
val ageBy: Map[Int, Int] = (1 to 2000).map(i => i -> (age + i)).toMap
ageBy(24) // res1: Int = 42

how to definie dataype tuple of lists of touples including lists in ocaml?

I am getting error when I define a type as: type table = (string * (string * string list) list) for encoding this value:
let atable = (
"Student",
[
("Id", ["2";"4";"7";"9"]);
("Name", ["Jim";"Linnea";"Steve";"Hannah"]);
("Gender",["Male";"Female";"Male";"Female"]);
("Course",["Geography";"Economics";"Informatics";"Geography"
])
The environment I am using is this one as I had difficulties using cygwin in Windows.
How to define a datatype satisfying the data table student above?
First, your expresion isn't syntactically valid. It's missing ]) at the end.
OCaml infers the types of things, so once you have a valid expression you can ask the toplevel for the type:
# let atable = (
"Student",
[
("Id", ["2";"4";"7";"9"]);
("Name", ["Jim";"Linnea";"Steve";"Hannah"]);
("Gender",["Male";"Female";"Male";"Female"]);
("Course",["Geography";"Economics";"Informatics";"Geography"
])]);;
val atable : string * (string * string list) list =
("Student",
[("Id", ["2"; "4"; "7"; "9"]);
("Name", ["Jim"; "Linnea"; "Steve"; "Hannah"]);
("Gender", ["Male"; "Female"; "Male"; "Female"]);
("Course", ["Geography"; "Economics"; "Informatics"; "Geography"])])
So the type is indeed string * (string * string list) list.
If you still have a problem after fixing the syntax, the problem must be elsewhere. It would be helpful to see a full example along with the specific error you're seeing.

Removing list object given field value python

Given
This is not simply removing elements from a list; its removing but by using field value
philani = Student(20, "Philani Sithole", "Male", [64,65])
sarah = Student(19, "Sarah Jones", "Female", [82,58])
fabian = Student(50, "Fabian Hamza", "Male", [50,52])
students = [philani, sarah, fabian]
How can I remove the object fabian from students list given the name "Fabian Hamza"
Desired results:
students = [philani, sarah]
It tried this
name = "Fabian Hamza"
for i in xrange(len(students)):
if hasattr(students[i], name):
students.pop(i)
But its not working
You can use a list comprehension to achieve that:
[s for s in students if s.name != 'Fabian Hamza']
If you want just the list of names, try:
[s.name for s in students if s.name != 'Fabian Hamza']

Retrieve matching values from branch to leaf

So im trying to make a tree with companies as the branch and subcompanies of the company as leafs.
Every company/subcompany consists of a name, a gross income and a list with their subcompanies.
I've made a function that returns a tuple with (name,gross income) for all the companies/subcompanies in the company tree.
What I would like to do now, is to find the total income for a certain company/subcompany, which means that the function should add all the subcompanies gross income together with the companies and return the result. It should only have a string as input (the name of the company).
type Department = a' list
and a' = | SubDep of string * float
| Company of string * float * Department;;
let company =
Company("Arla", 1000.0, [SubDep("Karolines Køkken", 500.0);
Company("Lurpak", 200.0, [SubDep("Butter",100.0); SubDep("Milk", 100.0)]);
SubDep("Arla Buko", 100.0);
Company("Riberhus", 200.0, [SubDep("Cheese",200.0)])
]);;
So an example run should be:
companyTotalIncome "Arla" company;;
And the result should be: 2400
To sum everything up you can do something like this (don't really know why you'll need the name of the company in here as for your example you just summed it all up):
let rec depIncome =
function
| SubDep (_, income) -> income
| Company (_,income, sub) -> income + totalIncome sub
and totalIncome =
List.sumBy depIncome
but your data structures are real strange (why list of companies a department?)
I would do maybe like this:
type Department =
{ name : String
income : float
}
type Company =
{ name : String
departmens : Department list
subCompanies : Company list
}
and then
let rec income company =
company.departments |> List.sumBy (fun d -> d.income }
+ company.subCompanies |> List.sumBy income
of course you can move the income to the company too (or add one there too) - I really don't know what you want to do - just my 5cts.

Filter a list of lists

I would like to know how to filter a whole list out of list of lists
Example: [ ["Bob", "Baker", "male", "70000"],
["Alice", "Allen", "female", "82000"] ]
And now I would like to filter the list which contains female. So output would be:
["Alice", "Allen", "female", "82000"]
Thanks
Ankur's answer will certainly solve your problem, but I would like to make a suggestion that could make your life easier. It seems that you're storing all your data as strings in lists, but really what you'd like is a data type that could hold all this data in a more organized fashion, which can be done using Haskell data types, something like:
data Person = Person {
firstName :: String,
lastName :: String,
gender :: String,
salary :: String
} deriving (Eq, Show)
Then you could easily sort your data with filter (("female" ==) . gender) a. While this is a bit more code up front, later on if you were to add a "title" field for "Mr", "Mrs", etc, then it wouldn't matter if you added it at as the first field or the last field, this code would still work. Also, if for whatever reason you had an invalid value like ["Bob", "Baker", "male", "70000", "female"], Ankur's solution would give you an incorrect result, but with a custom data type, this would not even compile.
You could further improve your data structure with a few tweaks. I would suggest making a data type for gender, and then use Int or Double for the salary field, so you would have
data Gender = Male | Female deriving (Eq, Show, Read)
data Person = Person {
firstName :: String,
lastName :: String,
gender :: Gender,
salary :: Int
} deriving (Eq, Show)
filtGender :: Gender -> [Person] -> [Person]
filtGender gend people = filter ((gend ==) . gender) people
main :: IO ()
main = do
let people = [Person "Bob" "Baker" Male 70000,
Person "Alice" "Allen" Female 82000]
putStr "The females are: "
print $ filtGender Female people
putStr "The males are: "
print $ filtGender Male people
Prelude> let a = [ ["Bob", "Baker", "male", "70000"], ["Alice", "Allen", "female", "82000"] ]
Prelude> filter (elem "female") a
[["Alice","Allen","female","82000"]]