About the Question category


#1

(Replace this first paragraph with a brief description of your new category. This guidance will appear in the category selection area, so try to keep it below 200 characters. Until you edit this description or create topics, this category won’t appear on the categories page.)

Use the following paragraphs for a longer description, or to establish category guidelines or rules:

  • Why should people use this category? What is it for?

  • How exactly is this different than the other categories we already have?

  • What should topics in this category generally contain?

  • Do we need this category? Can we merge with another category, or subcategory?


#2

Looking for example code for functions returning List[Char] .
I’m new to this site (and more importantly Scala!) … Any tips?


#3

Hello,

Like this?

val fun1: Int => List[Char] = n => List.fill(n)(‘a’)

 Best, Oliver

#4

Hi Oliver,
Thanks for that and for getting back so quickly. That (your) function worked for me…

Yes, I have the following in the main function:

val empty = ListChar;
val full = List(‘a’, ‘b’, ‘c’);

println(empty.isEmpty); //this prints true (correct)

println(getToNext(full)); // this prints List(‘b’) (correct)

println(“checking is empty umm empty :” + empty.isEmpty); // this prints true(correct)
println(getToNext(empty)); // This prints List(‘b’) (INCORRECT)
}

def getToNext(chars:List[Char]): List[Char]=
{
// println(chars.isEmpty)
if (chars.isEmpty)
List(‘a’)
// if (!chars.isEmpty) chars.tail
List(‘b’)
}
Not sure if this is clear. But help appreciated :slight_smile:
Mairead


#5

Hi Oliver,
Apologies - I found where the problem was… I did not have an else in the getNext function … so the (implicit) return does not terminate the function as in Java…
Thanks …
Mairead