module Classes =
type Student(name : string, age : int) =
member this.Name = name
member this.Age = age
/// An instances of the students class
let student1 = Student("bob", 20)
let student2 = Student("sally",18)
let student3 = Student("frank",22)
let student4 = Student("lisa",19)
let student5 = Student("john",17)
printfn "student 1: %s, age: %d" student1.Name student1.Age
printfn "student 2: %s, age: %d" student2.Name student2.Age
printfn "student 3: %s, age: %d" student3.Name student3.Age
printfn "student 4: %s, age: %d" student4.Name student4.Age
printfn "student 5: %s, age: %d" student5.Name student5.Age
let studentList = [student1; student2; student3; student4; student5]
// studentList |> List.iter (printf "%s , %d")
printfn "%O" studentList
printfn "%A" studentList
studentList |> List.iter (printf "%s" Student)
答案 0 :(得分:1)
这将是正确的代码:
studentList |> List.iter (fun s -> printf "Name: %s, Age: %s" s.Name s.Age)
或者这个:
studentList |> List.iter (printf "%A")
我真的无法想到从哪里开始解释原始版本的问题...对于初学者,为什么要将Student
作为第二个参数传递给最后一个printf
线?你甚至没有在任何地方定义,所以它有点令人费解。其次,我不会在这种模型中使用类,记录会更适合。
我真的,真的鼓励你在开始一个项目之前获得一些关于F#的理论知识。我可以全心全意地推荐的一个资源是fsharpforfunandprofit.com。