我正在继续学习Haskell,我已经定义了一个名为SplitUp的程序如下:
<?php
namespace App;
use Illuminate\Auth\Authenticatable;
use Illuminate\Database\Eloquent\Model;
use Illuminate\Auth\Passwords\CanResetPassword;
use Illuminate\Foundation\Auth\Access\Authorizable;
use Illuminate\Contracts\Auth\Authenticatable as AuthenticatableContract;
use Illuminate\Contracts\Auth\Access\Authorizable as AuthorizableContract;
use Illuminate\Contracts\Auth\CanResetPassword as CanResetPasswordContract;
class User extends Model implements AuthenticatableContract,
AuthorizableContract,
CanResetPasswordContract
{
use Authenticatable, Authorizable, CanResetPassword;
/**
* The database table used by the model.
*
* @var string
*/
protected $table = 'users';
/**
* The attributes that are mass assignable.
*
* @var array
*/
protected $fillable = ['name', 'username', 'password', 'role', 'active', 'email'];
/**
* The attributes excluded from the model's JSON form.
*
* @var array
*/
protected $hidden = ['password', 'remember_token'];
}
splitUp的作用是将字符串及其分隔符拆分。例如,“One.Word!”会去“。!”,“”一个“,”一词“”
我不明白为什么用程序设置的方式为什么输入没有以x:word:words的递归的相反顺序给出?因为如果我们将x添加到(单词:单词)列表的前面并且递归不应该返回像droW enO这样的东西吗?
答案 0 :(得分:4)
我会尽力回答我认为误解的原因。
让我们定义一个简单的递归函数,只需要一个字符串并将其重新组合在一起。
rewrite :: String -> String
rewrite [] = ""
rewrite x = x
rewrite (x:xs) = x : rewrite xs
正如您所看到的,我将x
放在列表的开头并附加递归调用,类似于您的示例。这只会给我一个起始字符串本身,而不是它的反向。让我们看一下将“Hello”传递给函数的分步示例。
rewrite "Hello" = 'H' : rewrite "ello"
= 'H' : 'e' : rewrite "llo"
= 'H' : 'e' : 'l' : rewrite "lo"
= 'H' : 'e' : 'l' : 'l' : rewrite "o"
= 'H' : 'e' : 'l' : 'l' : 'o'
= "Hello" -- and we're done